virtual

Calling base class definition of virtual member function with function pointer

狂风中的少年 提交于 2019-11-26 21:48:23
问题 I want to call the base class implementation of a virtual function using a member function pointer. class Base { public: virtual void func() { cout << "base" << endl; } }; class Derived: public Base { public: void func() { cout << "derived" << endl; } void callFunc() { void (Base::*fp)() = &Base::func; (this->*fp)(); // Derived::func will be called. // In my application I store the pointer for later use, // so I can't simply do Base::func(). } }; In the code above the derived class

How to achieve “virtual template function” in C++

走远了吗. 提交于 2019-11-26 21:41:15
first off: I have read and I know now that a virtual template member function is not (yet?) possible in C++. A workaround would be to make the class a template and then use the template-argument also in the member-function. But in the context of OOP, I find that the below example would not be very "natural" if the class was actually a template. Please note that the code is actually not working, but the gcc-4.3.4 reports: error: templates may not be ‘virtual’ #include <iostream> #include <vector> class Animal { public: template< class AMOUNT > virtual void eat( AMOUNT amount ) const { std::cout

gof-原型模式

冷暖自知 提交于 2019-11-26 20:45:54
原型模式:基于类的设计方案最终会出现   与 面向过程 相比 面向对象 的特性是将状态(数据)和行为(逻辑)绑定在一起,基于类的OOP设计方案会有些令人不舒服的设计 引用场景:规避代码冗余,简化行为操作 例子: <-符号表示继承 class Monster {   //... }; class Ghost : public Monster {}; class Demon : public Monster {}; class Sorcerer : public Monster {}; class Spawner { public:   virtual ~Spawner() {}   virtual Monster* spawnMonster() = 0; }; class GhostSpawner : public Spawner { public:   virtual Monster* spawnMonster()   {     return new Ghost();   } }; class DemonSpawner : public Spawner { public:   virtual Monster* spawnMonster()   {     return new Demon();   } }; // 你懂的 提供一个生成器: typedef Monster* (

观察者模式(Observer Pattern)

依然范特西╮ 提交于 2019-11-26 20:31:09
观察者模式(Observer Pattern) —— 定义对象间的一种一对多的依赖关系,以便当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并自动刷新。 观察者模式可以理解为发布-订阅模式,即多个订阅者(观察者)向发布者(被观察者)订阅状态信息,当发布者更新状态时会将状态信息向它的订阅者发布信息。发布者需要自己维护订阅者列表,可以注册或者注销对状态信息感兴趣或不感兴趣的订阅者。 // subject.h #include<vector> #include<string> using namespace std; typedef int State; class Observer; // 被观察者(发布者)抽象 class Subject { public: Subject(){} virtual ~Subject(){} virtual void Attach(Observer* obv); // 注册观察者 virtual void Detach(Observer* obv); // 注销观察者 virtual void Notify(); // 通知观察者 virtual void SetState(const State& st) = 0; virtual State GetState() = 0; private: //观察者列表 vector<Observer*>

C++ Virtual function being hidden

不羁的心 提交于 2019-11-26 20:13:55
问题 I'm having a problem with C++ inheritance. I have a class hierarchy: class A { public: virtual void onFoo() {} virtual void onFoo(int i) {} }; class B : public A { public: virtual void onFoo(int i) {} }; class C : public B { }; int main() { C* c = new C(); c->onFoo(); //Compile error - doesn't exist } My question is: why doesn't this compile? My understanding is that C should inherit both onFoo functions from A - and in fact, this compiles if you remove the redefinition of onFoo in B - but g+

Alternative to c++ static virtual methods

拟墨画扇 提交于 2019-11-26 19:34:51
问题 In C++ is not possible to declare a static virtual function, neither cast a non-static function to a C style function pointer. Now, I have a plain ol' C SDK that uses function pointers heavily. I have to fill a structure with several function pointers. I was planning to use an abstract class with a bunch of static pure virtual methods, and redefine them in derived classes and fill the structure with them. It wasn't until then that I realized that static virtual are not allowed in C++. Also

Can I get polymorphic behavior without using virtual functions?

∥☆過路亽.° 提交于 2019-11-26 18:25:33
问题 Because of my device I can't use virtual functions. Suppose I have: class Base { void doSomething() { } }; class Derived : public Base { void doSomething() { } }; // in any place { Base *obj = new Derived; obj->doSomething(); } the obj->doSomething() will call just the Base::doSomething() Is there a way with Base *obj , to call the doSomething of the Derived ? I know I can just put a virtual before doSomething() of Base it solve the problem, but I'm limited by my device, the compiler doesn't

【转】重载(overload)、覆盖(override)、隐藏(hide) 辨析

不问归期 提交于 2019-11-26 17:42:55
对这些概念老是记不清楚,转了一篇比较通俗的文章帮助记忆,高手请绕道^_^~ 写正题之前,先给出几个关键字的中英文对照,重载(overload),覆盖(override),隐藏(hide)。在早期的C++书籍中,可能翻译的人不熟悉专业用语(也不能怪他们,他们不是搞计算机编程的,他们是英语专业的),常常把重载(overload)和覆盖(override)搞错! 我们先来看一些代码及其编译结果。 实例一:     #include "stdafx.h" #include class CB { public: void f(int) { cout << "CB::f(int)" << endl; } };   class CD : public CB { public: void f(int,int) { cout << "CD::f(int,int)" << endl; } void test() { f(1); } };  int main(int argc, char* argv[]) { return 0; } 编译了一下 error C2660: 'f' : function does not take 1 parameters 结论:在类CD这个域中,没有f(int)这样的函数,基类中的void f(int)被隐藏 。 如果把派生CD中成员函数void f(int,int

Calling virtual function from destructor

醉酒当歌 提交于 2019-11-26 17:42:49
Is this safe ? class Derived: public PublicBase, private PrivateBase { ... ~Derived() { FunctionCall(); } virtual void FunctionCall() { PrivateBase::FunctionCall(); } } class PublicBase { virtual ~PublicBase(){}; virtual void FunctionCall() = 0; } class PrivateBase { virtual ~PrivateBase(){}; virtual void FunctionCall() { .... } } PublicBase* ptrBase = new Derived(); delete ptrBase; This code crases sometimes with IP in a bad address. That is not a good idea to call a virtual function on constructor is clear for everyone. From articles like http://www.artima.com/cppsource/nevercall.html I

What are Virtual Methods?

蹲街弑〆低调 提交于 2019-11-26 17:28:09
问题 Why would you declare a method as "virtual". What is the benefit in using virtual? 回答1: The Virtual Modifier is used to mark that a method\property(ect) can be modified in a derived class by using the override modifier. Example: class A { public virtual void Foo() //DoStuff For A } class B : A { public override void Foo() //DoStuff For B //now call the base to do the stuff for A and B //if required base.Foo() } 回答2: Virtual allows an inheriting class to replace a method that the base class