Thread: question about class design and overloading

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    question about class design and overloading

    Hello

    I have a simple class in which I have int timeouts and some other data..

    For instance:

    Code:
    class data {
    public:
      data() { }
    
      int &timeouts() { return m_timeouts; }
      //...and so on..
    
    private:
      int m_timeouts;
      int m_some_other_data;
      std::string m_somestr;
    };
    Now I dont only want to access timeouts but also to preform ++ operation on it (to increase it for 1)..
    Now I wonder.. Would it be more sensible to make new function increase_timeouts() or overload operator ++ on class so that I could do:

    data data_instance;
    data_instance++;

    This looks nicer but I wonder if you guys have any considerations regarding that kind of design since user might not know what ++ operator does..

    What would you guys do?

    Thanks for help again

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    if 'data' stores other things than a ++ on data is ambiguous. I think an increment function would be more intuitive.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by Perspective View Post
    if 'data' stores other things than a ++ on data is ambiguous. I think an increment function would be more intuitive.
    Or maybe make timeouts public or function return reference (without const - so it would be allowed to change)?

    I dont actually like increment_number() kind of functions..

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I definitely agree with Perspective. operator++ should only be used when it's "obvious" what it means - and in this case, it's not clear what data it increments.

    Exposing the member variables isn't the right solution - that just exposes stuff inside your class that don't really belong outside the class.

    What does it actually "mean" that you are incrementing the timeout?

    If the class implementation isn't "obvious", then perhaps the entire class is wrong?

    I would say that a "inc(rement)..." type function call should be the right thing to do - if it's not, then perhaps the entire approach is wrong.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    A solution might be a helper class. The helper class overloads operator ++, but nothing else (or maybe restricts operators = and declares its parent class as friend).
    Then you can actually declare your int as that helper class and make a function that returns a reference to it.
    This way you can do myobj->gettimeouts()++;
    Or just myobj->timeouts++;
    It could be public as long as you restrict the ability for anything outside the class to modify it.

    Those are my thoughts.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by Elysia View Post
    A solution might be a helper class. The helper class overloads operator ++, but nothing else (or maybe restricts operators = and declares its parent class as friend).
    Then you can actually declare your int as that helper class and make a function that returns a reference to it.
    This way you can do myobj->gettimeouts()++;
    Or just myobj->timeouts++;
    It could be public as long as you restrict the ability for anything outside the class to modify it.

    Those are my thoughts.
    So that I make a nested class (timeout) inside the class and overload ++ operator on it?

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes.
    You can also overload more, but make them private and declare the parent class as a friend, so it has full access to the object.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I have no clue what you're using this class for, but does the user actually need to increment the timeout or would it be better for a member function to do that after the function returns successfully or something like that?
    Would you only ever need to increment by 1 or would you also need to add 10, subtract 3, multiply by 2...? If so, you could just have a pair of SetTimeout() & GetTimeout() functions and allow the user to increment/decrement it as much as they want.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Declaring instances of different implementations
    By dwks in forum C++ Programming
    Replies: 8
    Last Post: 07-16-2008, 11:43 AM