virtual-functions

Should I always use the override contextual keyword?

余生颓废 提交于 2019-12-08 19:23:34
问题 I know that the override contextual keyword was introduced to write safer code (by checking for a virtual function with the same signature) but I don't feel good about it, because it seems to be redundant for me to write override every time I want to override a virtual function. Is it a bad practice to not use override contextual keyword for 99% of cases? Why/when should I have to use it ( a compiler warning is not enough when we are hiding a virtual function mistakenly )? EDIT: In other

How to detect if virtual method is overridden in c# [duplicate]

↘锁芯ラ 提交于 2019-12-08 15:13:35
问题 This question already has answers here : Detect if a method was overridden using Reflection (C#) (8 answers) Closed 3 years ago . Is it possible to determine if a virtual method has been overridden: class ABase { public void DoSomething(object p) { p.Process(); if( /* DoSomethingExtra is implemented */ ) DoSomethingExtra(p); } public virtual void DoSomethingExtra(object p) { } } class ADerived { public override void DoSomethingExtra(object p) { p.ProcessMore(); } } I realize that this example

Pure Virtual Methods with Different Datatypes

淺唱寂寞╮ 提交于 2019-12-08 13:18:43
问题 I'm making a base class for my container classes to derive from so I can maintain a consistent interface. It currently looks something like this: template <typename Datatype> class BaseClass { public: virtual Datatype Foo() = 0; virtual Datatype Bar() = 0; }; template <typename Datatype> class DerivedClass: public BaseClass<Datatype> { public: virtual Datatype Foo() { } virtual Datatype Bar() { } }; However, with some of my derived classes, Foo() and Bar() may need to have different return

Does Objective-C have something like C++ virtual functions?

守給你的承諾、 提交于 2019-12-08 09:06:39
问题 In objective-c it is possible to add a @dynamic to a property. Is this also possible for normal instance methods? EDIT I think i wasn't clear enough. I want to do the following: @interface MyClass @property (retain) NSObject *somePropertyObject; - (void) myMethod; @end @implementation MyClass @dynamic somePropertyObject; //Make myMethod dynamic. I do not want to implement it. Like C++ Virtual @end 回答1: I think you might be asking how to declare a method that will be implemented some time

implement a virtual method from the base class as derived in static

☆樱花仙子☆ 提交于 2019-12-08 07:58:28
问题 I have an abstract base class with a virtual method. In the derived class, this method is implemented. However, I want the function in the derived class as a static method in order to be able to call the function without instantiating an object of that class. class Base { virtual double Foo(double rParam) const; }; class Derived1 : public Base { static double Foo(double rParam); }; class Derived2 : public Base { static double Foo(double rParam); }; Essentially, Derived1 and Derived2 provide

Why derived class does not have the vtable pointer and used instead vtable of the base class?

梦想的初衷 提交于 2019-12-08 07:39:00
问题 I am interested in the implementation of a virtual function in pure C. Here an example of the implementation. Then the implementation of the derived class through a pointer to the virtual functions table of the base class. Why derived class does not have the vtable pointer and used instead vtable of the base class. Maybe because they are the same offset ? void myClassDerived_ctor(struct myClassDerived *this) { myClassBase_ctor(&this->base); this->base.vtable = (void*)&myClassDerived_vtable +

Can CRTP completely replace virtual functionality for smaller designs?

喜欢而已 提交于 2019-12-08 01:01:47
问题 Is CRTP capable enough to outsmart virtual functionality completely ? The only disadvantage I see with CRTP is notable amount of code generated for every recurring pattern. For smaller designs, (where 2-3 classes are derived from a base), is CRTP a better idea ? 回答1: CRTP does not provide runtime polymorphism . If you need runtime polymorphism, you need virtual methods. Worse, since the base class is templated, you can't even really use the subclass objects as if they were of the same type as

Debug Assertion Failed! Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)

若如初见. 提交于 2019-12-08 00:57:50
问题 I know this problem is assessed many times on these forums, but they really are unique to their specific cases most times. This is a project for a class (on C++ no less), and the point of the project was to remake the classic board game Reversi. I have toiled through code for hours and finally made a program that will work, or so I thought! The big problem I am having seems to come from my deconstructor as it's giving me this error many of us have seen. My code is posted below and from my own

Virtual constructor idiom and factory design

∥☆過路亽.° 提交于 2019-12-07 17:29:46
问题 In virtual constructor idiom there are virtual functions which returns new object OR copy of the object using virtual functions. But then to call these virtual functions polymorphic way, you must have object of that class created using actual constructor. In design pattern context, it means client is aware of the type of object before using polymorphic way of object creation? 回答1: The client doesn't necessarily have to be aware of the concrete type. For example, consider this hierarchy:

How to get every virtual function index just as the compiler does?

只愿长相守 提交于 2019-12-07 08:00:44
问题 Is there some plugin or tool which can read a .h file (or simply modify Intellisense itself) and spit out every function and it's virtual function table index? There's a pattern which I have yet to figure out having to do with polymorphism, and it gets 5x harder when you start to have 5 classes or more deriving from each other. No matter what, though, the MSVC++ compiler always spits out the correct virtual function table index when it compiles the virtual function call from C++ to Assembly.