相关疑难解决方法(0)

C++使用非静态函数重载静态函数

我想打印两个不同的东西,这取决于一个函数是静态调用Foo::print()还是来自一个实例Foo foo; foo.print();

编辑:这是一个绝对不起作用的类定义,已经有几个人回答了.

class Foo {
    string bla;
    Foo() { bla = "nonstatic"; }

    void print() { cout << bla << endl; }
    static void print() { cout << "static" << endl; }
};
Run Code Online (Sandbox Code Playgroud)

但是,有没有一种很好的方法来实现这种效果?基本上,我想做:

if(this is a static call)
    do one thing
else
    do another thing
Run Code Online (Sandbox Code Playgroud)

换句话说,我知道PHP可以检查是否*this定义了变量,以确定是否静态调用该函数.C++是否具有相同的功能?

c++ static overloading non-static

36
推荐指数
1
解决办法
2万
查看次数

标签 统计

c++ ×1

non-static ×1

overloading ×1

static ×1