Can data members be constant (with const) so that they are given a value by the constructor and them 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)
Thanks for the answer, but in the example a static const variable is defined inside the class! I thought it is illegal. Doesn't it generate an error? (if not, I would like to know why the tutorial states that static data members need to be defined in the global scope)
Thanks for the answers. Now what about a static const class member whose type is non-integral (like a class)? Where is it defined?
Another question: If the base class has a virtual function, will the inherited function (the one belonging to the derived class) be virtual too automatically?
I think I read something about it, but I'm not sure. If C inherits from B, which inherits from A, and A has a virtual function, will the compiler automatically assume that B's function is virtual too? (It's critical if a program runs the function through a pointer to B which points to an object of type C, since if B's function is automatically marked as virtual and C is overriding the function, the result will be execution of C's function, while if B's function is not virtual, the result will be execution of B's function (which is inherited from A) because members declared in C cannot be accessed through a pointer of type B* . I'm sorry if this question is hard to understand...I tried to explain it clearly.
If C inherits from B and b inherits from A, the function will probably be virtual in the derived classes, I agree, otherwise things would not make sense, but what happens if there is a pointer of type B*, pointing on an object of class C, and you execute the function for that object?
What happens if both classes, base and derived, have functions with the same parameters, the same name and the same return type, but the one in the base class is not virtual? Is it inherited too or running executing the function for an object of type Derived through a pointer to Base will generate an error? (yes, I know I'm asking a lot of questions I could answer by trying things in C++)
struct A
{
virtualvoid print1(){cout << "That's an 'A'!\n";}
void print2(){cout << "That's an 'A'!(2)\n";}
};
struct B: public A
{
void print1(){cout << "That's a 'B'!\n";}
void print2(){cout << "That's a 'B'!(2)\n";}
};
struct C: public B
{
void print1(){cout << "That's a 'C'!\n";}
void print2(){cout << "That's a 'C'!(2)\n";}
};
int main()
{
B* pt = new C;
pt->print1();
pt->print2();
return 0;
}
...which means that if the base class class and the derived class have functions with the same name but the base class's function is not virtual, it still can be accessed through a pointer pointing to an object of the derived class - but the derived class's function cannot be accessed through a pointer of type Base.
and if A's function is virtual, B's function behaves exactly as it would behave if it was explicitly defined as virtual.
Thanks for you help. :) BUT I have another question: why do integral type static const variables have to be defined inside the class, while non-integral ones have to be defined in the global scope? (I'm trying to understand the logic behind this...)
No, I forgot "integral".
So integral static const data members are the only members that can be initialized in the class body but they can have their value set outside the class as all the other static members.
At http://www.cplusplus.com/doc/tutorial/classes2.html is explained why static members have to be initialized outside the class
Thanks, but I still have this question: Why integral types can be initialized inside the class, while other types can't? (I'm trying to understand the logic behind this...)
Ok, the answer to the static const question is "because the compiler is stupid".
1 2 3 4 5 6 7
// File B.h:
struct B {
staticconst std::string helloWorld;
};
// Then in B.cpp:
const std::string B::helloWorld( "Hello World!" );
This is one of my pet peeves about C++.
Actually I believe the real answer is that for trivial POD types, the compiler needn't generate any code to perform the initialization. As soon as the compiler has to generate code, it doesn't quite know how to if it is parsing a header file because all .cpp files that included the header directly or indirectly would end up with said code.
Second question:
Once a function is declared virtual it is always virtual in all derived classes even if not declared so.