Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Using assignment operator = for a class with const memeber variables?

Using assignment operator = for a class with const memeber variables?

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresquestion
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 13978559
    wrote on last edited by
    #1

    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 WORK

    delete[] 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?

    C L _ 3 Replies Last reply
    0
    • U User 13978559

      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 WORK

      delete[] 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?

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      There could be a lot of guesswork, but, as a matter of fact, I suppose you should ask your Lecturer.

      1 Reply Last reply
      0
      • U User 13978559

        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 WORK

        delete[] 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?

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • U User 13978559

          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 WORK

          delete[] 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?

          _ Offline
          _ Offline
          _Superman_
          wrote on last edited by
          #4

          Since the value of SIZE is constant and the same for all instances of the class, I suggest it be made a static const. That way you wouldn't need to assign a value to it in the assignment operator overload.

          static const int SIZE = <value>;

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups