overloading assignment operator

I need some explanation of why I can't do this: MMsg msg2 = msg1; in the following code and still have it give the same results as
1
2
MMsg msg2;
msg2 = msg1;

I don't thing I'm understanding what exactly is going on with the '=' assignment.
Thanks.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;
using namespace System;

class MMsg {

private:
	char* m_message;
public:
	//base constructor
	MMsg(const char* message = "this is the default message"){
		
		m_message = new char[strlen(message) + 1];
		strcpy(m_message, message);
	}
	
	~MMsg() {
	delete m_message;
	}

	void show_message(){

		cout << m_message << endl;
	}
	MMsg& operator = (const MMsg& msg){
		
		if(this == &msg)
			return *this;

		delete m_message;
		m_message = new char[strlen(msg.m_message)+1];
		strcpy(this->m_message, msg.m_message);
		return *this;
	}
	void change_message(char* msg){

		delete m_message;
		m_message = new char[strlen(msg) + 1];
		strcpy(this->m_message, msg);
	}
};
int main(array<System::String ^> ^args)
{
    MMsg msg1("It's beginning to look alot like Christmas");
	msg1.show_message();
	//MMsg msg2 = msg1;
	MMsg msg2;
	msg2.show_message();
	msg2 = msg1;
	msg1.change_message("Night of the living dead");
	msg1.show_message();
	msg2.show_message();
    return 0;
}
T a=b; is syntactical sugar for T a(b);, which is a call to the copy constructor. a=b is a call to operator=().
It's strange, though. The compiler should define a default copy constructor, so it should compile.
Anyway, if you have an operator=() defined, you can easily define the copy constructor like this:
1
2
3
T(const T &a){
    *this=a;
}
Well was it not compiling or was it just not doing what you expect? I thought that a default copy constructor would have been created too, although it wouldn't have worked even if it had. Anytime you define a custom operator= you need to define the copy constructor.
Unrelated error:

You're using new[] and delete.

If you use new[], you have to use delete[].

ie:

1
2
//delete m_message;  // bad
delete[] m_message;  // good 


Looks like you need to change lines 21, 33, and 40.
I added a copy constructor and altered the delete statements. The following code compiles and runs as expected without errors; However, read the output below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;
using namespace System;

class MMsg {

private:

	char* m_message;

public:

	//base constructor
	MMsg(const char* message = "this is the default message"){
		
		cout << "base constructor called" << endl;
		m_message = new char[strlen(message) + 1];
		strcpy(m_message, message);
	}

	//copy constructor
	MMsg(const MMsg& copy) {

		cout << "copy constructor called" << endl;
		delete[] m_message;
		cout << "copy constructor called" << endl;
		m_message = new char[strlen(copy.m_message) + 1];
		strcpy(m_message, copy.m_message);	
	}

	~MMsg() {
	delete[] m_message;
	}

	void show_message(){

		cout << m_message << endl;
	}

	MMsg& operator=(const MMsg& msg){

		cout << "assignment operator called" << endl;
		if(this == &msg)
			return *this;

		delete[] m_message;
		m_message = new char[strlen(msg.m_message)+1];
		strcpy(this->m_message, msg.m_message);
		return *this;
	}
	void change_message(char* msg){
		
		delete[] m_message;
		m_message = new char[strlen(msg) + 1];
		strcpy(m_message, msg);
	}
};

int main(array<System::String ^> ^args)
{
    MMsg msg1("It's beginning to look alot like Christmas");
	msg1.show_message();
	//MMsg msg2 = msg1;
	MMsg msg2;
	msg2.show_message();
	msg2 = msg1;
	msg1.change_message("Night of the living dead");
	msg1.show_message();
	msg2.show_message();
    return 0;
}


the following output is produced:

base constructor called
It's beginning to look alot like Christmas
base constructor called
this is the default message
assignment operator called
Night of the living dead
It's beginning to look alot like Christmas
//end output

But if I use:
 
MMsg msg2 = msg1 

instead of
1
2
MMsg msg2;
msg2 = msg1;

I get the following output and the program abruptly terminates:

base constructor called
It's beginning to look alot like Christmas
copy constructor called
//end output

I ran a debug and when it gets to this line:
 
MMsg msg2 = msg1;

msg2.m_message contains garbage instead of the default message it should contain when the base constructor was called.
On line 28, you're deleting a pointer that doesn't point anywhere.
thank you Helios - solved!
Topic archived. No new replies allowed.