class Base{
public:
void counter();
....
}
class Dervied: public Base{
public:
....
}
void main()
{
Base *ptr=new Derived;
ptr->counter();
}
Run Code Online (Sandbox Code Playgroud)
为了识别基类指针指向派生类并使用派生成员函数,我们使用"虚拟".
同样,我们可以使派生数据成员"虚拟"吗?(数据成员是公开的)
我有一些代码使用调用此代码的类的'this'指针.例如:
Some::staticFunction<templateType>(bind(FuncPointer, this, _1));
Run Code Online (Sandbox Code Playgroud)
这是我从boost调用bind函数.但没关系.现在我必须包装此代码.我做了一个宏:
#define DO(Type, Func) Some::staticFunction<Type>(bind(FuncPointer, this, _1));
Run Code Online (Sandbox Code Playgroud)
并且编译器将此代码插入到调用此宏的类中,因此'this'来自调用者.但我不想使用宏和首选功能(内联).但如何解决'这'传递.我可以在内联函数中使用它,如在宏中,或者我必须手动传递它吗?
是否有可能代替虚拟函数具有虚拟变量?
class B { virtual int fn(); /*virtual int val;*/ };
class D1: public B { virtual int fn() {return 0;};
/*int D1::val = 0;*/
class D2: public B { virtual int fn() {return 3;};
/*int D2::val = 3;*/
Run Code Online (Sandbox Code Playgroud)
现在我正在写作,b->fn()因为我不知道如何拥有一个我认为会更有效的虚拟变量(b->val).在C++中有可能吗?基本上我想在vtable中有一个const变量而不是const函数指针.