Help with copy constructors

This is a discussion on Help with copy constructors within the C++ Programming forums, part of the General Programming Boards category; can anyone help me with copy constructors. i am writing a program with a class that stores an array of ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    3

    Help with copy constructors

    can anyone help me with copy constructors. i am writing a program with a class that stores an array of integers and im not sure how to write the copy constructor so that it copies the array into the new class.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    1) It takes a const reference parameter of type className.
    2) It has no return value
    3) It's called when you pass an object by value to a function, or return an object by value from a function, or create an object with another object.

    Inside the copy constructor, you would copy the elements from the other objects array:
    Code:
    for(int i = 0; i<12; i++)
    { 
          memberarray[i] = otherObject.memberarray[i];
    }

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    3
    i tried implementing the code you suggested but i get an error message saying that my original object.array is undeclarred

    could that be because i didnt put new array[0] in the constructor?
    Last edited by nessie; 04-24-2005 at 07:19 AM.

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    3
    i got that code to work using

    Code:
    int i=0;
    	while(i<size){ 
          b.array[i] = array[i];
          cout<<b.array[i]<<endl;
          ++i;
    	}
    but it is now generating random numbers to the new array

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    You've got this backwards:

    b.array[i] = array[i];

    array[i] is the new object's member(that is why it is not preceded by an object name).
    b.array[i] is the old object's member. b is the object that is the function argument. You want to assign old to new. But, you are assigning new to old, and new is filled with junk values, so you are overwriting b's values with junk values. In fact, that shouldn't even compile if you followed step 1, which said the parameter should be a const reference type. When you put const in front of a parameter, it ensures that the function cannot change the object. Whenever a function shouldn't change the argument you send to the function, you should put const in front of the parameter name in the function definition. That way the compiler can alert you to any errors you are making--just like here.
    Last edited by 7stud; 04-24-2005 at 08:21 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copy constructors; Best practices (and private)
    By Mario F. in forum C++ Programming
    Replies: 15
    Last Post: 06-23-2006, 04:42 PM
  2. 'Passing by Refrence for Efficiency', Copy Constructors?
    By Zeusbwr in forum C++ Programming
    Replies: 4
    Last Post: 10-23-2004, 07:11 AM
  3. Copy constructors and operator=()
    By filler_bunny in forum C++ Programming
    Replies: 13
    Last Post: 08-25-2003, 07:43 AM
  4. Copy constructors and private constructors
    By Eibro in forum C++ Programming
    Replies: 5
    Last Post: 11-24-2002, 09:16 AM
  5. Copy constructors........
    By incognito in forum C++ Programming
    Replies: 1
    Last Post: 05-02-2002, 08:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21