Re: Are Base data members accessible in Derived constructor initialiser
list?
* Joost Kraaijeveld:
Alf P. Steinbach wrote:
* Joost Kraaijeveld:
Are Base data members accessible in Derived constructor initialiser
list and is the following legal?
class Base
{
public:
protected:
std::string text; };
class Derived : public Base
{
public:
Derived(const std::string& aText)
: text(aText)
{}
};
It seems that FAQ item 5.2 covers this.
I assume that you are referring to the home work section? It is not, I
was actually hoping for an answer in terms of C++ language rules.
I have read 12.6.2 but I could not determine if this is valid C++. Hence
my question.
This is discussed in ?12.6.2/2 of the standard.
Essentially, you can use a mem-initializer to initialize a direct
non-static data member of the constructor's class (not of a base class),
and/or a direct base class, and/or a virtual base class.
The latter is because a virtual base class may need to be initialized by
a most derived class' constructor (consider the diamond inheritance
pattern case where two or more classes derive virtually from a class and
provide different initializers, then at bottom a common most derived
class), so it is a case where a mem-initializer can be ignored...
To initialize base class members use a base class constructor.
Alternatively or additionally, I suggest you try the code with a C++
compiler.
The code gets rejected by gcc 4.2.
Which is correct.
Cheers, & hth.,
- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?