Text RPG - Overloading Operators

This is a discussion on Text RPG - Overloading Operators within the Game Programming forums, part of the General Programming Boards category; Aside from my Database project, I have another little secret I'm creating (more like trying to create) a Text RPG. ...

  1. #1
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156

    Text RPG - Overloading Operators

    Aside from my Database project, I have another little secret

    I'm creating (more like trying to create) a Text RPG. I have a StatsMgr class in which i'm going to add a few overloaded operators to manipulate the Users levels.

    I'm just starting to code the first operator to increment the Players Level, but then I thought "Do I need to overload the prefix operator or the postfix operator?".

    Hm.... Which is suitable? Tell me what you think

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The general rule of thumb is prefix operator, unless you actually need a postfix.

  3. #3
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    Quote Originally Posted by tabstop View Post
    The general rule of thumb is prefix operator, unless you actually need a postfix.
    Thanks, that's easier of the 2 in my oppinion anyways

    [edit]
    I have overloaded it

    Code:
    StatsMgr& StatsMgr::operator++()
    {
        ++PlayerLevel;
        return *this;
    }
    What does everyone think? Is it alright?
    Last edited by legit; 11-25-2008 at 12:04 PM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,744
    hmm... you are talking about C++ operator overloading, right? If so, are you sure that overloading an operator is even the correct choice to begin with?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    Quote Originally Posted by laserlight View Post
    hmm... you are talking about C++ operator overloading, right?
    Yeah, sure I am.

    Quote Originally Posted by laserlight View Post
    If so, are you sure that overloading an operator is even the correct choice to begin with?
    Sorry, i'm not following?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,744
    Quote Originally Posted by legit
    Sorry, i'm not following?
    Suppose you see this: ++stats_mgr; Would you think that this is "increment player level" or "increment stats manager"? On the other hand, the call of a named function like stats_mgr.playerLevelUp() or something like that would immediately bring to mind "increment player level".
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    The postfix operator can be implemented as creating a temp object and invoking the prefix operator on it and returning it.
    Alternatively, you can derive from boost::incrementable.
    http://www.boost.org/doc/libs/1_32_0....htm#smpl_oprs

    But, as laserlight says, beware. If it isn't immediately obvious what the operators does, don't do it.
    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
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    Quote Originally Posted by laserlight View Post
    Suppose you see this: ++stats_mgr; Would you think that this is "increment player level" or "increment stats manager"? On the other hand, the call of a named function like stats_mgr.playerLevelUp() or something like that would immediately bring to mind "increment player level".
    Oh so it's better to just make a function to do this instead of using operators?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,744
    Quote Originally Posted by legit
    Oh so it's better to just make a function to do this instead of using operators?
    Probably, unless you were overloading operator++ for a PlayerLevel class. Note, however, that overloaded operators are functions.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    Quote Originally Posted by laserlight View Post
    Probably, unless you were overloading operator++ for a PlayerLevel class. Note, however, that overloaded operators are functions.
    Ok thanks, I'll just create a non-overloading operator function to do this for me

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 10-27-2007, 12:42 PM
  2. text rpg help
    By xxwerdxx in forum Game Programming
    Replies: 1
    Last Post: 11-26-2005, 07:16 PM
  3. How to use FTP?
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2005, 02:17 PM
  4. Overloading compound operators?
    By Nyda in forum C# Programming
    Replies: 2
    Last Post: 04-28-2004, 05:42 AM
  5. Check out My Text Rpg Game
    By knight543 in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2002, 10:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21