eveo
eveo

Reputation: 2833

Copy constructors in C++ and copying values into objects

#include <iostream>
#include <cstring>
using namespace std;

class foo {
     int number;
     char string[20];
public:
     foo( ) { 
          set(3, 5); 
     }
     void set (int a, int b) { 
          number = a + b;
        strcpy( string, "summer" ); 
     }

     foo (const foo & c ) { 
     }

     void output() { 
          cout << number << ',' << string << endl;  
     }
};

void showyanothing (foo);

int main() {
     foo a, b;
     showyanothing( a );
     a.output();
     b.output();
}

void showyanothing (foo z) { 
     cout << "... how are you?\n";
     z.output(); 
}

Output

... hello ...
... how are you?
-1218897013,l·ôl·Y·ôl·À
8,summer
8,summer

The line with -1218897013,l·ôl·Y·ôl·À, those are the values in the object Z created by the copy constructor. If I change my copy constructor to the following, outputting object z will yield 10, summer.1.2.3. Why? Where did the values magically get inserted from into Z?

new copy constructor

 foo (const foo & c ) { 
     number = c.number + 2;
     strcpy( string, c.string );
     strcat( string, ".1.2.3" );
 }

new output:

... how are you?
10,summer.1.2.3
8,summer
8,summer

In the copy constructor, what I'm confused about is why does c.number equate to 8, and c.string equate to summer? Where did it get those values from?

Upvotes: 1

Views: 129

Answers (3)

n0rd
n0rd

Reputation: 12630

Object occupy memory whether you initialize it or not. Memory contains some arbitrary value. If you don't initialize it, it can be anything. That explains the initial garbage output you get.

Next you provide sensible copy constructor, which initializes object based on source object which is passed to it. In the beginning of your main() you create object a which is constructed using default constructor, and hence gets its number member set to 8 as a result of set(3,5) call. showyanothing (foo z) function gets its parameter passed by value, which means creating a new instance of class foo using copy constructor which receives object a, it adds 2 to a's number (which is 8) and copies the string.

Upvotes: 0

Nate Hekman
Nate Hekman

Reputation: 6657

The first version of your copy constructor does not initialize foo's member variables. Therefore number and string will contain random data--whatever happened to be in RAM at the location where the new object gets instantiated.

The second version correctly assigns values to the member variables, based on the object getting passed in by reference (the object we're supposed "copying"). In this case you are passing in a (because you call showyanothing(a)) which was instantiated using the default constructor, which gave it the number 8 and the string "summer".

Upvotes: 0

NirmalGeo
NirmalGeo

Reputation: 771

Simple check out the function call

void showyanothing (foo z) 

The copy constructor is invoked here. Since you have a copy constructor in the earlier code and with no initialization of the data members, it prints random val. On the other hand, when you edit the copy constructor you get the result ( i.e. the following is invoked!)

foo (const foo & c ) { 
     number = c.number + 2;
     strcpy( string, c.string );
     strcat( string, ".1.2.3" );
 }

Hope it solves your doubt :)

Upvotes: 4

Related Questions