473,508 Members | 4,712 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Initializing a base class

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?
Jul 22 '05 #1
8 1740
Dave wrote:
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 )
{

}
};

Jul 22 '05 #2

"Jeff Schwab" <je******@comcast.net> wrote in message
news:sM********************@comcast.com...
Dave wrote:
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 )
{

}
};


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.
Jul 22 '05 #3
Dave wrote:
"Jeff Schwab" <je******@comcast.net> wrote in message
news:sM********************@comcast.com...
Dave wrote:
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.


<snip>code</>
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

Jul 22 '05 #4

"Jeff Schwab" <je******@comcast.net> wrote in message
news:or********************@comcast.com...
Dave wrote:
"Jeff Schwab" <je******@comcast.net> wrote in message
news:sM********************@comcast.com...
Dave wrote:

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.
<snip>code</>
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


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...
Jul 22 '05 #5
Dave wrote:
"Jeff Schwab" <je******@comcast.net> wrote in message
news:or********************@comcast.com...
Dave wrote:
"Jeff Schwab" <je******@comcast.net> wrote in message
news:sM********************@comcast.com...
Dave wrote:
>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.


<snip>code</>
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

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


The D is a B, so just do this:

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

Hth,
Jeff

Jul 22 '05 #6

"Jeff Schwab" <je******@comcast.net> wrote in message
news:sO********************@comcast.com...
Dave wrote:
"Jeff Schwab" <je******@comcast.net> wrote in message
news:or********************@comcast.com...
Dave wrote:

"Jeff Schwab" <je******@comcast.net> wrote in message
news:sM********************@comcast.com...
>Dave wrote:
>
>
>>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.

<snip>code</>

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

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?


Implicit default constructor.
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...


The D is a B, so just do this:

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

Hth,
Jeff

Jul 22 '05 #7
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.


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.

Jul 22 '05 #8

"Jeff Schwab" <je******@comcast.net> wrote in message
news:y_********************@comcast.com...
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.


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!
Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

50
6290
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong...
3
3468
by: J.J. Feminella | last post by:
(Please disregard the previous message; I accidentally sent it before it was completed.) I have source code similar to the following. public class Vehicle { protected string dataV; // ......
3
5692
by: Diebels | last post by:
Hi, I have some problems using static variables which results in a core dump. I have attached code and coredump to the end of my message. I am trying to implement a kind of factory design. I...
4
1795
by: Jamie Hankins | last post by:
I'm probably being dense here. In the following situation: class Base { int x; int y; } class Decendant : Base { int z; }
2
1892
by: eriwik | last post by:
Given a simple class like class test { private: size_t size_; int* data_; public: test(size_t s) : size_(s), data_(new int { /* ... */ };
0
7228
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7128
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7332
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7393
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7502
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5057
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4715
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
426
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.