4

i know that you can not initialize the member variable(other than static const) inside the class directly without using constructor.

but just wanted to know what is the reason behind this. below is the code snippet

if any body could help

class a 
{

    int c=5;

// giving error error C2864: 'a::c' : only static const integral data members can be 

// initialized within a class

    int b;


public:
    a():c(1),b(2){}

    void h()
    {
        printf("%d,%d",c,b);
    }
};

int main()
{

    a l;

    l.h();

    getchar();
}
David G
  • 94,763
  • 41
  • 167
  • 253
hims15
  • 96
  • 1
  • 4
  • I mean, different languages have different restrictions... Not sure what kind of answer you're looking for. In C++, instance-specific initialization must be inside the constructor or its initialization list. – jedwards Apr 01 '13 at 18:41
  • possible duplicate of [Why can't I have a non-integral static const member in a class?](http://stackoverflow.com/questions/370283/why-cant-i-have-a-non-integral-static-const-member-in-a-class) – Bo Persson Apr 01 '13 at 18:42
  • @jedwards that is something i want to know why instances are not allowed to be initialized directly. – hims15 Apr 01 '13 at 19:05
  • @BoPersson : i am unable to understand answer given in that post...can you explain .Thanks in advance – hims15 Apr 01 '13 at 19:08
  • @Bo Persson: Not a duplicate, for three reasons: this example is integral, non-static and non-const. All three differences are relevant. Non-integral variables may have different behavior on host and target environments, non-static members can be mentioned in ctors, and for non-const variables the moment of initialization is more critical. – MSalters Apr 02 '13 at 07:01

2 Answers2

5

Actually you can. But only in C++11.

The following is a valid C++11 code:

class A
{
    int x = 100; //valid in c++11
};

Your compiler might not support this, but GCC 4.8.0 compiles it fine.

Hope that helps.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • @ArmenTsirunyan, GCC and Clang do. – chris Apr 01 '13 at 18:45
  • @Armen: It is [GCC 4.8.0](http://coliru.stacked-crooked.com/view?id=8c0efcb8cc51f614370b5735b9388d86-61c3814520a8d4318f681038dc4b4da7) – Nawaz Apr 01 '13 at 18:45
  • I know it was supported before 4.8.0. 4.7.1 perhaps? – chris Apr 01 '13 at 18:47
  • @chris: Maybe. I don't remember if I tried this before. I didn't write any code recently that requires such initialization actually. – Nawaz Apr 01 '13 at 18:49
1

The class definition is mainly meant to tell other classes what interface your class will have and how much memory it occupies, as well as any values associated with the class that are already known at compile time (i.e. constants). There is no executable code directly in the class definition (although there will probably be executable code in a function that's defined within the class's definition). The code that will execute lies in the definitions of the functions themselves.

Edit: Apparently this is supported in C++11.

maditya
  • 8,626
  • 2
  • 28
  • 28