Problems with Static Data Member reference type, may be a compiler error

I believe that the static data member of the class can be a reference type initialized by a global variable as follows:

#include <iostream>
#include <stdio.h>

const unsigned int global =0x1fee;
struct K {
    int a;
    static const  int & iref;
};
const int & K::iref=global;
int main()
{
    printf("%d\n",K::iref);
    return 0;

}

But in my VC ++ test, it can compile, but throw an exception when it is executed. Is this just a bug with VC ++? Did I do something wrong?

+4
source share
1 answer

You just need

const int & k :: Iref

to

to :: Iref

And you need to initialize the variable when you declare, and not outside its scope.

0
source

Source: https://habr.com/ru/post/1663392/


All Articles