Thread: Calling class member

  1. #1
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820

    Calling class member

    Another quick question guys. Say I have a class eg.
    Code:
    class person
    {
     public:   
        void goToBathRoom();
        void poop();
        void pee();
    };
    
    void person :: goToBathRoom()
    {
        cout<<"What do you want to do [1]Poop [2]Pee"<<endl;
        int poopOrPee;
        cin>>poopOrPee;
        switch(poopOrPee)
        {
          case 1:
          //This is my question
          poop();
          //is this ok it seems to work 
          person::poop();
          //or is it this?
          break;
          case 2:
          pee();
          person::pee();
          break;
         }
    }
    Thanks as always
    Woop?

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I believe just calling the function is all that is needed. Since you are already working with a particular object you are inside of it's scope.

  3. #3
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    ok just making sure didn't want to mess things up
    Woop?

  4. #4
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    calling Object::function() should only be done if 'function' is static. when you just call a function from within an objects function definition, it is implied to be 'this->function()' ie.) that instance of the object is making the function call.

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>calling Object::function() should only be done if 'function' is static.
    Even then you don't need to, since calling (an object).(static member function)() is the same as calling (class):static member function)().

    The only time when you'd really need to call Classname::function() would be when the function is overloaded in a derived class and you want to call the function from the base class.
    Code:
    class A
    {
    public:
       void pee() {cout << "Now peeing...\n";}
    };
     
    class B : public A
    {
    public:
       void pee() {cout << "Peeing has commenced...\n";}
       void poo() {cout << "Now pooing...\n"; A::pee();} //Calls base class' version
    };
    Last edited by Hunter2; 07-12-2004 at 09:18 PM. Reason: Forgot to make B a derived class of A :D
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #6
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    Question ???

    what exactly is, " this-> " I have heard it before.. but I am not familiar with this somewhat abstract idea..

    if someone could break this-> down to the 1st grade level.. that would be wicked awesome.


    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  7. #7
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    ha.

    Here is my bad programming humor for the day.

    Code:
    class person
    {
     public:   
        void goToBathRoom();
        void poop();
        void pee();

    Q: shouldn't void poop() and void pee() return objects ?

    ha.
    Last edited by The Brain; 07-13-2004 at 08:31 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>Q: shouldn't void poop() and void pee() return objects ?
    It could, but you're voiding yourself of your feces and urine.

    >>what exactly is, " this-> "
    The keyword 'this' gives a pointer to the object that it's in. For example:
    Code:
    Person* Person::getPointerToTheObject()
    {
       return this;
    }
     
    int main()
    {
       Person p;
       Person* ptr1;
       Person* ptr2;
       ptr1 = &p;
       ptr2 = p.getPointerToTheObject();
     
       return 0;
    }
    In this example, both ptr1 and ptr2 will contain valid pointers to the Person object called 'p'. I believe that's how cout works; its operator<< () is defined to return an ostream&, so what it does is:
    1. Output whatever came after the <<
    2. Return (*this) (this returns a pointer to itself; so de-reference the pointer and it returns itself).

    That's why you can chain together a bunch of <<:
    Code:
    ((((cout << "Hello! My name is ") << name) << " and I am ") << age) << "years old!";
    cout << "Hello! My name is " returns cout, so the next step is cout << name, which returns cout, etc. and you get a nice chain reaction type thing going on

    And to answer your question:
    pointer->something()
    is the same as
    (*pointer).something()

    So in short, -> is a shortcut that we use because we're lazy. Also, you can overload the -> operator, but that's not done very often so you probably don't need to worry about it. Putting it all together, this->doSomething() is basically saying (this current object).doSomething().
    Last edited by Hunter2; 07-13-2004 at 11:03 AM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  9. #9
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    :)

    awesome. understand completely. excellent example. (sorry for the long time to reply)
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  10. #10
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    simple question

    oh I forgot to ask... is it possible to dereferrence "this" (i.e. this->gotothebathroom( ) ) as pointer to 'class person'..?? or will "this" only return a 'person' pointer...
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  11. #11
    Compulsive Liar Robc's Avatar
    Join Date
    Jul 2004
    Posts
    149
    >is it possible to dereferrence "this" (i.e. this->gotothebathroom( ) )
    Yes.

    >as pointer to 'class person'..?? or will "this" only return a 'person' pointer...
    I'm a bit confuzzled. A pointer to 'class person' and a 'person' pointer are the same thing. this is simply a pointer to the type that the class defines. So in your example, this is a pointer-to-person and *this is a person object.

  12. #12
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    Thumbs up :)

    got it now.. thanks for your help


    another tool to add to my intermediate c++ repitior.


    I hope I spelled, "repitior" right...
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  13. #13
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    Talking

    I think the only question I have left is.. any reason to use this instead of returning your own pointer to the object..?
    Last edited by The Brain; 07-18-2004 at 10:17 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  14. #14
    Compulsive Liar Robc's Avatar
    Join Date
    Jul 2004
    Posts
    149
    >I hope I spelled, "repitior" right...
    Repertoire, for future reference.

    >any reason to use this instead of returning your own pointer to the object..?
    Well, you should be thinking twice about returning a pointer to the object to begin with, but why bother creating another pointer just to return it when this is sitting there waiting to be used?

  15. #15
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>any reason to use this instead of returning your own pointer to the object..?
    Yes. For example, if you have a member function which adds a pointer of the object to a static class array for internal use:
    Code:
    class MyClass
    {
    public:
       MyClass() {IndexMyself();}
       ~MyClass() {UnIndexMyself();}
       void DoSomething();
       void DoSomethingElse(); //or whatever
       static void DoSomethingToAllObjectsEverCreated();
     
    protected:
       void IndexMyself();
       void UnIndexMyself();
       static std::set<MyClass*> allObjects;
    };
     
     
    void MyClass::IndexMyself()
    {
       allObjects.insert(this);
    }
     
    void MyClass::UnIndexMyself()
    {
       allObjects.erase(allObjects.find(this));
    }
     
    void MyClass::doSomethingToAllObjectsEverCreated()
    {
       for(std::set<MyClass*>::iterator it = allObjects.begin(); it != allObjects.end(); ++it)
       {
    	  //do something to each object
       }
    }
    It would be hard to do something like that without the use of this. Of course, you could store the pointers in your own array and write your own function and blah blah blah, but that's just an example of something you could do using this (make the class more of an automatic thing)

    **EDIT**
    Also, like I said... without returning this, you wouldn't be able to chain together a bunch of << with cout. And that's another good reason to use this

    **EDIT2**
    !@#$%!^% I hate these edit boxes!!! Why does it always screw up my code???
    Last edited by Hunter2; 07-18-2004 at 12:31 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing class member function to pthread_create
    By lehe in forum C++ Programming
    Replies: 6
    Last Post: 03-27-2009, 07:47 PM
  2. derived class can not access base class protected member?
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2007, 06:32 PM
  3. _beginthread & calling a class member function
    By matth in forum C++ Programming
    Replies: 1
    Last Post: 03-10-2006, 02:39 PM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM