virtual-functions

What's the advantage of this indirect function call?

情到浓时终转凉″ 提交于 2019-12-04 03:36:09
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; }; 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++ FAQ Lite says here and here . The primary advantage of NVI is separating interface from implementation. A

Hide virtual function with non-virtual override

核能气质少年 提交于 2019-12-04 02:59:27
问题 Having #include <iostream> using namespace std; class A { public: virtual void foo() { cout << "A" << endl; } }; class B : public A { public: void foo() { cout << "B" << endl; } }; class C : public B { public: void foo() { cout << "C" << endl; } }; int main() { C c; B* b = &c; b->foo(); return 0; } The output is C , but I expected B . I didn't declare B::foo() with the virtual modifier, so I expect the function call to be determined by the static type (no polymorphism). Why is C::foo() being

Visitor and templated virtual methods

时间秒杀一切 提交于 2019-12-04 02:20:15
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 accept_visitor(Visitor_Base& visitor) = 0; }; // More forward declarations struct Base_Int; struct Base

Resolution of virtual function with default parameters [duplicate]

只愿长相守 提交于 2019-12-04 01:26:57
问题 This question already has answers here : Can virtual functions have default parameters? (6 answers) Closed 5 years ago . header.h #include <iostream> using namespace std; class A { public: virtual void display(int i=5) { cout<< "Base::" << i << endl; } }; class B : public A { public: void display(int i=9) { cout<< "Derived::" << i << endl; } }; source.h #include <iostream> #include "header.h" using namespace std; int main() { A * a = new B(); a->display(); A* aa = new A(); aa->display(); B*

What's the issue with malloc() and virtual functions? [duplicate]

不想你离开。 提交于 2019-12-04 01:26:26
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: C++: why is new needed? Why cant I use malloc to allocate space for my objects when they are children of a class containing virtual functions? This is really frustrating. Is there a good reason? The following program illustrates the problem. It segfaults on line 27, where I call aa->f() #include <iostream> #include <cstdlib> class A { public: virtual int f() {return 1;} }; class B { public: int f() {return 1;} }

Why does this virtual destructor trigger an unresolved external?

坚强是说给别人听的谎言 提交于 2019-12-04 01:03:40
Consider the following: In X.h: class X { X(); virtual ~X(); }; X.cpp: #include "X.h" X::X() {} Try to build this (I'm using a .dll target to avoid an error on the missing main, and I'm using Visual Studio 2010): Error 1 error LNK2001: unresolved external symbol "private: virtual __thiscall X::~X(void)" (??1X@@EAE@XZ) Small modifications result in a successful build, however: X.h: class X { inline X(); // Now inlined, and everything builds virtual ~X(); }; or X.h: class X { X(); ~X(); // No longer virtual, and everything builds }; What causes the unresolved external in the linker when the

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

白昼怎懂夜的黑 提交于 2019-12-03 23:22:23
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(*cstream, fn); The files for which the condition is true are processed fine; the ones for which it is false

Accessing subclass members from a superclass pointer C++

六眼飞鱼酱① 提交于 2019-12-03 16:50:47
I have an array of custom class Student objects. CourseStudent and ResearchStudent both inherit from Student, and all the instances of Student are one or the other of these. I have a function to go through the array, determine the subtype of each Student, then call subtype-specific member functions on them. The problem is, because these functions are not overloaded, they are not found in Student, so the compiler kicks up a fuss. If I have a pointer to Student, is there a way to get a pointer to the subtype of that Student? Would I need to make some sort of fake cast here to get around the

How to dynamically load a C# dll from a C++ DLL

限于喜欢 提交于 2019-12-03 13:43:47
I have a C++ application. This supports users' C++ plugin DLL's, it will dynamically load these DLL's and then be able to create and use the user's types dynamically. These user types derive from base types and interfaces defined in the main application's core library, so I hold user's objects as pointers to the base class and call the user's virtual functions to make their magic happen. Now I want to extend the plugin DLL's to allow managed DLL's (I care about C# mostly). I want all of the same magic to happen in C# plugin DLL's. How can I dynamically load these dll's, some how I think win32

C++ how to call method in derived class from base class

倖福魔咒の 提交于 2019-12-03 13:26:18
What I want to do is for Execute() to run and completes it calls the Base::Done() then calls the Derived::Done() . I'm doing this because Base class Execute will do something and when its done call the Derived::Done() . I hope I'm explaining it correctly. Kind of like a listener that is called when a task completed. I'm kinda stuck on how the Base class will call the Derived class. class Base { virtual void Done(int code){}; void Execute(); } void Base::Execute() { } class Derived : Base { void Done(int code); void Run(); } Derived::Done(int code) { } void Derived::Run() { Execute(); } You can