Question:
C++ Constructors & copy constructors?
Neil D
2007-11-22 03:32:45 UTC
I am trying to do a C++ assignment with little success it seems right now.
I want to know about constructors and especially copy constructors in plain english as all the books I have talk in techno babble.

I need to use copy constructors in my assignment but have no idea how or how to even start, I have 3 weeks in which to get this done and panic is beginnign to set in
Thanks
Three answers:
spoonmeiser
2007-11-22 12:41:38 UTC
A copy constructor is always implicitly created when you create a class. So usually, you don't need to do anything to have a copy constructor.



The default copy constructor just copies the values of all the members of an object to a new object. You will need to define the copy constructor yourself if, say, one of your members is a pointer (the default copy constructor will copy the pointer and both objects will end up pointing to the same data, which is probably not what you want) or you are doing some sort of reference counting.



Defining a copy constructor is just like defining a normal constructor, except that it takes a reference to the same class as it's only required argument.



So, you might have:



class X

{

  public:

    X(int, int); // normal constructor

    X(X&); // copy constructor

  ...

};



Usually, you'd define it with a const reference.



X::X(X &other)

{

  ...

}
DogmaBites
2007-11-22 13:22:43 UTC
First, the regular constructor is used when you create an object yourself. For example:



class MyClass {

MyClass (int v1, int v2);

...

};



MyClass mc(1,2)



In this example you explicitly create the variable mc.



The copy constructor is used when you want to make a copy of another object. This is commonly used when passing parameters to functions. When you declare a function such as:

int func(MyClass m);

The function recieves a copy of the parameter. This copy is constructed using the copy constructor. You can also use the copy constructor yourself if you decide to create a object copied from another.



class MyClass {

MyClass(int v1, v2); // regular constructor

MyClass(MyClass const &other); // copy constructor

...

};



MyClass::MyClass(MyClass const &other)

{

//copy values from other

v1 = other.v1;

// etc.

}



void f1(MyClass m); // This func takes a copy of MyClass



MyClass mc(1,2); // Create MyClass object with constructor

MyClass mc2(mc1) // mc2 is a copy of mc



f1(mc); // WHen f1 is called, it receives a copy of mc.



f1 can do whatever it wants with it's copy of mc. With it's own copy, whatever it does does not affect the original.
ishan j
2007-11-22 15:12:15 UTC
#include

class xyz

{

int x,y;

public:

xyz(xyz &temp)

{

x=temp.x;

y=temp.y;

}

void xyz::get()

{

cout<<"enter x";

cin>>x;

cout<<"enter y";

cin>>y;

}

void xyz::show()

{

cout<<"xyz="<
int main()

{

xyz a;

a.get();

a.show();

xyz x1(a);

//or we can call

xyz x1=a;

x1.show();

return(0);

here copy constructor x1copy the value of a;


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...