virtual

Operator== in derived class never gets called

风格不统一 提交于 2019-12-23 12:22:14
问题 Can someone please put me out of my misery with this? I'm trying to figure out why a derived operator== never gets called in a loop. To simplify the example, here's my Base and Derived class: class Base { // ... snipped bool operator==( const Base& other ) const { return name_ == other.name_; } }; class Derived : public Base { // ... snipped bool operator==( const Derived& other ) const { return ( static_cast<const Base&>( *this ) == static_cast<const Base&>( other ) ? age_ == other.age_ :

Virtual function compiler optimization c++

纵饮孤独 提交于 2019-12-23 10:07:51
问题 class Base { public: virtual void fnc(size_t nm) { // do some work here } void process() { for(size_t i = 0; i < 1000; i++) { fnc(i); } } } Can and will the c++ compiler optimize calls to the fnc function from the process funtion, considering its going to be the same function every time it's invoked inside the loop ? Or is it gonna fetch the function adress from the vtable every time the function is invoked ? 回答1: I checked an example on godbolt.org. the result is that NO, none of the

c++ virtual keyword vs overriding function

ε祈祈猫儿з 提交于 2019-12-23 07:58:15
问题 I am learning c++ and am learning about the virtual keyword. I have scoured the internet trying to understand it to no avail. I went into my editor and did the following experiment, expecting it to print out the base message twice (because I was under the impression that the virtual keyword is needed to override functions). However, it printed out two different messages. Can someone explain to me why we need the virtual keyword if we can simply override functions and still seemingly get

state pattern C++

时间秒杀一切 提交于 2019-12-23 07:49:13
问题 I'm trying to instigate a simple State pattern, after following some of the excellent tutorials here: http://gameprogrammingpatterns.com/state.html I am half way through this current tutorial, and I am trying to replicate the static instances of each state, by containing them within the base class. However, when it comes to switching states, g++ is throwing this error. state_test.cpp: In member function ‘virtual void Introduction::handleinput(Game&, int)’: state_test.cpp:55:16: error: cannot

C++, virtual inheritance, strange abstract class + clone problem

人盡茶涼 提交于 2019-12-23 04:53:35
问题 Sorry for the larger amount of the source code. There three abstract classes P, L, PL. The third class PL is derived from classes P and L using the virtual inheritance: template <typename T> //Abstract class class P { public: virtual ~P() = 0; virtual P <T> *clone() const = 0; }; template <typename T> P<T>::~P() {} template <typename T> P<T> * P <T>:: clone() const { return new P <T> ( *this ); } template <typename T> //Abstract class class L { public: virtual ~L() = 0; virtual L <T> *clone()

call sub class method from base class specific function

回眸只為那壹抹淺笑 提交于 2019-12-23 04:22:44
问题 I've got a question concerning handling of virtual function in C++ programming. I have something like this: template<class T> class baseClass { virtual void doSomething(T& t) { // some baseClass specific code } void doSomethingElse(T& t) { // some baseClass specific code this->doSomething(t); } } template<class T> class subClass { virtual void doSomething(T&) { // some subclass related code } } Now, if I construct an object of type subClass.... int main(int argc, char *argv[]) { subClass

组件协作模式之策略模式(Strategy)

淺唱寂寞╮ 提交于 2019-12-23 02:38:38
文章目录 一、概念 二、动机 三、源代码讲解 四、使用策略模式进行改进 五、类图结构 六、要点总结 一、概念    定义一系列算法,把他们一个个封装起来,并且使他们可以互相替换(变化<各个算法>) 。该模式使得算法可独立于使用它的客户程序(稳定<SalesOrder类>)而变化(扩展,子类化)。——《设计模式》GOF 二、动机    在软件构建过程中, 某些对象可能用到的算法多种多样,经常改变,如果将这些算法都编码到对象中,将会使得对象变得异常复杂 ;而且有时候支持不使用的算法也是一个性能负担。    如何在运行时根据需要透明地更改对象的算法? 将算法与对象本身解耦 ,从而避免上述问题? 三、源代码讲解 enum TaxBase { CN_Tax , US_Tax , DE_Tax , FR_Tax // 更改 } ; class SalesOrder { TaxBase tax ; public : double CalculateTax ( ) { //... if ( tax == CN_Tax ) { //CN*********** } else if ( tax == US_Tax ) { //US*********** } else if ( tax == DE_Tax ) { //DE*********** } else if ( tax == FR_Tax ) {

Cant copy construction be done without creating an explicit function in the pure virtual base class?

和自甴很熟 提交于 2019-12-22 18:32:36
问题 My objective is to do a deep copy of a class, but a virtual class is causing trouble. #include<iostream> using namespace std; class Vir//pure virtual class { public: virtual void hi()=0; }; class Handler:public Vir { public: int i; Handler() {} Handler(int val):i(val) {} void hi() {cout<<"Value of i="<<i<<endl;} int getI() const {return i;} void setI(int j) {i=j;} }; class ControlPanel { public: Vir *v; ControlPanel(const ControlPanel& c)//copy constructor { v=new Handler; v->setI(c.getI());

Virtual assignment operator overloading- how the correct overloaded function is chosen?

送分小仙女□ 提交于 2019-12-22 17:49:05
问题 The following code (from C++ FAQs 24.11) is implementing virtual assignment operator overloading and overriding: #include <iostream> using namespace std; class B{ public: virtual ~B() throw(); virtual B& operator= (const B& b) throw(); }; B::~B() throw(){} B& B::operator= (const B& b) throw() { cout << "B::operator=(const B&)\n"; return *this; } class D : public B{ public: virtual D& operator= (const B& b) throw(); D& operator= (const D& d) throw(); }; D& D::operator= (const B& b) throw()

Inheritance of virtual member functions with the same name

我只是一个虾纸丫 提交于 2019-12-22 17:42:47
问题 class A { A() {}; virtual ~A() {}; virtual void Start() {}; virtual void Start(float a) {}; }; class B : public A { }; class C : public A { virtual void Start(float a) {}; } ... B BObj; BObj.Start(); // -> fine, no complain from g++ ... ... C CObj; CObj.Start(); // -> not fine -> error: no matching function for call to ‘C::Start()’ ... I suspect that the problem comes from that both virtual functions have the same name, but different parameter signature. What I would like to know is that this