Are Base data members accessible in Derived constructor initialiserlist?

J

Joost Kraaijeveld

Hi,

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)
{}
};


TIA

Joost
 
A

Alf P. Steinbach

* 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.

Alternatively or additionally, I suggest you try the code with a C++
compiler.


Cheers, & hth.,

- Alf
 
S

Salt_Peter

Hi,

Are Base data members accessible in Derived constructor initialiser list
and is the following legal?

No to legal. What is the purpose of Base's constructor(s)?
 
J

Joost Kraaijeveld

Alf said:
* Joost Kraaijeveld:

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.
Alternatively or additionally, I suggest you try the code with a C++
compiler.
The code gets rejected by gcc 4.2.

TIA

Joost
 
D

dertopper

Hi,

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)
   {}

};

You said in message [email protected]
that g++ rejects your code, so there was probably some kind of error
message that should have got you on the right way (like "Error: cannot
access base class members in initialization list").

I suspect that what you _really_ want to know is the reason why one
cannot initialize base class members in an initialization list. This
is because at the time the initialization list is "executed", all base
class members are already constructed. Were you able to put base class
members in your initialization list you'd call their constructors
twice, which doesn't make sense. Note that you can use the assignment
operator if you want to change the value of base class members (if
such an operator is available).

Regards,
Stuart
 
A

Alf P. Steinbach

* Joost Kraaijeveld:
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.

The code gets rejected by gcc 4.2.

Which is correct.


Cheers, & hth.,

- Alf
 
J

Joost Kraaijeveld

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.
OK, thanks

Joost
 

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
473,965
Messages
2,570,148
Members
46,710
Latest member
FredricRen

Latest Threads

Top