Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
If i want to make function callable without using class object,It's necessity of the programmer to make it public & static, so that it is callable without creating object of class.

Now my problem is when I do so, the function seems to be alienated by the class. it belongs to as pointer to interface objects I need to use are not pointing to the objects they do in functions that are not static. To get access to those pointers again I remove the static identifier, thereby rendering the function un callable. (And around and around I go...)

While I'm sure this is the way things are, how can I make a function static and call it while retaining the pointers I would otherwise enjoy.

Thank you.

:Ron
Posted
Updated 12-Jul-11 19:50pm
v3

I think your problem is here: "It's necessity of the programmer to make it public & static, so that it is callable without creating object of class."

This is a misconception. To understand it, learn how Singleton design pattern works.

See:
http://en.wikipedia.org/wiki/Design_pattern_(computer_science)[^],
http://en.wikipedia.org/wiki/Singleton_(mathematics)[^],
http://en.wikipedia.org/wiki/Singleton_pattern[^].

In your case, you need to have an instance of the class but provide a mechanism making this instance to be only one per your process. Look at the code sample in the referenced article and you well see it.

Static members of the class are not really alienated by the class. They simply have no access no any non-static members as they don't have an access to the instance. They have access to all static members of the class. Each static members are per-class, each non-static — per instance.

Also, it is not true that a static method function work with the instance of its class. It can, but the instance should be passed to it as a parameter.

Now, a little surprise for you: do you know that the non-static function (also called the instance function) works with the instance exactly in the same way: it is passed as a function parameter; only this parameter is implicit, not shown in the signature. Now, when I tell you how this parameter is called, you should catch the essence of instances. Read my lips: it is called "this". Think about it.

—SA
 
Share this answer
 
Comments
Richard MacCutchan 13-Jul-11 5:50am    
+5, I like it, especially the last paragraph.
Sergey Alexandrovich Kryukov 13-Jul-11 10:34am    
Thank you, Richard.
--SA
ThatsAlok 13-Jul-11 6:29am    
agreed with richard :-) +5
Sergey Alexandrovich Kryukov 13-Jul-11 10:34am    
Thank you.
--SA
You can't. I thnk what you're saying is, how do you access properties inside a class instance. Static methods exist outside of any class instance. If you have a variable called 'i', and two class instances, how does your static method know which 'i' you're referring to ?
 
Share this answer
 
If you need both, then either move the static function in another class or rename it and then implement the member function by calling the static function.

class A
{
public:
    static int fs() { return 1; }
    int fm() { return fs(); }
};

int main()
{
    A a;

    // Use static function directly
    std::cout << A::fs() << std::endl;

    // Use instance function directly
    std::cout << a.fm() << std::endl;

    // Use static function via a pointer to function
    int (*f1)() = &A::fs;
    std::cout << a.fm() << std::endl;

    // Use instance function via a pointer to a member function
    int (A::*f2)() = &A::fm;
    std::cout << (a.*f2)() << std::endl;
}


By the way, I think you could uses boost to avoid the problem of having to know if the function is static or not...

C++ is a bit inconsistent in that area as a.fs() would compile but it not possible to take the address of a static function as it was a member function. In my opinion, either both should have been allowed or both prohibited. I would opt for the second option as someone would be able to optimize a function that does not need this by making it static without affecting code (but not the other way around).
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900