Question: How to copy entire objects?

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 3014 Views - Last Post: 29 July 2012 - 07:13 PM Rate Topic: -----

#1 Sushii  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 11-December 08

Question: How to copy entire objects?

Posted 27 July 2012 - 01:27 PM

Hi again, just another quick question.

How do i copy entire objects using a copy constructor?

Here is my code right now.

Equal operator:
disk disk::operator=(const disk &copy) {
	disk scopy(copy);
}


Copy constructor:
disk::disk(const disk &from) {
	strcpy(mmode, from.mmode);
	size_max = from.size_max;
	num = from.num;
	s = new segment[size_max];
	
	//strcpy(s, from.s);
}



The copy constructor so far copies all the values of the variables, but how do i copy what is in the object? i tried using strcpy but it doesn't seem to work.

Thanks in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: Question: How to copy entire objects?

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4077
  • View blog
  • Posts: 12,589
  • Joined: 25-December 09

Re: Question: How to copy entire objects?

Posted 27 July 2012 - 02:09 PM

Quote

The copy constructor so far copies all the values of the variables, but how do i copy what is in the object?

Please explain what you mean by the above. If you copy all the variables you should be finished.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 Sushii  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 11-December 08

Re: Question: How to copy entire objects?

Posted 27 July 2012 - 02:16 PM

View Postjimblumberg, on 27 July 2012 - 03:09 PM, said:

Quote

The copy constructor so far copies all the values of the variables, but how do i copy what is in the object?

Please explain what you mean by the above. If you copy all the variables you should be finished.

Jim


What i meant was, how do i copy the entire instance/object instead of just the variables?

e.g.

If function get_num(); returned a 5 in the original, how do i also copy that return value into a new instance/object?
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 4077
  • View blog
  • Posts: 12,589
  • Joined: 25-December 09

Re: Question: How to copy entire objects?

Posted 27 July 2012 - 02:21 PM

Show a complete program that illustrates your problems/questions. Then ask specific questions based on the code you supplied. At very minimum show the complete class definition and implementation and how you are trying to use your copy constructor.

Jim
Was This Post Helpful? 0
  • +
  • -

#5 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1348
  • View blog
  • Posts: 4,645
  • Joined: 19-February 09

Re: Question: How to copy entire objects?

Posted 27 July 2012 - 03:37 PM

View PostSushii, on 27 July 2012 - 11:27 PM, said:

The copy constructor so far copies all the values of the variables, but how do i copy what is in the object? i tried using strcpy but it doesn't seem to work.


To copy an array you could use a for loop to copy each element. There is also a copy function in STL algorithms.

Copy constructor
Was This Post Helpful? 0
  • +
  • -

#6 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2117
  • View blog
  • Posts: 3,242
  • Joined: 21-June 11

Re: Question: How to copy entire objects?

Posted 28 July 2012 - 08:36 AM

View PostSushii, on 27 July 2012 - 10:27 PM, said:

disk disk::operator=(const disk &copy) {
	disk scopy(copy);
}


That is not a correct definition of operator=.

First of all it is generally expected that operator= modifies the object it is used on (to the point that I'd consider any implementation that doesn't do that erroneous). You never modify the current object - you just create a new object and never do anything with it at all.

Second of all, operator= is generally expected to return a reference, you've declared it to return by value.

Third of all, you've declared operator= to return a value, but you never actually return anything from the method. So the behavior is undefined.
Was This Post Helpful? 0
  • +
  • -

#7 Sushii  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 11-December 08

Re: Question: How to copy entire objects?

Posted 28 July 2012 - 02:31 PM

View Postsepp2k, on 28 July 2012 - 09:36 AM, said:

View PostSushii, on 27 July 2012 - 10:27 PM, said:

disk disk::operator=(const disk &copy) {
	disk scopy(copy);
}


That is not a correct definition of operator=.

First of all it is generally expected that operator= modifies the object it is used on (to the point that I'd consider any implementation that doesn't do that erroneous). You never modify the current object - you just create a new object and never do anything with it at all.

Second of all, operator= is generally expected to return a reference, you've declared it to return by value.

Third of all, you've declared operator= to return a value, but you never actually return anything from the method. So the behavior is undefined.


The copy constructor basically makes a copy of the existing object, return by pointer or reference will not give the copy a new address which becomes a problem when the destructor is called. Or am i missing something?

Edit: i am returning scopy, i must've forgot to add that.

disk disk::operator=(const disk &copy) {
	disk scopy(copy);
	return scopy;
}

This post has been edited by Sushii: 28 July 2012 - 02:32 PM

Was This Post Helpful? 0
  • +
  • -

#8 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2117
  • View blog
  • Posts: 3,242
  • Joined: 21-June 11

Re: Question: How to copy entire objects?

Posted 28 July 2012 - 02:51 PM

View PostSushii, on 28 July 2012 - 11:31 PM, said:

The copy constructor basically makes a copy of the existing object, return by pointer or reference will not give the copy a new address which becomes a problem when the destructor is called. Or am i missing something?


That's not how it works. You're creating a local variable called scopy. That local variable has its own address and is completely distinct from the current object. The copy constructor copies the given object into that variable. When the method returns, scopy goes out of scope, so its address is no longer valid. Since you're returning by value, the returned value is another copy of that variable (if you'd return by reference, the return value would be an invalid reference).

Nowhere are you ever modifying the current object.

The fact that you return scopy does not magically change the current object. The return value of operator= is only relevant if you use the return value of the assignment somewhere - for example if you chain assignments or do something like while(foo = bar). It does not in any way affect which effect the assignment has. If your operator= doesn't do anything except returning a value, assigning a variable of type disk won't do anything except returning a value (i.e. it won't actually change the variable).
Was This Post Helpful? 0
  • +
  • -

#9 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2993
  • View blog
  • Posts: 10,346
  • Joined: 08-August 08

Re: Question: How to copy entire objects?

Posted 28 July 2012 - 07:53 PM

Are you sure the default copy constructor isn't enough? Look at the examples below. In the first one, a copy constructor is needed to double the values, but in the second, the default copy constructor does the job.
#include <iostream>

using namespace std;

struct example {
  int a, b;
  double c;
  
  example (int n1, int n2, double n3) {
    a = n1;
    b = n2;
    c = n3;
  }
  
  example(const example &ex){
    a = ex.a*2;
    b = ex.b*2;
    c = ex.c*2;
  }

  void showexample() {
    cout << a << " \t" << b << " \t" << c << endl;
  }
  
};

struct example_too {
  int A, B;
  double C;
  
  example_too(int n1, int n2, double n3) {
    A = n1;
    B = n2;
    C = n3;
  }
  
  void showexample() {
    cout << A << " \t" << B << " \t" << C << endl;
  }
  
};

int main (int argc, char * const argv[]) {
  example one(10,20,35.25);
  one.showexample();
  
  example two = one;
  two.showexample();
  
  example_too three(10,20,35.25);
  three.showexample();
  
  example_too four = three;
  four.showexample();
	
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3579
  • View blog
  • Posts: 11,130
  • Joined: 05-May 12

Re: Question: How to copy entire objects?

Posted 28 July 2012 - 07:56 PM

The default copy constructor does a shallow copy. If you have pointers in your class/struct, then the default copy constructor will just copy the pointer over. It will not allocate new space and copy over the data from one array to another.

#include <iostream>

using namespace std;

class Kaboom
{
public:
    Kaboom()
    {
        click = new int[10];
        for(int i = 0; i < 10; ++i)
            click[i] = 0;
    }

    ~Kaboom()
    {
        delete [] click;
    }

    int& operator [](int index)
    {
        return click[index];
    }

private:
    int * click;
};

int main()
{
    Kaboom k1;
    Kaboom k2(k1);

    cout << k1[2] << endl;
    cout << k2[2] << endl;
    k1[2] = 3;
    cout << k1[2] << endl;
    cout << k2[2] << endl;
    return 0;
}


This post has been edited by Skydiver: 28 July 2012 - 08:03 PM

Was This Post Helpful? 0
  • +
  • -

#11 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2993
  • View blog
  • Posts: 10,346
  • Joined: 08-August 08

Re: Question: How to copy entire objects?

Posted 28 July 2012 - 08:01 PM

Right, but I've seen no evidence that the object uses pointers or that the copy constructor needs to modify the copy.
Was This Post Helpful? 0
  • +
  • -

#12 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3579
  • View blog
  • Posts: 11,130
  • Joined: 05-May 12

Re: Question: How to copy entire objects?

Posted 28 July 2012 - 08:30 PM

Yeah, it's not obvious if you don't have enough context.

See line 5 of the second chunk of code in the original post on this thread, as well as the other thread that spawned this thread.
Was This Post Helpful? 1
  • +
  • -

#13 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2117
  • View blog
  • Posts: 3,242
  • Joined: 21-June 11

Re: Question: How to copy entire objects?

Posted 28 July 2012 - 11:13 PM

View PostSkydiver, on 29 July 2012 - 04:56 AM, said:

The default copy constructor does a shallow copy. If you have pointers in your class/struct, then the default copy constructor will just copy the pointer over. It will not allocate new space and copy over the data from one array to another.


To clarify that a bit: The default copy constructor will call the copy constructors of all member variables (and presumably those will call the copy constructors of their members), so assuming that all members' copy constructors create a deep copy (which most C++ class's copy constructors do), the default copy constructor will also create a deep copy.

That just doesn't help you if some of your members are pointers. Of course the most easy way to work around that is to not use pointers, for members you want to be copied by the copy constructor.

In this case I assume there is a C-string involved because the copy constructor calls strcpy. By simply replacing that C string with a std::string, you'll make sure that a proper copy of that string will be created without having to define your own copy constructor (or operator=).
Was This Post Helpful? 2
  • +
  • -

#14 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2993
  • View blog
  • Posts: 10,346
  • Joined: 08-August 08

Re: Question: How to copy entire objects?

Posted 29 July 2012 - 04:12 AM

View Postsepp2k, on 29 July 2012 - 02:13 AM, said:

In this case I assume there is a C-string involved because the copy constructor calls strcpy. By simply replacing that C string with a std::string, you'll make sure that a proper copy of that string will be created without having to define your own copy constructor (or operator=).

And since there's already a pass by reference we can assume this is C++ so it shouldn't be a problem to use std::string or any other C++ version of anything that requires pointers in C, such as a vector in place of a dynamic array if one is being used.
Was This Post Helpful? 0
  • +
  • -

#15 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2117
  • View blog
  • Posts: 3,242
  • Joined: 21-June 11

Re: Question: How to copy entire objects?

Posted 29 July 2012 - 04:29 AM

View PostCTphpnwb, on 29 July 2012 - 01:12 PM, said:

And since there's already a pass by reference we can assume this is C++


That and the fact that the question is about copy constructors and operator=.

This post has been edited by sepp2k: 29 July 2012 - 04:29 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2