copy constructor of parent clas

Discussion in 'C++' started by Kench, Jun 2, 2004.

  1. Kench

    Kench Guest

    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;
    }
     
    Kench, Jun 2, 2004
    #1
    1. Advertising

  2. 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
     
    Victor Bazarov, Jun 2, 2004
    #2
    1. Advertising

  3. 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
     
    Mark A. Gibbs, Jun 2, 2004
    #3
  4. 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
     
    Mark A. Gibbs, Jun 2, 2004
    #4
  5. Kench

    Kench Guest

    "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!
     
    Kench, Jun 2, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pantalaimon
    Replies:
    3
    Views:
    51,107
    John Harrison
    Oct 9, 2004
  2. Replies:
    6
    Views:
    515
  3. Replies:
    4
    Views:
    517
    James Kanze
    Oct 23, 2007
  4. Generic Usenet Account
    Replies:
    10
    Views:
    2,282
  5. cinsk
    Replies:
    35
    Views:
    2,652
    James Kanze
    Oct 11, 2010
Loading...

Share This Page