Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Calling base class constructor from derived class Copy constructor

Reply
Thread Tools

Calling base class constructor from derived class Copy constructor

 
 
ali
Guest
Posts: n/a
 
      03-05-2007
Hi,

I am new to C++ and trying to understand how to work on Inheritance
and Operator overloading. I understand that the derived class can pass
the base class constructor in its constructor definition as following
code:

Car::Car(int id, int colorID, int type):Vehicle(id, colorID)
{
this->type = type;
}

What I am having difficulty with, is, how do I call the base class
constructor when writing the copy constructor for the derived class?

Example:

Car::Car(const Car &rhs)
{
//my code
}

Can I just add it as:

Car::Car(const Car &rhs):Vehicle(rhs.getID, rhs.getColor)
{
this->type = rhs.type;
}

I'm not sure of the above code copy constructor code accuracy, but
would appreciate some guidance.

Thank you!

Ali

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      03-05-2007
ali wrote:
> Hi,
>
> I am new to C++ and trying to understand how to work on Inheritance
> and Operator overloading. I understand that the derived class can pass
> the base class constructor in its constructor definition as following
> code:
>
> Car::Car(int id, int colorID, int type):Vehicle(id, colorID)
> {
> this->type = type;
> }
>

Better to use an initialisation list:

ar::Car(int id, int colorID, int type)
: Vehicle(id, colorID), type(type) {}

> What I am having difficulty with, is, how do I call the base class
> constructor when writing the copy constructor for the derived class?
>

You can't. If you have some initialisation code you want to share
between constructors, and an initialise method and call it as required.

--
Ian Collins.
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      03-05-2007
* ali:
> Hi,
>
> I am new to C++ and trying to understand how to work on Inheritance
> and Operator overloading. I understand that the derived class can pass
> the base class constructor in its constructor definition as following
> code:
>
> Car::Car(int id, int colorID, int type):Vehicle(id, colorID)
> {
> this->type = type;
> }
>
> What I am having difficulty with, is, how do I call the base class
> constructor when writing the copy constructor for the derived class?
>
> Example:
>
> Car::Car(const Car &rhs)
> {
> //my code
> }
>
> Can I just add it as:
>
> Car::Car(const Car &rhs):Vehicle(rhs.getID, rhs.getColor)
> {
> this->type = rhs.type;
> }
>
> I'm not sure of the above code copy constructor code accuracy, but
> would appreciate some guidance.


You can just add


Car::Car( Car const& other ): Vehicle( other )
{}

But that's what the compiler does for you if you don't declare a copy
constructor.

So you don't have to do anything.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      03-05-2007
* Alf P. Steinbach:
> * ali:
>> Hi,
>>
>> I am new to C++ and trying to understand how to work on Inheritance
>> and Operator overloading. I understand that the derived class can pass
>> the base class constructor in its constructor definition as following
>> code:
>>
>> Car::Car(int id, int colorID, int type):Vehicle(id, colorID)
>> {
>> this->type = type;
>> }
>>
>> What I am having difficulty with, is, how do I call the base class
>> constructor when writing the copy constructor for the derived class?
>>
>> Example:
>>
>> Car::Car(const Car &rhs)
>> {
>> //my code
>> }
>>
>> Can I just add it as:
>>
>> Car::Car(const Car &rhs):Vehicle(rhs.getID, rhs.getColor)
>> {
>> this->type = rhs.type;
>> }
>>
>> I'm not sure of the above code copy constructor code accuracy, but
>> would appreciate some guidance.

>
> You can just add
>
>
> Car::Car( Car const& other ): Vehicle( other )
> {}
>
> But that's what the compiler does for you if you don't declare a copy
> constructor.


Uh, more members in Car, yes, didn't see that, should be

Car::Car( Car const& other ): Vehicle( other ), type( other.type )
{}


>
> So you don't have to do anything.
>



--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
David Harmon
Guest
Posts: n/a
 
      03-05-2007
On 5 Mar 2007 00:26:02 -0800 in comp.lang.c++, "ali"
<> wrote,
>Can I just add it as:
>
>Car::Car(const Car &rhs):Vehicle(rhs.getID, rhs.getColor)
>{
> this->type = rhs.type;
>}


Preferable, if Vehicle has suitable copy constructor already

Car::Car(const Car &rhs)
: Vehicle(rhs),
type(rhs.type)
{ }



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Casting from base to derived class in base constructor pastbin@gmail.com C++ 2 02-07-2008 02:41 PM
call base class constructor from derived class constructor Rahul C++ 16 11-07-2007 03:40 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 01:44 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 12:07 AM
Invoking templatized base class constructor from templatized derived class constructor mrstephengross C++ 5 05-18-2005 07:12 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57