【发布时间】:2011-07-18 22:50:22
【问题描述】:
我想打印两个不同的东西,具体取决于函数是使用Foo::print() 静态调用还是从Foo foo; foo.print(); 的实例调用
编辑:这是一个绝对行不通的类定义,已经有几个人回答了。
class Foo {
string bla;
Foo() { bla = "nonstatic"; }
void print() { cout << bla << endl; }
static void print() { cout << "static" << endl; }
};
但是,有没有什么好的方法可以达到这个效果呢?基本上,我想做:
if(this is a static call)
do one thing
else
do another thing
换一种说法,我知道 PHP 可以检查 *this 变量是否已定义,以确定函数是否被静态调用。 C++ 有同样的能力吗?
【问题讨论】:
-
print()的两个版本具有相同的签名。而且我认为它们不能以这种方式超载。
标签: c++ static overloading non-static