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? | | | | re: Initializing a base class
Dave wrote:[color=blue]
> 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?[/color]
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 )
{
}
}; | | | | re: Initializing a base class
"Jeff Schwab" <jeffplus@comcast.net> wrote in message
news:sM2dncXTsZ6Gh3eiRVn-tA@comcast.com...[color=blue]
> Dave wrote:[color=green]
> > 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?[/color]
>
> 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 )
> {
>
> }
> };
>[/color]
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. | | | | re: Initializing a base class
Dave wrote:[color=blue]
> "Jeff Schwab" <jeffplus@comcast.net> wrote in message
> news:sM2dncXTsZ6Gh3eiRVn-tA@comcast.com...
>[color=green]
>>Dave wrote:
>>[color=darkred]
>>>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?[/color]
>>
>>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.[/color][/color]
<snip>code</>
[color=blue]
> 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.[/color]
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 | | | | re: Initializing a base class
"Jeff Schwab" <jeffplus@comcast.net> wrote in message
news:orKdnRMSNegAgHeiRVn-jw@comcast.com...[color=blue]
> Dave wrote:[color=green]
> > "Jeff Schwab" <jeffplus@comcast.net> wrote in message
> > news:sM2dncXTsZ6Gh3eiRVn-tA@comcast.com...
> >[color=darkred]
> >>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.[/color][/color]
>
> <snip>code</>
>[color=green]
> > But I'm talking specifically about the case of a copy constructor.[/color][/color]
Passing[color=blue][color=green]
> > an int is easy, but I don't know how to pass the base subobject of[/color][/color]
parameter[color=blue][color=green]
> > "other". Basically, I need to fill in the "???" as it appears in my
> > original post.[/color]
>
> 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[/color]
That's exactly what copy constructors are all about!!!
[color=blue]
> 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
>[/color]
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... | | | | re: Initializing a base class
Dave wrote:[color=blue]
> "Jeff Schwab" <jeffplus@comcast.net> wrote in message
> news:orKdnRMSNegAgHeiRVn-jw@comcast.com...
>[color=green]
>>Dave wrote:
>>[color=darkred]
>>>"Jeff Schwab" <jeffplus@comcast.net> wrote in message
>>>news:sM2dncXTsZ6Gh3eiRVn-tA@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.[/color]
>>
>><snip>code</>
>>[color=darkred]
>>>But I'm talking specifically about the case of a copy constructor.[/color][/color]
>
> Passing
>[color=green][color=darkred]
>>>an int is easy, but I don't know how to pass the base subobject of[/color][/color]
>
> parameter
>[color=green][color=darkred]
>>>"other". Basically, I need to fill in the "???" as it appears in my
>>>original post.[/color]
>>
>>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[/color]
>
>
> That's exactly what copy constructors are all about!!![/color]
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?
[color=blue][color=green]
>>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
>>[/color]
>
>
> 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...[/color]
The D is a B, so just do this:
struct D: B { D( D const &other): B( other ) { } };
Hth,
Jeff | | | | re: Initializing a base class
"Jeff Schwab" <jeffplus@comcast.net> wrote in message
news:sO2dnbxvVNlnsnei4p2dnA@comcast.com...[color=blue]
> Dave wrote:[color=green]
> > "Jeff Schwab" <jeffplus@comcast.net> wrote in message
> > news:orKdnRMSNegAgHeiRVn-jw@comcast.com...
> >[color=darkred]
> >>Dave wrote:
> >>
> >>>"Jeff Schwab" <jeffplus@comcast.net> wrote in message
> >>>news:sM2dncXTsZ6Gh3eiRVn-tA@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[/color][/color][/color]
of[color=blue][color=green][color=darkred]
> >>>>>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.[/color]
> >
> > Passing
> >[color=darkred]
> >>>an int is easy, but I don't know how to pass the base subobject of[/color]
> >
> > parameter
> >[color=darkred]
> >>>"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[/color]
> >
> >
> > That's exactly what copy constructors are all about!!![/color]
>
> 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?
>[/color]
Implicit default constructor.
[color=blue][color=green][color=darkred]
> >>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
> >>[/color]
> >
> >
> > Surely there is a correct way to copy construct derived objects. I'm[/color][/color]
only[color=blue][color=green]
> > trying to find out how it should properly be done! Let me modify the[/color][/color]
code[color=blue][color=green]
> > 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[/color][/color]
from[color=blue][color=green]
> > the B subobject of "other", how would I do so? other::B doesn't work,
> > other.B doesn't work, etc...[/color]
>
> The D is a B, so just do this:
>
> struct D: B { D( D const &other): B( other ) { } };
>
> Hth,
> Jeff
>[/color] | | | | re: Initializing a base class
[color=blue][color=green]
>>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?
>>[/color]
>
>
> Implicit default constructor.[/color]
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. | | | | re: Initializing a base class
"Jeff Schwab" <jeffplus@comcast.net> wrote in message
news:y_2dnfDYeInr2HeiRVn-sA@comcast.com...[color=blue]
>[color=green][color=darkred]
> >>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?
> >>[/color]
> >
> >
> > Implicit default constructor.[/color]
>
> 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.
>[/color]
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! |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,449 network members.
|