Sign in to follow this  
swiftcoder

instance.staticFunction( ) ???

Recommended Posts

swiftcoder    18432
This one is really bothering me, basically I implemented my Kernel class like this to start with:
class Kernel
{
public:
void addTask(Task *t);
void removeTask(Task *);
};
And then I changed it to this:
public:
static void addTask(Task *t);
static void removeTask(Task *t);
Now it is really messing with my head, I can not only call
Kernel::addTask(t);
But I can call
kernel_instance->addTask(t);
even though only the static functions are there. At first I thought this was a problem with the code cache not having synchronized with my source files, but it still works both ways after multiple cleans and a manual emptying of the cache. I am running GCC 4.0, and I am wondered if this is an extension to the C++ standard (ability to call static member functions on an instance of the class), or if a bug in GCC, or just some thing really messed up in my IDE. Any thoughts would be aprecciated, SwiftCoder

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
I can't say one way or another whether that should work as it appears to, but I can make a suggestion: If you're going to have a class with only static functions, why not add in a pure virtual function and/or a private copy/constructor so it won't be possible to create instances? Alternately, why not just use a namespace..?
-Extrarius

Share this post


Link to post
Share on other sites
swiftcoder    18432
Quote:
Original post by Anonymous Poster
If you're going to have a class with only static functions, why not add in a pure virtual function and/or a private copy/constructor so it won't be possible to create instances? Alternately, why not just use a namespace..?
-Extrarius

I just trimmed the interface down to the couple of functions that showed the problem, it actually does much more, but I will keep that in mind.
Quote:
Original post by Sharlin
No, it's standard C++.

Interesting, I guess I just never tried this before, good to know anyway.

Share this post


Link to post
Share on other sites
Timjanmannen    122
kernel...->staticfunc() is the same function anyway, why should you not be able to use it?
I am not 100% accurate but i think that with non-static classfunctions, the compiler passes the adress of the class as a first 'invisible' argumnet. if it is a static memberfunction, it dosnt, it is as if you called any function. sortof like a function declared in a namespace. im not too sure about this, but i have a feeling it is something like that.

Share this post


Link to post
Share on other sites
Zahlman    1682
The static function will be resolved, AFAIK, according to the static type of the instance variable, and called just the same as if you had written it "normally". It's rather like how you can do both sizeof(int) and sizeof(foo) (where you have previously declared "int foo;").

Share this post


Link to post
Share on other sites
swiftcoder    18432
Quote:
Original post by Zahlman
The static function will be resolved, AFAIK, according to the static type of the instance variable, and called just the same as if you had written it "normally". It's rather like how you can do both sizeof(int) and sizeof(foo) (where you have previously declared "int foo;").

So if I have something like this:

class A
{
public:
static void foo();
};
class B : public A
{
public:
static void foo(); // override
};

A *a = new B();

a->foo();

Does the last line call A::foo() or B::foo() ?

Share this post


Link to post
Share on other sites
Sharlin    864
A::foo(), just the same way as it would if foo was a non-virtual non-static member function. And no, you can't have virtual static functions :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this