virtual-functions

How do I suppress C++ vtable generation for pure virtual classes using G++?

不羁岁月 提交于 2020-01-03 08:58:19
问题 Supressing C++ vtable generation can be done in MSVC using the __declspec(novtable) attribute. However, it seems that there is no equivalent attribute for the GNU C++ compiler. The fact is that leaving the vtables for pure virtual classes unnecessarily links in __cxa_abort() and many others, and I want to avoid this happening because I'm programming for an embedded system. So, what should I do? struct ISomeInterface { virtual void Func() = 0; }; class CSomeClass : public ISomeInterface {

What's the advantage of this indirect function call?

若如初见. 提交于 2020-01-01 09:07:13
问题 I found the following code in a library: class Bar { public: bool foo(int i) { return foo_(i); } private: virtual bool foo_(int i) = 0; }; Now I'm wondering: Why would you use this indirection? Could there be any reasons why the above would be better than the simple alternative: class Bar { public: virtual bool foo(int i) = 0; }; 回答1: This is the Non-Virtual Interface Idiom (NVI). That page by Herb Sutter has a good bit of detail about it. However, temper what you read there with what the C++

Visitor and templated virtual methods

拜拜、爱过 提交于 2020-01-01 08:37:52
问题 In a typical implementation of the Visitor pattern, the class must account for all variations (descendants) of the base class. There are many instances where the same method content in the visitor is applied to the different methods. A templated virtual method would be ideal in this case, but for now, this is not allowed. So, can templated methods be used to resolve virtual methods of the parent class? Given (the foundation): struct Visitor_Base; // Forward declaration. struct Base { virtual

C++ calling completely wrong (virtual) method of an object

╄→尐↘猪︶ㄣ 提交于 2020-01-01 07:52:50
问题 I have some C++ code (written by someone else) which appears to be calling the wrong function. Here's the situation: UTF8InputStreamFromBuffer* cstream = foo(); wstring fn = L"foo"; DocumentReader* reader; if (a_condition_true_for_some_files_false_for_others) { reader = (DocumentReader*) _new GoodDocumentReader(); } else { reader = (DocumentReader*) _new BadDocumentReader(); } // the crash happens inside the following call // when a BadDocumentReader is used doc = reader->readDocument(

C++ calling completely wrong (virtual) method of an object

只谈情不闲聊 提交于 2020-01-01 07:52:05
问题 I have some C++ code (written by someone else) which appears to be calling the wrong function. Here's the situation: UTF8InputStreamFromBuffer* cstream = foo(); wstring fn = L"foo"; DocumentReader* reader; if (a_condition_true_for_some_files_false_for_others) { reader = (DocumentReader*) _new GoodDocumentReader(); } else { reader = (DocumentReader*) _new BadDocumentReader(); } // the crash happens inside the following call // when a BadDocumentReader is used doc = reader->readDocument(

Invoking virtual function and pure-virtual function from a constructor

你离开我真会死。 提交于 2019-12-30 04:56:05
问题 When i invoke a virtual function from a base constructor, the compiler does not give any error. But when i invoke a pure-virtual function from the base class constructor, it gives compilation error. Consider the sample program below: #include <iostream> using namespace std; class base { public: void virtual virtualfunc() = 0; //void virtual virtualfunc(); base() { virtualfunc(); } }; void base::virtualfunc() { cout << " pvf in base class\n"; } class derived : public base { public: void

Difference between calling of virtual function and non virtual function?

浪子不回头ぞ 提交于 2019-12-30 03:11:27
问题 This is in fact an interview question, I can't figure out the answer. Anyone knows about this? You can talk about any difference, for example, the data that are push into stack. 回答1: Though virtualism/dynamic dispatch is strictly implementation defined, most(read all known ) compilers implement it by using vptr and vtable . Having said that, the difference between calling a non virtual function and virtual function is: Non-virtual functions are resolved statically at Compile-time , While

How to explain this behaviour with Overloaded and Overridden Methods? [duplicate]

佐手、 提交于 2019-12-30 03:03:56
问题 This question already has answers here : Overload resolution and virtual methods (5 answers) Closed 5 years ago . Could anyone be so nice and explain me why this code shows Derived.DoWork(double) . I can come up with some explanations for this behaviour, however I want someone to clarify this for me. using System; public class Base { public virtual void DoWork(int param) { Console.WriteLine("Base.DoWork"); } } public class Derived : Base { public override void DoWork(int param) { Console

How to explain this behaviour with Overloaded and Overridden Methods? [duplicate]

China☆狼群 提交于 2019-12-30 03:03:14
问题 This question already has answers here : Overload resolution and virtual methods (5 answers) Closed 5 years ago . Could anyone be so nice and explain me why this code shows Derived.DoWork(double) . I can come up with some explanations for this behaviour, however I want someone to clarify this for me. using System; public class Base { public virtual void DoWork(int param) { Console.WriteLine("Base.DoWork"); } } public class Derived : Base { public override void DoWork(int param) { Console

Virtual Table C++

淺唱寂寞╮ 提交于 2019-12-29 14:17:11
问题 I read a lot of people writing "a virtual table exists for a class that has a virtual function declared in it". My question is, does a vtable exists only for a class that has a virtual function or does it also exist for classes derived from that class. e.g class Base{ public: virtual void print(){cout<<"Base Print\n";} }; class Derived:public Base{ public: void print(){cout<<"Derived print\n";} }; //From main.cpp Base* b = new Derived; b->print(); Question: Had there been no vtable for class