virtual-functions

C++ base class pointer calls child virtual function, why could base class pointer see child class member

末鹿安然 提交于 2019-12-13 20:25:22
问题 I think I might be confusing myself. I know class with virtual functions in C++ has a vtable (one vtable per class type), so the vtable of Base class will have one element &Base::print() , while the vtable of Child class will have one element &Child::print() . When I declare my two class objects, base and child , base 's vtable_ptr will pointer to Base class's vtable, while child 's vtable_ptr will point to Child class's vtable. After I assign the address of base and child to an array of Base

boost python: how to call a C++ virtual function

别来无恙 提交于 2019-12-13 18:09:38
问题 I have python embedded in a C++ application. The C++ calls python and passes it as an argument a C++ object. that object has some virtual functions and can be a base class for some derived class. How do I make boost::python understand that it's a virtual function? consider the following: in C++: class Base { public: virtual void func(); } class Derived { public: virtual void func(); } BOOST_PYTHON_MODULE(module_api) { class_<Base>("Base") .def("func", &Base::func); // ?? what should I put

Comparison of Virtual Function Pointers in C++

邮差的信 提交于 2019-12-13 13:26:45
问题 Say I want to check to see whether a subclass has implemented one of it's parent's virtual functions (never mind whether this smells of bad architecture... it's an exercise). If I wanted to see if two regular functions were identical, I could just check &f == &g . // Plain old functions void f() {} void g() {} ... std::cout << "&f " << &f << "\n"; // "1" OK, for some reason func ptrs are converted std::cout << "&g " << &f << "\n"; // "1" to booleans when printed. I can dig it. std::cout << "

runtime-check whether an instance (Base*) override a parent function (Base::f())

烂漫一生 提交于 2019-12-12 20:06:53
问题 How to determine whether a pointer of base ( B ) class is (polymorphism-ly) override a certain virtual function of the base class? class B{ public: int aField=0; virtual void f(){}; }; class C : public B{ public: virtual void f(){aField=5;}; }; class D: public B{}; int main() { B b; C c; D d; std::vector<B*> bs; bs.push_back(&b); bs.push_back(&c); bs.push_back(&d); for(int n=0;n<3;n++){ //std::cout<<(bs[n]->f)==B::f<<std::endl; //should print "true false true" } } I tried to compare the

Inherit from two polymorphic classes

孤人 提交于 2019-12-12 19:18:36
问题 Given the following code class T { public: virtual ~T () {} virtual void foo () = 0; }; class U { public: U() {} ~U() {} void bar () { std::cout << "bar" << std::endl; } }; class A : public U, public T { public: void foo () { std::cout << "foo" << std::endl; } }; int main () { A * a = new A; std::vector<U*> u; std::vector<T*> t; u.push_back(a); t.push_back(reinterpret_cast<T*>(u[0])); u[0]->bar (); t[0]->foo (); delete a; return 0; } I get the output I would expect bar foo However, if I

C++: How is this technique of compile-time polymorphism called and what are the pros and cons?

試著忘記壹切 提交于 2019-12-12 17:02:47
问题 At work, I have come across code which basically looks like that: #include <iostream> using namespace std; enum e_Specialization { Specialization_A, Specialization_B }; template<e_Specialization> class TemplatedBase { public: string foo() { return "TemplatedBase::foo"; } }; template<> string TemplatedBase<Specialization_A>::foo() { return "TemplatedBase<Specialization_A>:foo"; } int main() { TemplatedBase<Specialization_A> o; cout << o.foo() << endl; return 0; } which outputs TemplatedBase

“Warning: Can't find linker symbol for virtual table for value XXX value” using GCC and GDB (CodeBlocks)

泪湿孤枕 提交于 2019-12-12 09:38:16
问题 I'm getting a runtime error ("memory can't be written") that, after inspection through the debugger, leads to the warning in the tittle. The headers are the following: componente.h: #ifndef COMPONENTE_H #define COMPONENTE_H using namespace std; class componente { int num_piezas; int codigo; char* proovedor; public: componente(); componente(int a, int b, const char* c); virtual ~componente(); virtual void print(); }; #endif // COMPONENTE_H complement.h implementation #include "Componente.h"

C++ override private pure virtual method as public

北慕城南 提交于 2019-12-12 08:21:10
问题 Why does this happen? http://coliru.stacked-crooked.com/a/e1376beff0c157a1 class Base{ private: virtual void do_run() = 0; public: void run(){ do_run(); } }; class A : public Base { public: // uplift ?? virtual void do_run() override {} }; int main() { A a; a.do_run(); } Why can I override a PRIVATE virtual method as public? 回答1: According to https://en.cppreference.com/w/cpp/language/virtual#In_detail overwriting a base's virtual member function only care about the function name, parameters,

Accessing subclass members from a superclass pointer C++

孤街浪徒 提交于 2019-12-12 08:13:56
问题 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

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

試著忘記壹切 提交于 2019-12-12 07:58:15
问题 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