0

See the code in http://cpp.sh/8bsdl:

// copy constructor: deep copy
#include <iostream>
#include <string>
using namespace std;

class Example5 {
    string* ptr;
  public:
    Example5 (const string& str) : ptr(new string(str)) {cout << "constructor" << '\n';}
    ~Example5 () {delete ptr;}
    // copy constructor:
    Example5 (const Example5& x) : ptr(new string(x.content())) {cout << "copy constructor" << '\n';}
    // access content:
    const string& content() const {return *ptr;}
};

int main () {
    // cout << "copy constructor" << '\n';
  Example5 foo ("Example1");
  Example5 baz ("Example2");
  Example5 bar = foo; // default copy constructor
  bar = baz; // default copy assignment

  cout << "bar's content: " << bar.content() << '\n';
  return 0;
}

I defined a customized copy constructor, but it seems that it never gets called (In fact, the program doesn't produce any output, not even from the constructor...).

The same thing happens when I commented out line 12 where I hope to invoke the default copy constructor. Can anyone tell me what's wrong here?

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
  • Where did you run the code?(Which IDE) – DimitrijeCiric May 17 '22 at 10:33
  • 1
    You are violating the rule-of-three by not defining a proper copy assignment operator for the class but using it. So `bar = baz;` will result in a double free and therefore undefined behavior. – user17732522 May 17 '22 at 10:35
  • 1
    Replace '\n' with std::endl. std::endl in addition to inserting a newline character will also flush a buffer. – michalt38 May 17 '22 at 10:38
  • See http://cpp.sh/9aozj – Alan Birtles May 17 '22 at 10:47
  • And finally, why do you instantiate a string on heap? – Alexey May 17 '22 at 11:27
  • Elision can sometimes skip making a copy altogether. Disabling it via compiler flag would likely show you what you’re expecting. – sweenish May 17 '22 at 12:25
  • The dupe is not good and is only tangentially related to the question. – sweenish May 17 '22 at 12:26
  • @sweenish - I agree the linkage is indirect, but it is valid. The OP has not followed the rule of three, so has undefined behaviour (destruction of `baz` and `bar`, after the assignment `bar = baz` deletes a dynamically allocated string twice). Undefined behaviour means the behaviour of the *whole program* (not just code executed after the destructor call) has indeterminate outcome. Not producing expected output from one or more of the constructor calls is a perfectly feasible outcome of that undefined behaviour. – Peter May 17 '22 at 13:38
  • @sweenish The copy constructor call cannot be elided in this case. It is always called. But it is a red hering because OPs problem has nothing to do with it. They say that they never see any output at all. That's because of the undefined behavior from the double free or in this case practically speaking because the program crashes before any output is flushed (since they are not using `std::endl` or a line-buffered terminal). So I think the duplicate is relevant. – user17732522 May 17 '22 at 14:15
  • 1
    It's a relevant link, but this question is not a duplicate of it. Duplicate is a bad term about 70% of the time. I'd take it up with support, but they do not care a whit about improving the platform, it seems they're treading water holding it all together. – sweenish May 17 '22 at 14:30

0 Answers0