Thread: Calling non-static functions on static variables

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    14

    Calling non-static functions on static variables

    In the Controller class, I have the following:
    Code:
    static Controller the;
    Controller()
    	{
    		static Controller the=*this;
    	}
    In my main function:
    Code:
    static Controller::the->mainloop();
    mainloop() is a non-static function of the Controller class. This doesn't work. I'm pretty sure I'm calling it wrong.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Controller::the.mainloop();
    Remove "static" keyword. Only required when defining variables.
    Since "the" isn't a pointer, you can't use "->", you must use ".".
    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.

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    14

    thanks

    Thanks a lot. It's kinda inconsistent and unintuitive...

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, not really. What did you expect? I find it exactly what it should be.
    No complications.
    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.

  5. #5
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    "the" is the most unintuitive variable name you could possibly use O_o

  6. #6
    Registered User
    Join Date
    Jun 2008
    Posts
    14
    If, lets say, you have a person named John Smith, sometimes you can also see the name written Smith, John. Likewise, "the controller" is Controller::the. I got the habit from Java, where you would have Controller.the.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Aha. Another Java user.
    No wonder, then.
    You'll get used to it. C++ is far superior to Java
    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 2006
    Posts
    2,149
    What are you trying to do here?
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  9. #9
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    Is Controller supposed to be a singleton class?

  10. #10
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    In the constructor of 'Controller' you affect *this to a static variable of type 'Controller' ? What do you want to do?

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    90
    Assuming you want a singleton class, do something like this:

    Code:
    class Controller {
    public:
      static Controller *the;
    
      static void init()
      {
        the = new Controller();
      }
    
      // declare other functions here
    
    private:
      Controller()
      {
        // do initialization here
      }
    }
    Then somewhere near the start of your program, do this:

    Code:
    Controller::init();

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Elysia View Post
    Remove "static" keyword. Only required when defining variables.
    Not true. Class member functions can be static; the only difference is that they are called outside the context of particular objects.

  13. #13
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    The word "static" in a static variable declaration doesn't mean the same thing as the word "static" in a static member declaration -- two unrelated concepts that happen to overload the same keyword.

  14. #14
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by grumpy View Post
    Not true. Class member functions can be static; the only difference is that they are called outside the context of particular objects.
    The point Elysia was trying to make is that they are not used when applying variables or functions. Yes, the static key word has several uses.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, point was that you don't use "static" when calling a function.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about static members and functions
    By e66n06 in forum C++ Programming
    Replies: 5
    Last Post: 01-07-2008, 02:41 PM
  2. Using private class members in static functions
    By sethjackson in forum C++ Programming
    Replies: 2
    Last Post: 09-23-2005, 09:54 AM
  3. Static variables and functions problem in a class
    By earth_angel in forum C++ Programming
    Replies: 16
    Last Post: 09-15-2005, 12:08 PM
  4. Visual C++ 6/.net Debug static variables
    By neandrake in forum C++ Programming
    Replies: 2
    Last Post: 04-27-2005, 03:45 AM
  5. Replies: 2
    Last Post: 10-02-2004, 10:12 AM