Noob question about classes

This is a discussion on Noob question about classes within the C++ Programming forums, part of the General Programming Boards category; If I have a class defined like this: Code: class Sprite { public: int xpos; int ypos; void move(); }; ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    26

    Noob question about classes

    If I have a class defined like this:

    Code:
    class Sprite {
        public:
          int xpos;
          int ypos;
          void move();
    };
    And then I go to define the function move(), this seems to work fine:

    Code:
    Sprite::move() {
        xpos = xpos++;
        ypos = ypos++;
    };
    But I was wondering how you distinguish between the class variable Sprite.xpos and a global variable xpos if you have one. If it's inside a class definition does it always assume you're talking about the class variable?

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Easy. Prefix your member variables with "m_", then all is good in the world.
    Otherwise you can say this->xpos which is just more typing than m_xpos.
    Last edited by cpjust; 06-27-2008 at 08:38 PM. Reason: I need some sleep...

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    26
    Ah, thanks.

    Hmm, I'm getting some kind of error if I use this.xpos, but if I put this->xpos it seems to work, is "this" actually a pointer?

    Is there a way to change the global variable xpos from within the class function? I could just rename my variables of course, but I'm curious if it's possible...

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Sorry, it's been a long day. Yes, this is a pointer, so you need to use -> instead of .

    You can use the :: operator to refer to the global namespace. Ex:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int var;
    
    class Test
    {
    public:
    	void Run()
    	{
    		this->var = 3;
    		::var = 5;
    	}
    
    	int GetVar() const
    	{
    		return var;
    	}
    
    private:
    	int var;
    };
    
    int main()
    {
    	Test test;
    	test.Run();
    
    	cout << "Global var = " << var << "\nTest::var = " << test.GetVar() << endl;
    
    	return 0;
    }

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    26
    Oh, of course, the scope thingy.

    That's great, thanks for your help.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,647
    Actually, the member variable name hides the global variable name in member function scope, so prefixing with this-> is unnecessary. You would only have to fully qualify the global variable name:
    Code:
    var = 3;
    ::var = 5;
    I am not sure if member variable name decoration is necessary. I like to use an underscore suffix, but recently it has come to my attention that one can use the same name for a constructor argument as a member variable and still use the initialisation list correctly. I am not concerned with other cases where there can be ambiguity since one can always qualify the variable names as needed.

    A little off topic, but instead of writing:
    Code:
    xpos = xpos++;
    ypos = ypos++;
    Write:
    Code:
    ++xpos;
    ++ypos;
    For further reference read Stroustrup's answer to the FAQ What's the value of i++ + i++?
    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,548
    I like to put prefixes to variables to remember what scope they're in.
    So I put "m_" before any class member variables and "g_" before any global variables.
    Then you shouldn't have as much trouble differentiating the variables.

    Local variables to a function could be prefixed with "f_" or simply nothing at all. Then you'll know where the variables are defined.
    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
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Prefixes are a form of Hungarian Notation, which is bad for many reasons, one of which is that names lie if types (or scope, in this case) changes. Conventions are also only as consistent as those using them.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    All things have ups and downs.
    No matter what you do, you still have to change something if the scope of a variable changes. Well, almost always anyway.

    You use notations and you have to rename them when their scope changes.
    If you use "this->" and "::" for members and global variables, you have to remove or change them if the scope changes, and so on.
    Besides, some complain at the use of "this->" before the use of a member variable if not necessary.
    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.

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    True. Actually, I don't use prefixes or the this->qualifier. I also don't tend to write huge member functions, so it's fairly intuitive to the reader that the variable is a member variable.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Quote Originally Posted by medievalelks View Post
    ...so it's fairly intuitive to the reader that the variable is a member variable.
    I know once when I looked over some old and thought "WTH? Where did this variable come from?".
    It took me a while to realize it was a member. So every since then I always prefix members with "m_".
    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.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,647
    If you use "this->" and "::" for members and global variables, you have to remove or change them if the scope changes, and so on.
    The difference is that name decoration depends on convention, but full qualification of names depends on the core language syntax. I suspect that that is what Stroustrup had in mind when he wrote that he has "problems with every scheme that embeds information about language-technical details (e.g., scope, storage class, syntactic category) into names".

    I know once when I looked over some old and thought "WTH? Where did this variable come from?".
    It took me a while to realize it was a member. So every since then I always prefix members with "m_".
    If the function is of a digestible size, I find it easy to spot if a variable is a local variable. If it is not, and the function is a member function, then the variable is probably a member variable, so I would look it up to confirm.
    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

  13. #13
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by medievalelks View Post
    Prefixes are a form of Hungarian Notation, which is bad for many reasons, one of which is that names lie if types (or scope, in this case) changes. Conventions are also only as consistent as those using them.
    That's true if you use full Hungarian Notation such as iNum, fVal, bRet, dwSomething...
    I only use m_ & g_ for member & global variables, and sometimes p for pointers. I leave the actual types of the variables out of the notation.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question on classes
    By ssjnamek in forum C++ Programming
    Replies: 3
    Last Post: 05-09-2005, 04:12 PM
  2. Quick Question on Classes
    By gguy85 in forum C++ Programming
    Replies: 6
    Last Post: 02-14-2005, 01:28 AM
  3. Question about classes and inheritance
    By ~Kyo~ in forum C++ Programming
    Replies: 4
    Last Post: 10-23-2004, 05:36 PM
  4. simple noob question: HWND, HDC etc.
    By freedik in forum Windows Programming
    Replies: 12
    Last Post: 08-19-2003, 03:59 AM
  5. Newbie question about the Private type in classes
    By Ryeguy457 in forum C++ Programming
    Replies: 1
    Last Post: 09-07-2002, 10:17 PM

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