Thread: is it possible to use a function of a class, without instantiating that class?

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    142

    is it possible to use a function of a class, without instantiating that class?

    if so how? I saw this in java, i watned to do this too in C++,

    ie,

    class a
    {
    afunction
    };

    a.afunction(...) // when using,

    many thanks

  2. #2
    Registered User quagsire's Avatar
    Join Date
    Jun 2002
    Posts
    60
    Declare the member function as static and use scope resolution :
    Code:
    class a
    {
       public :
       static void test()
       {
          cout << "hallo";
       }
    };
    
    int main()
    {
       a::test();
       return 0;
    }
    Just note that no non-static member functions or member variables should be used.

  3. #3
    mickeyREN
    Guest
    wow, thanks!!

    just wondering if this is the 'only' and standard way to do this? because i don't remember encountering this stuff when i was reading my c++ book,

    thanks, that'll be my last question i hope,

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    yes its the only way. You can call a static function without an object. You cannot do this with other member functions because they must send a this pointer.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    142
    alright thanks!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. class errors
    By romeoz in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2003, 07:57 PM