virtual-functions

Contiguous storage of polymorphic types

我是研究僧i 提交于 2019-12-19 05:16:39
问题 I'm interested to know if there is any viable way to contiguously store an array of polymorphic objects, such that virtual methods on a common base can be legally called (and would dispatch to the correct overridden method in a subclass). For example, considering the following classes: struct B { int common; int getCommon() { return common; } virtual int getVirtual() const = 0; } struct D1 : B { virtual int getVirtual final const { return 5 }; } struct D2 : B { int d2int; virtual int

Why do virtual functions need to be passed with a pointer and not by value(of the object)?

試著忘記壹切 提交于 2019-12-18 16:53:20
问题 I think I understand the concept of virtual methods and vtables, but I don't understand why there is a difference between passing the object as a pointer(or reference) and passing it by value (which kind of scraps the vtable or something?) Why would something like this work: Material* m = new Texture; poly->setMaterial(m); // methods from Texture are called if I keep carrying the pointer around And not this?: Material m = Texture(); poly->setMaterial(m); // methods from Material are called if

Are there alternatives to polymorphism in C++?

你。 提交于 2019-12-18 16:32:16
问题 The CRTP is suggested in this question about dynamic polymorphism. However, this pattern is allegedly only useful for static polymorphism. The design I am looking at seems to be hampered speedwise by virtual function calls, as hinted at here. A speedup of even 2.5x would be fantastic. The classes in question are simple and can be coded completely inline, however it is not known until runtime which classes will be used. Furthermore, they may be chained, in any order, heaping performance insult

Why does virtual assignment behave differently than other virtual functions of the same signature?

老子叫甜甜 提交于 2019-12-18 11:47:12
问题 While playing with implementing a virtual assignment operator I have ended with a funny behavior. It is not a compiler glitch, since g++ 4.1, 4.3 and VS 2005 share the same behavior. Basically, the virtual operator= behaves differently than any other virtual function with respect to the code that is actually being executed. struct Base { virtual Base& f( Base const & ) { std::cout << "Base::f(Base const &)" << std::endl; return *this; } virtual Base& operator=( Base const & ) { std::cout <<

abstract classes in std containers

橙三吉。 提交于 2019-12-18 11:34:16
问题 Very often, when I program, I use polymorphism because it naturally models the objects that I need. On the other hand I very often use standard containers to store these objects, and I tend to avoid pointers because this either requires me to free up the objects instead of popping them off the stack or requires me to know for sure the objects will stay on the stack while I use the pointer. Of course there are all kinds of pointer-container objects that sort of do this task for you, but in my

Clone pattern for std::shared_ptr in C++

若如初见. 提交于 2019-12-18 09:05:13
问题 Why do you need (in order to make it compile) the intermediate CloneImplementation and std::static_pointer_cast (see Section 3 below) to use the Clone pattern for std::shared_ptr instead of something closer (see Section 2 below) to the use of raw pointers (see Section 1 below)? Because as far as I understand, std::shared_ptr has a generalized copy constructor and a generalized assignment operator? 1. Clone pattern with raw pointers : #include <iostream> struct Base { virtual Base *Clone()

How to override virtual function in good style? [C++]

好久不见. 提交于 2019-12-18 07:44:28
问题 guys I know this question is very basic but I've met in few publications (websites, books) different style of override virtual function. What I mean is: if I have base class: class Base { public: virtual void f() = 0; }; in some publications I saw that to override this some authors would just say: void f(); and some would still repeat the virtual keyword before void. Which form of overwriting is in good style? Thank you for your answers. 回答1: This is purely a matter of taste. Some weak

How to override virtual function in good style? [C++]

守給你的承諾、 提交于 2019-12-18 07:44:25
问题 guys I know this question is very basic but I've met in few publications (websites, books) different style of override virtual function. What I mean is: if I have base class: class Base { public: virtual void f() = 0; }; in some publications I saw that to override this some authors would just say: void f(); and some would still repeat the virtual keyword before void. Which form of overwriting is in good style? Thank you for your answers. 回答1: This is purely a matter of taste. Some weak

Virtual Functions: Iterating over a vector<Base Class> that is populated with subclass objects

天涯浪子 提交于 2019-12-18 06:12:37
问题 Short Description: I am iterating over a vector calling a virtual function on every object in the vector in order to execute a sequence of actions. The vector is of the base class as is the iterator. All the objects are children. When the virtual function is called it executes the base class's function. (Really) Long Description: I am attempting to model a creature that has a set of behaviors. My base class is abstract with only two functions (virtual) which all the subclasses have overridden

Differing return type for virtual functions

一曲冷凌霜 提交于 2019-12-18 05:47:05
问题 A virtual function's return type should be the same type that is in base class, or covariant. But why do we have this restriction? 回答1: Because of the nonsense that would ensue: struct foo { virtual int get() const { return 0; } }; struct bar : foo { std::string get() const { return "this certainly isn't an int"; } }; int main() { bar b; foo* f = &b; int result = f->get(); // int, right? ...right? } It isn't sensible to have a derived class return something completely unrelated. 回答2: Because