Using assignment operator = for a class with const memeber variables?
-
I'm doing an assignment and it was told that SIZE of the array has to be const and also that I will need to create an assignment operator, but idk maybe my lecturer didn't think about it? I have this code:
class NameList
{
private:
const int SIZE; // Size of the array
string* mp_list; //points to the start of the array
...And then there's assignment operator
NameList NameList::operator = (const NameList &otherList)
{
SIZE = otherList.SIZE; // DOES NOT WORKdelete[] mp_list;
mp_list = new string[SIZE]; // Creating an array with a new size*mp_list = *otherList.mp_list; // copying content of the other array
}This will not work since you can't change const. But it's bad if I don't change the SIZE variable. Is there a way to create an assignment operator with const variable? Or is it ok if the size of the array is not const or it's bad programming? What would you do?
-
I'm doing an assignment and it was told that SIZE of the array has to be const and also that I will need to create an assignment operator, but idk maybe my lecturer didn't think about it? I have this code:
class NameList
{
private:
const int SIZE; // Size of the array
string* mp_list; //points to the start of the array
...And then there's assignment operator
NameList NameList::operator = (const NameList &otherList)
{
SIZE = otherList.SIZE; // DOES NOT WORKdelete[] mp_list;
mp_list = new string[SIZE]; // Creating an array with a new size*mp_list = *otherList.mp_list; // copying content of the other array
}This will not work since you can't change const. But it's bad if I don't change the SIZE variable. Is there a way to create an assignment operator with const variable? Or is it ok if the size of the array is not const or it's bad programming? What would you do?
-
I'm doing an assignment and it was told that SIZE of the array has to be const and also that I will need to create an assignment operator, but idk maybe my lecturer didn't think about it? I have this code:
class NameList
{
private:
const int SIZE; // Size of the array
string* mp_list; //points to the start of the array
...And then there's assignment operator
NameList NameList::operator = (const NameList &otherList)
{
SIZE = otherList.SIZE; // DOES NOT WORKdelete[] mp_list;
mp_list = new string[SIZE]; // Creating an array with a new size*mp_list = *otherList.mp_list; // copying content of the other array
}This will not work since you can't change const. But it's bad if I don't change the SIZE variable. Is there a way to create an assignment operator with const variable? Or is it ok if the size of the array is not const or it's bad programming? What would you do?
You need to assign a value to a constant item, otherwise it has no meaning. You need something like:
class NameList
{
private:
const int SIZE = 100; // Size of the array
string* mp_list; //points to the start of the array
...
public:
NameList()
{
mp_list = new string[SIZE];
}
...From there you should be able to figure out how to make your assignment operator method.
-
I'm doing an assignment and it was told that SIZE of the array has to be const and also that I will need to create an assignment operator, but idk maybe my lecturer didn't think about it? I have this code:
class NameList
{
private:
const int SIZE; // Size of the array
string* mp_list; //points to the start of the array
...And then there's assignment operator
NameList NameList::operator = (const NameList &otherList)
{
SIZE = otherList.SIZE; // DOES NOT WORKdelete[] mp_list;
mp_list = new string[SIZE]; // Creating an array with a new size*mp_list = *otherList.mp_list; // copying content of the other array
}This will not work since you can't change const. But it's bad if I don't change the SIZE variable. Is there a way to create an assignment operator with const variable? Or is it ok if the size of the array is not const or it's bad programming? What would you do?
Since the value of
SIZE
is constant and the same for all instances of the class, I suggest it be made astatic const
. That way you wouldn't need to assign a value to it in the assignment operator overload.static const int SIZE = <value>;