I am working on a project and I am facing a little bit strange code which I cannot understand why and how can this happen !

I have a class Foo and Baz, and Foo has a non-static method that is called from Baz class without instantiating Foo:

class Foo {
    public:
       void qux(int a, int b);
};


class Baz {
    public:
        void bar(void);
};


void Baz::bar(void){
    Foo::qux(2,3);          // This should not happen as qux is not a static method !!
}
有帮助吗?

解决方案

The only way that would work is if Baz was derived from Foo at some level.

Or, of course, Foo bears a different meaning in that scope (via a using, typedef, define or other).

If neither apply, your compiler is seriously broken.

其他提示

This can happen in case of Baz is inherited from Foo.

In this case you can call method of the base class directly in the form that you mentioned:

void Baz::bar(void){
    Foo::qux(2,3);
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top