top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration

Facebook Login
Site Registration

Can a copy constructor accept an object of the same class as parameter, instead of reference of the object?

+1 vote
356 views
Can a copy constructor accept an object of the same class as parameter, instead of reference of the object?
posted Dec 6, 2014 by Alwaz

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote

No.: a copy constructor accept an object of the same class as parameter, instead of reference of the object

It is specified in the definition of the copy constructor itself. It should generate an error if a programmer specifies a copy constructor with a first argument that is an object and not a reference.

answer Dec 10, 2014 by Mohammed Hussain
0 votes

in copy constructor you have to pass the object of same class...((mandatory));

please check the example of copy constructor.

class sample
{
  int a;

  public:
    sample()
    {
        a = 10;
    }

    sample(&obj)
    {
        a = obj.a;
    }

    void show()
    {
        cout<<a;
    }
};

main()
{
  sample o1,o2;
  o1.show();
  o2.show();
}

in both it will print 10 on output screen;

answer Dec 8, 2014 by Chirag Gangdev
Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...