Thread: static member function ISSUE

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    56

    static member function ISSUE

    Code:
    #include <iostream>
    
    using namespace std;
    
    class test
    {
    
        public:
            static string justAFunction();
        private:
    
    }myTest;
    
    string test::justAFunction()
    {
    
        return string("I'm just testing...");
    
    }
    
    int main()
    {
        cout<<test.justAFunction();
    }
    Why couldn't I make this static function work? Arent static member functions made so i can call them withtout making an object of that class?

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by dhuan View Post
    Code:
    #include <iostream>
    
    using namespace std;
    
    class test
    {
    
        public:
            static string justAFunction();
        private:
    
    }myTest;
    
    string test::justAFunction()
    {
    
        return string("I'm just testing...");
    
    }
    
    int main()
    {
        cout<<test.justAFunction();
    }
    Why couldn't I make this static function work? Arent static member functions made so i can call them withtout making an object of that class?
    you are correct, but you cannot use the dot operator. you must use the :: operator instead, so in your case it would be
    Code:
    cout << test::justAFunction();

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Incidentally, if you are basically just using the class name as a namespace, then you might as well just use a namespace.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 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. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM