Could the data member be const?

Could data members be constant (with const) so that they are given a value by the constructor and then cannot be changed? If yes, can a data member be static const? (I'm asking the second question because a static member is defined outside the class while constant variables have to be defined and declared in one line.


// Example of constant member
struct A{
const int a;
A(int b):a(b){}

}

// Example of static constant member
struct B{
static const int a=15;
}

RULE: only the static integar variable could be defined inside the class. Other type of variables have to be defined outside the class.
RULE: const variables have to be defined and declared in one line.

---------------------------------------------------------
Static const integral could be as member array size:

struct B
{
static const int i=8;
char c[i];
};

works fine

------------------------------------------------------------
struct B
{
static const int i;
char c[i];
};
const int B::i = 8;

the compiler doesn't konw the size that 'c' would have

---------------------------------------------------------------

No comments:

Post a Comment