Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > copy constructor of parent clas

Reply
Thread Tools

copy constructor of parent clas

 
 
Kench
Guest
Posts: n/a
 
      06-02-2004
Sorry if this becomes a repost. I posted this to comp.lang.c++.moderated 1
hour ago still it does not show up there so posting this here.

Hi,
Consider class A & B both of which implement a copy constructor.
class B inherits from A.
When copy constructor of B is called, first the constructor of A gets called
My question is that, why the copy constructor for A not called.
Is there a way to have it call the copy constructor of class A?

#include <iostream>
using namespace std;


class A
{
public:
A() { cout << "A constructor called\n";};
A(A& a) { cout << "Copy A constructor called\n";};
~A() { cout << "Destructor A called\n";};
};
class B: public A
{
public:
B() { cout << "B constructor called\n";};
B(B& b) { cout << "Copy B constructor called\n"; };
~B() { cout << "Destructor B called\n";};
};
int main(void)
{
B b;
B bb = B(b);
return 0;
}



 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      06-02-2004
Kench wrote:
> Sorry if this becomes a repost. I posted this to comp.lang.c++.moderated 1
> hour ago still it does not show up there so posting this here.


It takes moderators up to a day to get through all posts in a moderated
newsgroup, usually. You should not expect a chat room performance from
the Usenet.

> Consider class A & B both of which implement a copy constructor.
> class B inherits from A.
> When copy constructor of B is called, first the constructor of A gets called


Not unless you specifically omit initialising the base class object.

> My question is that, why the copy constructor for A not called.


You didn't initialise the base class. It calls the default c-tor.

> Is there a way to have it call the copy constructor of class A?


Of course. See below.

>
> #include <iostream>
> using namespace std;
>
>
> class A
> {
> public:
> A() { cout << "A constructor called\n";};


First of all, lose all the semicolons after function bodies. It
just looks so amateurish and sloppy...

> A(A& a) { cout << "Copy A constructor called\n";};
> ~A() { cout << "Destructor A called\n";};
> };
> class B: public A
> {
> public:
> B() { cout << "B constructor called\n";};
> B(B& b) { cout << "Copy B constructor called\n"; };


You need to say

B(B& b) : A(b) { ...

to make sure that the 'A' part is copy-constructed.

> ~B() { cout << "Destructor B called\n";};
> };
> int main(void)
> {
> B b;
> B bb = B(b);
> return 0;
> }


Victor
 
Reply With Quote
 
 
 
 
Mark A. Gibbs
Guest
Posts: n/a
 
      06-02-2004

Kench wrote:

> Consider class A & B both of which implement a copy constructor.
> class B inherits from A.
> When copy constructor of B is called, first the constructor of A gets called
> My question is that, why the copy constructor for A not called.
> Is there a way to have it call the copy constructor of class A?
>
> #include <iostream>
> using namespace std;
>
>
> class A
> {
> public:
> A() { cout << "A constructor called\n";};
> A(A& a) { cout << "Copy A constructor called\n";};
> ~A() { cout << "Destructor A called\n";};
> };
> class B: public A
> {
> public:
> B() { cout << "B constructor called\n";};
> B(B& b) { cout << "Copy B constructor called\n"; };


B(B& b) : A(b) { cout << "Copy B constructor called\n"; };

> ~B() { cout << "Destructor B called\n";};
> };
> int main(void)
> {
> B b;
> B bb = B(b);
> return 0;
> }


mark

 
Reply With Quote
 
Mark A. Gibbs
Guest
Posts: n/a
 
      06-02-2004

Mark A. Gibbs wrote:

>> int main(void)
>> {
>> B b;
>> B bb = B(b);
>> return 0;
>> }


Incidently, the "B bb = B(b)" is a little ugly (and tough to say :/). Go
with "B bb = b", or better yet "B bb(b)".

mark

 
Reply With Quote
 
Kench
Guest
Posts: n/a
 
      06-02-2004

"Victor Bazarov" <> wrote in message
news:h6qvc.889$...
> Kench wrote:
> > Sorry if this becomes a repost. I posted this to comp.lang.c++.moderated

1
> > hour ago still it does not show up there so posting this here.

>
> It takes moderators up to a day to get through all posts in a moderated
> newsgroup, usually. You should not expect a chat room performance from
> the Usenet.
>
> > Consider class A & B both of which implement a copy constructor.
> > class B inherits from A.
> > When copy constructor of B is called, first the constructor of A gets

called
>
> Not unless you specifically omit initialising the base class object.
>
> > My question is that, why the copy constructor for A not called.

>
> You didn't initialise the base class. It calls the default c-tor.
>
> > Is there a way to have it call the copy constructor of class A?

>
> Of course. See below.
>
> >
> > #include <iostream>
> > using namespace std;
> >
> >
> > class A
> > {
> > public:
> > A() { cout << "A constructor called\n";};

>
> First of all, lose all the semicolons after function bodies. It
> just looks so amateurish and sloppy...
>
> > A(A& a) { cout << "Copy A constructor called\n";};
> > ~A() { cout << "Destructor A called\n";};
> > };
> > class B: public A
> > {
> > public:
> > B() { cout << "B constructor called\n";};
> > B(B& b) { cout << "Copy B constructor called\n"; };

>
> You need to say
>
> B(B& b) : A(b) { ...
>
> to make sure that the 'A' part is copy-constructed.
>
> > ~B() { cout << "Destructor B called\n";};
> > };
> > int main(void)
> > {
> > B b;
> > B bb = B(b);
> > return 0;
> > }

>
> Victor


that one worked..thanks!


 
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
template copy constructor vs normal copy constructor cinsk C++ 35 10-10-2010 11:14 PM
If a class Child inherits from Parent, how to implementChild.some_method if Parent.some_method() returns Parent instance ? metal Python 8 10-30-2009 10:31 AM
Static Method in base class accessing protected constructor of derived clas softwaredoug@gmail.com C++ 4 10-23-2007 02:32 PM
Constructor question, how does the call to the parent constructor work? marcwentink@hotmail.com C++ 6 05-09-2006 07:19 AM
Calling parent constructor from child constructor pantalaimon C++ 3 10-09-2004 09:17 AM



Advertisments