473,667 Members | 2,524 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 1747
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******@comca st.net> wrote in message
news:sM******** ************@co mcast.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******@comca st.net> wrote in message
news:sM******** ************@co mcast.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
constructo r 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******@comca st.net> wrote in message
news:or******** ************@co mcast.com...
Dave wrote:
"Jeff Schwab" <je******@comca st.net> wrote in message
news:sM******** ************@co mcast.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
constructo r 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******@comca st.net> wrote in message
news:or******** ************@co mcast.com...
Dave wrote:
"Jeff Schwab" <je******@comca st.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
>constructo r 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******@comca st.net> wrote in message
news:sO******** ************@co mcast.com...
Dave wrote:
"Jeff Schwab" <je******@comca st.net> wrote in message
news:or******** ************@co mcast.com...
Dave wrote:

"Jeff Schwab" <je******@comca st.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
>>constructo r 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******@comca st.net> wrote in message
news:y_******** ************@co mcast.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
6343
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 def foo(self, data): self.attr1=data self.attr2.append(data) The initialization of attr1 is obviously OK, all instances of Father redefine it in the method foo. But the initialization of attr2 is wrong
3
3488
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; // ... more protected fields }
3
5702
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 have a base class with several sub classes. In runtime I want to create a instance of a sub class and assign it to a base class pointer. Nothing fancy about that. I also want to be able in runtime to decide witch type of sub class that is to be...
4
1802
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
1899
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
8459
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8367
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8889
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8570
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8650
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7391
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4202
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2017
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.