tags:

views:

103

answers:

4

I have a class as given below, I want to write a copy constructor for the same. I need to create a deep copy constructor for this. following code is printing x and c properly but value of y here is garbage.

#include "stdafx.h"
#include <string.h>
class MyClass
{
public:

 MyClass(int a) : y(a) {
 }
 MyClass(const MyClass &myClass) : y(myClass.y)
 {
  x = myClass.x;
  c = new char[10];
  strcpy(c, myClass.c);
 }

  int x;
  char *c;
  int &y;

};

int _tmain(int argc, _TCHAR* argv[])
{
 MyClass m1(0);
 m1.c = new char[10];
 strcpy(m1.c, "gourav");
 m1.x = 10;
 m1.y = m1.x;

 MyClass m2 = m1;

 printf("x=%d\nc=%s\ny=%d\n", m2.x, m2.c, m2.y);
 return 0;
}
+3  A: 

The problem is not copying, but the constructor: MyClass(int a) : y(a) {}. Here y is a reference to a TEMPORARY variable, which disappears..

asen
+3  A: 

In your code, y is reference.. You're creating MyClass m1(0), so m1.y points to a temporary variable - 0. You just must not do this.. I don't know why you y member is reference.. ?? Anyway, if you want this to be that way, do that:

//..
int a = 10;
MyClass m1(a);
//..

Anyway, this is ugly.. And dangerous, if you don't know what you're actually doing.. You should really have a very good reason to do that.

Also, redesign your class, and its members' names..

So, the problem is NOT in the copy-constructor at all..

Kiril Kirov
I created a local variable as you suggested and passed it to m1(a) that too didn't fixed the problem.Also in the copy constructor when I am putting copy value like this y = myClass.y;then I am getting following errors.1>d:\working\oops.cpp\oops.cpp\oops.cpp(9) : error C2758: 'MyClass::y' : must be initialized in constructor base/member initializer list1> d:\working\oops.cpp\oops.cpp\oops.cpp(19) : see declaration of 'MyClass::y'
GJ
OKay, what do you mean by that? What did you change, to see this message?
Kiril Kirov
GJ
it's not allowed.. Why don't you just remove the ref and leave the member just int y; ? Also, you cannot redirect reference. So, m1.y = 10; will change the object, pointed by y.
Kiril Kirov
GJ
It was indeed not an good idea to use y, the question of writing a copy constructor for an class with an int, pointer and reference was asked to me by an interviewer, may be he wanted to test my two skills References and Copy Constructor.After your suggestions I figured out the mistake I was doing was passing a temporary value to a function where a reference is going to receive it.
GJ
Kiril Kirov
That's why, making y to point x is safe, because x will live as long as your object lives. But it doesn't make any sense :)
Kiril Kirov
+3  A: 

In your first constructor taking an int, you are referencing a temporary local variable.

Flinsch
+4  A: 

I don't like your code.

The other answers pointed out why you are getting garbage value.

A better version of your code would look like this :

#include <iostream>
class MyClass
{

  int x;
  std::string c; //use std::string instead of char*
  int &y;

  public :

  MyClass (int a, std::string b): x(a), c(b),y(x) {}
  MyClass (const MyClass& myclass) : x (myclass.x), c(myclass.c), y(myclass.y) {}

  void print()
  {
     std::cout << x << " " << c << " " << y <<std::endl;
  }
};

int main()
{

    MyClass m1 (10, "Gaurav");
    MyClass m2 = m1;

    m1.print();
    m2.print();
}

Output :

10 Gaurav 10
10 Gaurav 10
Prasoon Saurav
this way, y just points x.. what do you mean by this code?
Kiril Kirov
What has to be meant? `y` gets initialized to `x` which has already been intialized to `myclass.x`.
Prasoon Saurav
this way, isn't this member y useless? I meant "what do you win, putting y as a member, referencing to other member". Anyway, I was just curious (:
Kiril Kirov
@Kiril : Nothin! IMHO the whole point of using `y` is useless in OP. Ok! modified the code. :)
Prasoon Saurav
Sorry man, I didn't mean anything bad, don't get angry, just asking. Sorry :/
Kiril Kirov
@Kiril : Its fine man. Relax! `:)`
Prasoon Saurav