0

I am relative new to C++ and study copy-constructors. In this simple example I wanted to examine if really the copy-constructor that is explicitly defined is active. I put a cout-string there and I could not see that it's printed.

question: I wonder, why is the copy-constructor not used? Since in the main function body a copy of an object is made.

Person timmy_clone = timmy;

heres is the full code:

#include <iostream>

class Person {
public:
   int age;

   Person(int a) {
      this->age = a;
   }

   Person(const Person& person) {
      std::cout << "hello\n";
   }
};

int main() {
   Person timmy(10);
   Person sally(15);

   Person timmy_clone = timmy;
   std::cout << "timmy age " << timmy.age << " " << "sally age " << sally.age << " " <<   "timmy_clone age " << timmy_clone.age << std::endl;
   timmy.age = 23;
   std::cout << "timmy age " << timmy.age << " " << "sally age " << sally.age << " " << "timmy_clone age " << timmy_clone.age << std::endl;

   std::cout << &timmy << std::endl;
   std::cout << &timmy_clone << std::endl;

}

edit: I use MinGW and compile with -o

g++ main.cpp -o main.exe

edit2: here is another codesnippet where the explicitly defined copy-constructor is used. Still wonder why its used here and not in the first example?

   #include <iostream>

 class Array {
 public:
   int size;
   int* data;

  Array(int sz)
    : size(sz), data(new int[size]) {
  }
  Array(const Array& other)
     : size(other.size), data(other.data) {std::cout <<"hello\n";}

~Array()
{
    delete[] this->data;
}
 };


int main()
{
   Array first(20);
   first.data[0] = 25;

  {
    Array copy = first;
    std::cout << first.data[0] << " " << copy.data[0] << std::endl;
  }    // (1)

   first.data[0] = 10;    // (2)

  std::cout << "first data[0]: " << first.data[0];
}
user2991252
  • 760
  • 2
  • 11
  • 28
  • What are your compiler flags? The copy might be optimized away – lethal-guitar Apr 30 '14 at 07:20
  • this code is fine, I've tried it and it prints hello. – Shikamu Apr 30 '14 at 07:22
  • works fine with gcc 4.8.1 – Kiroxas Apr 30 '14 at 07:25
  • It does indeed work fine. See here: https://eval.in/144220 – lethal-guitar Apr 30 '14 at 07:26
  • What about the operator= in your class? – Яois Apr 30 '14 at 07:26
  • Where is your question? – RamblingMad Apr 30 '14 at 07:29
  • @KeillRandor overloading `operator=` in this case will do nothing different (probably) as when you construct a new object using the `object x = value` syntax it will just alias the assignment to the appropriate constructor unless one isn't found – RamblingMad Apr 30 '14 at 07:31
  • @CoffeeandCode - have you really read the text??? ..... " I wonder why since in the main-code a copy of an object is made " – user2991252 Apr 30 '14 at 07:32
  • @user2991252 well, can you state which compiler you're using and at what optimization level? – lethal-guitar Apr 30 '14 at 07:33
  • @user2991252 I was actually referring to the fact that that sentence isn't a question at all, just an observation. – RamblingMad Apr 30 '14 at 07:33
  • @CoffeeandCode - I made an update :-) – user2991252 Apr 30 '14 at 07:42
  • possible duplicate of [What are copy elision and return value optimization?](http://stackoverflow.com/questions/12953127/what-are-copy-elision-and-return-value-optimization) – juanchopanza Apr 30 '14 at 07:44
  • @juanchopanza: What part of this code relates to copy elision? The line in question is certainly not eligible. – Benjamin Lindley Apr 30 '14 at 07:50
  • @BenjaminLindley You're right. I misread the question. Close vote retracted. – juanchopanza Apr 30 '14 at 07:53
  • 3
    What output do you get, and what output do you expect? – Benjamin Lindley Apr 30 '14 at 07:57
  • 1
    @KeillRandor There is no assignment in his example, so overriding the assignment operator will have no effect. (Of course, for any real use, he should either override it or ban it.) – James Kanze Apr 30 '14 at 08:27
  • @CoffeeandCode There is no assignment to be aliased in the `Type x = y;` syntax. The `=` sign here is _not_ an assignment operator, but part of the initialization syntax. – James Kanze Apr 30 '14 at 08:28
  • @JamesKanze `=` is logically defined as assignment. I'm saying that it won't be treated as assignment in this example so no need to overload the assignment operator. – RamblingMad Apr 30 '14 at 08:42
  • @CoffeeandCode We're talking about C++ code, not pure mathematics (where `=` is often defined as comparison for equality). In C++, `=` is a token, whose meaning depends on context. In the given context, it is _not_ assignment, and has nothing to do with assignment. – James Kanze Apr 30 '14 at 09:00
  • @JamesKanze I'm talking about C++, not pure mathematics, where `=` is assignment and `==` is comparison. You're just feeding back exactly the point I'm trying to get across though; My point is that even though it would logically be defined as assignment, it isn't in the given case. – RamblingMad Apr 30 '14 at 09:04
  • @CoffeeandCode Except that in C++, `=` is _not_ always assignment. That's the whole point of what I am saying. – James Kanze Apr 30 '14 at 09:42
  • @JamesKanze are you reading my comments at all? – RamblingMad Apr 30 '14 at 09:48

1 Answers1

2

You code is working as expected. See here.

Maybe you have confused with copy-elision which is not applicable here.

masoud
  • 55,379
  • 16
  • 141
  • 208