Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. static getter/setter
Forum Updated to NodeBB v4.3 + New Features

static getter/setter

Scheduled Pinned Locked Moved Solved C++ Gurus
5 Posts 3 Posters 3.0k Views 2 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
    user4592357
    wrote on last edited by
    #1

    i need to have a static member in my class that will be assigned a value a few times during the program. i have a few ideas on how to do this but i'm not sure which is more correct. here's those ways:

    1. have a static member, and have a static method that'll return a reference to the static member
    2. have a static method that will create a static variable inside it and return a reference to it

    so which is more correct? or there's a better way? don't forget that i need to assign value to this. the reason i'm not sure which one to use is that if i use the 2nd option, is it appropriate to assign a value to function return value?

    thanks

    1 Reply Last reply
    0
    • JohanSoloJ Offline
      JohanSoloJ Offline
      JohanSolo
      wrote on last edited by JohanSolo
      #2

      IMHO solution 1 is better: you have a static member and static getter / setter. If you go with the second solution, then you're forced to use the same accessor for get / set, which might cause problem if you have a typo like if ( MyClass::myStaticGetter() = 8 ) instead of if ( MyClass::myStaticGetter() == 8 ).

      `They did not know it was impossible, so they did it.'
      -- Mark Twain

      1 Reply Last reply
      0
      • aha_1980A Offline
        aha_1980A Offline
        aha_1980
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @user4592357 said in static getter/setter:

        have a static method that will create a static variable inside it and return a reference to it

        Even if this is possible, I would not recomment to do this. The only place I have seen a pattern like this is for singleton, a la:

        static *Singleton::instance()
        {
          static Singleton *s = new Singleton;
          return s;
        }
        

        And what about option three: Having a static setter also? Therefore you encapsulate the access to your variable and can do sanity checks before the assignment also.

        And as you mentioned reference: if the size of your variable is <= 16 bytes, it may be more efficient to pass by value. If it is larger, pass by const reference.

        Qt has to stay free or it will die.

        U 1 Reply Last reply
        1
        • aha_1980A aha_1980

          @user4592357 said in static getter/setter:

          have a static method that will create a static variable inside it and return a reference to it

          Even if this is possible, I would not recomment to do this. The only place I have seen a pattern like this is for singleton, a la:

          static *Singleton::instance()
          {
            static Singleton *s = new Singleton;
            return s;
          }
          

          And what about option three: Having a static setter also? Therefore you encapsulate the access to your variable and can do sanity checks before the assignment also.

          And as you mentioned reference: if the size of your variable is <= 16 bytes, it may be more efficient to pass by value. If it is larger, pass by const reference.

          U Offline
          U Offline
          user4592357
          wrote on last edited by
          #4

          @aha_1980

          good idea. but i don't need to do checks before assigning, should i still use static getter + static setter?

          it'll be a QString, so a reference or value?

          1 Reply Last reply
          0
          • aha_1980A Offline
            aha_1980A Offline
            aha_1980
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @user4592357 said in static getter/setter:

            @aha_1980
            good idea. but i don't need to do checks before assigning, should i still use static getter + static setter?

            Isn't the reason to use classes that you can hide your data? And even if you don't need a check now, this can change in half a year. Further, it avoids the problems @JohanSolo mentioned.

            it'll be a QString, so a reference or value?

            In this case I'd use void mySetter(const QString &str); like all the Qt API do it.

            Qt has to stay free or it will die.

            1 Reply Last reply
            2

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved