Initializing a base class

D

Dave

class base
{
public:
base(const base &other) { // Init. here... }
// Stuff
};

class derived: public base
{
public:
derived(const derived &other): base(???) {}
};


What is the proper syntax for the initializer list in the derived
constructor to initialize its base subobject from the base subobject of
other?
 
J

Jeff Schwab

Dave said:
class base
{
public:
base(const base &other) { // Init. here... }
// Stuff
};

class derived: public base
{
public:
derived(const derived &other): base(???) {}
};


What is the proper syntax for the initializer list in the derived
constructor to initialize its base subobject from the base subobject of
other?

See TC++PL/p306. Call a constructor of the base class, as though the
base class's name were the name of a member variable.

class Base
{
public:
Base( int );
};

class Derived: Base
{
public:
Derived( ):
Base( 3 )
{

}
};
 
D

Dave

Jeff Schwab said:
See TC++PL/p306. Call a constructor of the base class, as though the
base class's name were the name of a member variable.

class Base
{
public:
Base( int );
};

class Derived: Base
{
public:
Derived( ):
Base( 3 )
{

}
};

But I'm talking specifically about the case of a copy constructor. Passing
an int is easy, but I don't know how to pass the base subobject of parameter
"other". Basically, I need to fill in the "???" as it appears in my
original post.
 
J

Jeff Schwab

But I'm talking specifically about the case of a copy constructor. Passing
an int is easy, but I don't know how to pass the base subobject of parameter
"other". Basically, I need to fill in the "???" as it appears in my
original post.

How do you expect to construct an object that can be constructed only
from an existing object of the same type? Unless the "Stuff" you
mentioned includes another constructor, I think you've got a design flaw
here. Of course, if you really want to walk on the dark side, you could
do something like this:

struct B { B( B const& ) { } };
struct D: B { D( ): B( *this ) { } };


-Jeff
 
D

Dave

Jeff Schwab said:
How do you expect to construct an object that can be constructed only
from an existing object of the same type? Unless the "Stuff" you

That's exactly what copy constructors are all about!!!
mentioned includes another constructor, I think you've got a design flaw
here. Of course, if you really want to walk on the dark side, you could
do something like this:

struct B { B( B const& ) { } };
struct D: B { D( ): B( *this ) { } };


-Jeff

Surely there is a correct way to copy construct derived objects. I'm only
trying to find out how it should properly be done! Let me modify the code
snippet you offered above:

struct B { B( B const& ) { } };
struct D: B { D( D const &other): B( ??? ) { } };

If I wanted to fill in the "???" such that initialization of B occurrs from
the B subobject of "other", how would I do so? other::B doesn't work,
other.B doesn't work, etc...
 
J

Jeff Schwab

Dave said:
That's exactly what copy constructors are all about!!!

To *copy* an object, you first have to *have* an object. How do you
create the first such object, if the class has only a copy-constructor?
Surely there is a correct way to copy construct derived objects. I'm only
trying to find out how it should properly be done! Let me modify the code
snippet you offered above:

struct B { B( B const& ) { } };
struct D: B { D( D const &other): B( ??? ) { } };

If I wanted to fill in the "???" such that initialization of B occurrs from
the B subobject of "other", how would I do so? other::B doesn't work,
other.B doesn't work, etc...

The D is a B, so just do this:

struct D: B { D( D const &other): B( other ) { } };

Hth,
Jeff
 
D

Dave

Jeff Schwab said:
To *copy* an object, you first have to *have* an object. How do you
create the first such object, if the class has only a copy-constructor?

Implicit default constructor.
 
J

Jeff Schwab

Implicit default constructor.

If you declare any constructor for your class, the default constructor
will not be generated. Specifically, if you declare only a copy
constructor, then there is no implicit no-arg constructor.
 
D

Dave

Jeff Schwab said:
If you declare any constructor for your class, the default constructor
will not be generated. Specifically, if you declare only a copy
constructor, then there is no implicit no-arg constructor.

Oh, of course! My bad... So, just assume it was part of "stuff"...

In any case, my question has been answered - I should have just realized
that I could pass "other" directly to the base class constructor and that it
would simply be sliced... Thanks for clearing it all up!
 

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,774
Messages
2,569,598
Members
45,161
Latest member
GertrudeMa
Top