Initialization of members in h file

B

Bari

Hi all,

I just wanted to know if I can ininialize data members in the header.
The compiler (gcc 2.96) returns an error.
The data members are not const (I know that const members can't be
initiated in the header).

Does anyone no the reason?

Thanks alot,
Bari
 
V

Victor Bazarov

Bari said:
I just wanted to know if I can ininialize data members in the header.

Only static ones and only ones of a const integral type.
The compiler (gcc 2.96) returns an error.

That's a very old compiler. Try something newer.
The data members are not const (I know that const members can't be
initiated in the header).
Huh?

Does anyone no the reason?

The reason is that the Standard prohibits initialisation of non-static
data members anywhere except the constructor initialiser list, and also
prohibits initialisation of static members if they are non-const or if
they are of non-integral type.

V
 
J

Jakob Bieling

I just wanted to know if I can ininialize data members in the header.
The compiler (gcc 2.96) returns an error.

-- header.h --
class test
{
int i;
int const j;
public:
test () : i (0), j (0) {}
};
-- end header.h --

This should work .. ?
The data members are not const (I know that const members can't be
initiated in the header).

See above. They can be initialized there as well. Either I am
misunderstanding what you are trying to do, or you are misunderstanding
how and where you can initialize stuff.

hth
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top