Copy Constructor Qtn

Discussion in 'C++' started by sarathy, Aug 18, 2006.

  1. sarathy

    sarathy Guest

    Hi all,
    I have doubt regarding how objects are passed in C++. The
    primary problem of passing by value in C++, is that the destructor of
    the object passed will be called twice, thus creating possible damage
    to the object passed. If that is the case, why isnt the following
    program producing the unexpected o/p. I expect the program to print

    In B::printValues : 22 33

    instead of

    In B::printValues : 1 0

    Since i have overloaded the copy constructor, i have introduced the
    problem mentioned above ( 2 calls to destructor ). But why is the copy
    of A in printValues() not printing the actual values in the first call
    ? Where is the program going wrong?

    # include <iostream>

    using std::cout;
    using std::endl;

    class A
    {
    int x, y;

    public:
    ~A();
    A();
    A(int , int );
    int getX();
    int getY();
    A(A&);
    };

    /* Default constructor of A.*/
    A::A()
    {
    cout << "A:: Def constructor" << endl;
    }

    /* Overloaded constructor of A. */
    A::A(int a, int b)
    {
    x=a;
    y=b;
    cout << "A:: Prm constructor" << endl;
    }

    /* Overloaded copy constructor of A. */
    A::A(A &a)
    {
    cout << "In Copy Constructor : " << a.getX() << " " << a.getY() <<
    endl;
    }

    /* Destructor of A. */
    A::~A()
    {
    x=-1;
    y=-1;
    cout << "A:: Def destructor" << endl;
    }

    /* Getters. */
    int A::getX() { return x; }
    int A::getY() { return y; }


    class B
    {
    public:
    void printValues(A);
    };

    void B::printValues(A a)
    {
    cout << "In B::printValues : " << a.getX() << " " << a.getY() << endl;
    cout.flush();
    }

    int main()
    {
    A a(22,33);
    B b;

    b.printValues(a);
    b.printValues(a);
    return 0;
    }

    OUTPUT
    -------------

    A:: Prm constructor
    In Copy Constructor : 22 33
    In B::printValues : 1 0
    A:: Def destructor
    In Copy Constructor : 22 33
    In B::printValues : -1 -1
    A:: Def destructor
    A:: Def destructor
     
    sarathy, Aug 18, 2006
    #1
    1. Advertising

  2. sarathy

    Bo Persson Guest

    "sarathy" <> skrev i meddelandet
    news:...
    > Hi all,
    > I have doubt regarding how objects are passed in C++.
    > The
    > primary problem of passing by value in C++, is that the destructor
    > of
    > the object passed will be called twice, thus creating possible
    > damage
    > to the object passed.


    Why? If you make a copy, you will later have to destruct both the
    original and the copy.

    > If that is the case, why isnt the following
    > program producing the unexpected o/p. I expect the program to print
    >
    > In B::printValues : 22 33
    >
    > instead of
    >
    > In B::printValues : 1 0
    >
    > Since i have overloaded the copy constructor, i have introduced the
    > problem mentioned above ( 2 calls to destructor ). But why is the
    > copy
    > of A in printValues() not printing the actual values in the first
    > call
    > ? Where is the program going wrong?


    You have a copy constructor that prints values, but it doesn't copy
    them.

    >
    > /* Overloaded copy constructor of A. */
    > A::A(A &a)
    > {
    > cout << "In Copy Constructor : " << a.getX() << " " << a.getY() <<
    > endl;
    > }
    >


    >
    > OUTPUT
    > -------------
    >
    > A:: Prm constructor
    > In Copy Constructor : 22 33
    > In B::printValues : 1 0
    > A:: Def destructor
    > In Copy Constructor : 22 33
    > In B::printValues : -1 -1
    > A:: Def destructor
    > A:: Def destructor


    I see one construction of an A, and two copies. That matches the three
    destructors.


    Bo Persson
     
    Bo Persson, Aug 18, 2006
    #2
    1. Advertising

  3. sarathy schrieb:
    > Hi all,
    > I have doubt regarding how objects are passed in C++. The
    > primary problem of passing by value in C++, is that the destructor of
    > the object passed will be called twice, thus creating possible damage
    > to the object passed.


    It is not the same object that is destructed twice. The original is
    desctructed once, and every copy is destructed once.

    > If that is the case, why isnt the following
    > program producing the unexpected o/p. I expect the program to print
    >
    > In B::printValues : 22 33
    >
    > instead of
    >
    > In B::printValues : 1 0

    [...]

    See below.

    > # include <iostream>
    >
    > using std::cout;
    > using std::endl;
    >
    > class A
    > {
    > int x, y;
    >
    > public:
    > ~A();
    > A();
    > A(int , int );
    > int getX();
    > int getY();


    int getX() const;
    int getY() const;

    > A(A&);
    > };

    [...]
    > /* Overloaded copy constructor of A. */
    > A::A(A &a)


    Should better be:

    A::A(const A& a)

    > {
    > cout << "In Copy Constructor : " << a.getX() << " " << a.getY() <<
    > endl;
    > }


    Your copy constructor doesn't copy construct your object. You have to
    initialize x and y of the constructed object with some values.

    --
    Thomas
     
    Thomas J. Gritzan, Aug 18, 2006
    #3
  4. sarathy

    Rolf Magnus Guest

    sarathy wrote:

    > Hi all,
    > I have doubt regarding how objects are passed in C++. The
    > primary problem of passing by value in C++, is that the destructor of
    > the object passed will be called twice, thus creating possible damage
    > to the object passed.


    How did you come to that conclusion? It's not true.

    > If that is the case, why isnt the following program producing the
    > unexpected o/p. I expect the program to print
    >
    > In B::printValues : 22 33
    >
    > instead of
    >
    > In B::printValues : 1 0


    If you wanted the values copied over to the new object, you'd actually have
    to copy them in your copy constructor.

    >
    > Since i have overloaded the copy constructor, i have introduced the
    > problem mentioned above ( 2 calls to destructor ).


    No, you haven't. When you pass an object by value, it gets copied. Storage
    is acquired for that copy, then your constructor is called to initialize
    it. Your constructor just happens to keep it uninitialized.

    > But why is the copy of A in printValues() not printing the actual values
    > in the first call ?


    The actual values are undefined.

    > Where is the program going wrong?


    Where your copy constructor happens to keep the "copy" uninitialized.
     
    Rolf Magnus, Aug 18, 2006
    #4
    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. VisionSet
    Replies:
    8
    Views:
    4,929
    Tris Orendorff
    Apr 29, 2004
  2. Aire
    Replies:
    3
    Views:
    475
    Mike Wahler
    Jan 25, 2004
  3. ali
    Replies:
    4
    Views:
    590
    David Harmon
    Mar 5, 2007
  4. Generic Usenet Account
    Replies:
    10
    Views:
    2,289
  5. cinsk
    Replies:
    35
    Views:
    2,656
    James Kanze
    Oct 11, 2010
Loading...

Share This Page