assignment operator

So im making a somewhat bigger project for a assignment in school and i'm wondering how i should go about and construct my assignment operator. Iv got 2 classes that inherit from another one and in the base class i make an array to hold 3 strings.

This is how my code snip looks like atm, what i'm wondering tho is if i need to copy the array as well in order for it to work properly and in that case how?
1
2
3
4
5
6
 Competiror::Competiror(const Competiror &origObj)
 {
	 this->name=origObj.name;
	 this->phonenNumber=origObj.phonenNumber;
	 this->age=origObj.age;
 }
What array? Show us your class definition. From the look of your assigment operator, if all types are non-pointer, you could leave default generated one.
1
2
3
4
5
6
7
8
9
10
11
12
13
const int sportsSize = 3;

class Competiror
{
private:
	Sport *sports[sportsSize];
	string name;
	string phonenNumber;
	int age;


public:
	
Yes, you should copy each array element individually. Or each dereferenced array elemend depending on what do you actually need.

If you need fixed size arrays, you should look into std::array. It supports assigning and some useful member functions like size(), begin() and end();
Do you reckon this would work:

1
2
3
4
5
6
7
8
9
10
11
 Competiror::Competiror(const Competiror &origObj)
 {
	 this->name=origObj.name;
	 this->phonenNumber=origObj.phonenNumber;
	 this->age=origObj.age;

	 for (int i = 0; i < sportsSize; i++)
	 {
		 sports[i] = origObj.sports[i];
	 }
 }


what the program does is creating a "fakeathlon" there every user competes in 3 sports (to give you an idea of what the program is meant to do).
@ OP: You realize that you have an array of pointers, and not a pointer to an array right? That was a mistake that I used to make far too often. How are you initializing those private data members if you don't mind me asking? There is a potential for an certain error here that would be easy to overlook.
@Computergeek01

Your right, the array is meant to have a fixed size so its better to have " Sport sports[sportsSize];" but now i need to implement the operator= as well.
Topic archived. No new replies allowed.