virtual-functions

Using reflection to override virtual method tables in C#

£可爱£侵袭症+ 提交于 2019-12-07 07:51:33
问题 Is there a way to change the virtual methods tables in C#? like change where a virtual method is pointing? class A { public virtual void B() { Console.WriteLine("B"); } } class Program { public static void MyB(A a) { Console.WriteLine("MyB"); } public static void Main(string[] Args) { A a = new A(); // Do some reflection voodoo to change the virtual methods table here to make B point to MyB a.B(); // Will print MyB } } 回答1: Take a look at LinFu. On Linfu's author's Blog there's an example of

Implement two functions with the same name but different, non-covariant return types due to multiple abstract base classes

六眼飞鱼酱① 提交于 2019-12-07 03:16:34
问题 If I have two abstract classes defining a pure virtual function with the same name, but different, non-covariant return types, how can I derive from these and define an implementation for both their functions? #include <iostream> class ITestA { public: virtual ~ITestA() {}; virtual float test() =0; }; class ITestB { public: virtual ~ITestB() {}; virtual bool test() =0; }; class C : public ITestA, public ITestB { public: /* Somehow implement ITestA::test and ITestB::test */ }; int main() {

C++: is a class with virtual base but without virtual functions polymorphic and has VTable?

三世轮回 提交于 2019-12-07 03:13:09
问题 Consider the following code: #include <iostream> #include <typeinfo> #include <type_traits> using namespace std; struct A { int data; }; struct B1 : A {}; struct B2 : virtual A {}; struct Base1 : virtual A {}; struct Base2 : virtual A {}; struct Derived : Base1, Base2 {}; int main() { cout << sizeof(B1) << endl; cout << sizeof(B2) << endl; cout << sizeof(Derived) << endl; cout << std::is_polymorphic<B1>::value << endl; cout << std::is_polymorphic<B2>::value << endl; cout << std::is

Question with virtual functions

谁说我不能喝 提交于 2019-12-07 02:33:55
问题 I have two classes: class x { public: virtual void hello() { std::cout << "x" << std::endl; } }; class y : public x { public: void hello() { std::cout << "y" << std::endl; } }; Can someone explain why the following two calls to hello() print different messages? Why don't they both print "y"? Is it because the first one is a copy while the second one actually points to the object in memory? int main() { y a; x b = a; b.hello(); // prints x x* c = &a; c->hello(); // prints y return 0; } 回答1:

overriding a function c#

混江龙づ霸主 提交于 2019-12-07 02:05:49
问题 I was just trying to master the concept of virtual function using a console app. I noticed as soon as I override a base class function, return baseclassname.functionname(parameters) gets inserted in my function body automatically. Why does this happen? class advanc { public virtual int calc (int a , int b) { return (a * b); } } class advn : advanc { public override int calc(int a, int b) { //automatically inserted return base.calc(a, b); } } 回答1: By overriding a virtual function you expand

Can a virtual function be overridden by a non-virtual function?

倾然丶 夕夏残阳落幕 提交于 2019-12-07 01:54:19
问题 In this code: class Base { public: virtual void method() = 0; }; class Derived1 : public Base{ public: virtual void method() override { } }; class Derived2 : public Base{ public: void method() override { } }; Is there any difference between Derived1 and Derived2 ? 回答1: From section 10.3 Virtual functions of the c++11 standard (draft n3337) point 2: If a virtual member function vf is declared in a class Base and in a class Derived, derived directly or indirectly from Base, a member function vf

Overloaded virtual function call resolution

时光毁灭记忆、已成空白 提交于 2019-12-07 01:35:42
问题 Please consider the following code: class Abase{}; class A1:public Abase{}; class A2:public A1{}; //etc class Bbase{ public: virtual void f(Abase* a); virtual void f(A1* a); virtual void f(A2* a); }; class B1:public Bbase{ public: void f(A1* a); }; class B2:public Bbase{ public: void f(A2* a); }; int main(){ A1* a1=new A1(); A2* a2=new A2(); Bbase* b1=new B1(); Bbase* b2=new B2(); b1->f(a1); // calls B1::f(A1*), ok b2->f(a2); // calls B2::f(A2*), ok b2->f(a1); // calls Bbase::f(A1*), ok b1->f

Behavior of virtual function in C++

拥有回忆 提交于 2019-12-06 20:33:20
问题 I have a question, here are two classes below: class Base{ public: virtual void toString(); // generic implementation } class Derive : public Base{ public: ( virtual ) void toString(); // specific implementation } The question is: If I wanna subclass of class Derive perform polymophism using a pointer of type Base , is keyword virtual in the bracket necessary? If the answer is no, what's the difference between member function toString of class Derive with and without virtual? 回答1: C++03 §10.3

QWidget keyPressEvent override

柔情痞子 提交于 2019-12-06 17:01:19
问题 I'm trying for half an eternity now overriding QWidgets keyPressEvent function in QT but it just won't work. I've to say i am new to CPP, but I know ObjC and standard C. My problem looks like this: class QSGameBoard : public QWidget { Q_OBJECT public: QSGameBoard(QWidget *p, int w, int h, QGraphicsScene *s); signals: void keyCaught(QKeyEvent *e); protected: virtual void keyPressEvent(QKeyEvent *event); }; QSGameBoard is my QWidget subclass and i need to override the keyPressEvent and fire a

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

感情迁移 提交于 2019-12-06 16:16:11
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 different implementations of a static function (that do not depend on object data), but I want those