Reputation: 13
I am trying to write an overloaded operator=
to assign copy the content of an object studentA
to studentB
. After compiling, it just crashes and as usual "stops working".
#include <cstdlib>
#include <iostream>
using namespace std;
class STUDENT {
public:
string ID;
string name;
unsigned int birthyear;
STUDENT(string x, string y, unsigned int z) {
ID = x;
name = y;
birthyear = z;
};
STUDENT operator = (STUDENT const &obj) {
STUDENT *res;
*res = obj;
return *res;
}
void print() {
cout << ID << " " << name << " " << birthyear;
}
};
int main() {
STUDENT studentA("1951049", "Nguyen Vinh Hao", 2001);
STUDENT studentB("1951050", "Nguyen Van Hao", 1999);
studentB = studentA;
studentB.print();
return 0;
}
I think there are some problems with the pointer *res
, but I don't really know what they are. I hope you can help me.
Upvotes: 1
Views: 249
Reputation: 31
Here STUDENT *res;
is currently not pointing anywhere and so you can't assign it values.
You don't need STUDENT *res;
here
You can just return obj;
or if you want obj
to be assigned to another pointer and then return it, you can do
STUDENT operator=(STUDENT const &obj)
{
const STUDENT *res;
res = &obj;
//here the pointer res points to the same address as the obj, thus they are pointing to same object.
return *res;
}
Upvotes: 1
Reputation: 356
In your case, you do not need to implement your own copy assignment operator=
, because both std::string
and (unsigned) int
types have their own built-in copy operator. In other words, they know how to copy safely their own resources.
As @Remy Lebeau already suggested the best option is to use the default:
class_name & class_name :: operator= ( const class_name & ) = default;
Even if you used Containers from the standard template library containers as your member variables,
class STUDENT {
public:
string ID;
string name;
unsigned int birthyear;
vector v; /*for example std::vector*/
};
again, you shouldn't need to worry about copying, moving, or managing the memory of your class. They handle their resources!
The container manages the storage space that is allocated for its elements and provides member functions to access them, either directly or through iterators (objects with properties similar to pointers).
You should start worrying about copy semantics when you introduce pointers in your classes, given that those pointers are responsible for managing the resources where they point to!
class STUDENT {
public:
string ID;
string name;
unsigned int birthyear;
float * p; /* this can introduce bug if not handled properly */
};
You can read more on previous questions: here, and here
Upvotes: 1
Reputation: 595329
Your operator=
is implemented all wrong. Your res
pointer does not point anywhere meaningful when you dereference and assign to it. That is why your code is crashing.
You need to actually copy the individual members yourself from obj
to *this
, similarly to how you are doing so in your constructor, eg:
#include <iostream>
using namespace std;
class STUDENT {
public:
string ID;
string name;
unsigned int birthyear;
STUDENT(string x, string y, unsigned int z) {
ID = x;
name = y;
birthyear = z;
}
STUDENT& operator=(STUDENT const &obj) {
ID = obj.ID;
name = obj.name;
birthyear = obj.birthyear;
return *this;
}
void print() const {
cout << ID << " " << name << " " << birthyear;
}
};
int main() {
STUDENT studentA("1951049", "Nguyen Vinh Hao", 2001);
STUDENT studentB("1951050", "Nguyen Van Hao", 1999);
studentB = studentA;
studentB.print();
return 0;
}
That being said, you are using types that the compiler already knows how to copy, so you should let the compiler generate a suitable copy assignment operator for you, eg:
#include <iostream>
using namespace std;
class STUDENT {
public:
string ID;
string name;
unsigned int birthyear;
STUDENT(string x, string y, unsigned int z) {
ID = x;
name = y;
birthyear = z;
}
//STUDENT& operator=(STUDENT const &) = default;
void print() const {
cout << ID << " " << name << " " << birthyear;
}
};
int main() {
STUDENT studentA("1951049", "Nguyen Vinh Hao", 2001);
STUDENT studentB("1951050", "Nguyen Van Hao", 1999);
studentB = studentA; // <-- automatically does the "right thing" for you!
studentB.print();
return 0;
}
Upvotes: 4