我想打印两个不同的东西,这取决于一个函数是静态调用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++是否具有相同的功能?
struct TimerEvent
{
event Event;
timeval TimeOut;
static void HandleTimer(int Fd, short Event, void *Arg);
};
Run Code Online (Sandbox Code Playgroud)
HandleTimer需要是静态的,因为我将它传递给C库(libevent).
我想继承这门课.如何才能做到这一点?
谢谢.