virtual-functions

Question with virtual functions

半城伤御伤魂 提交于 2019-12-05 05:07:44
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; } Yes, you are right x b = a; Invokes a copy constructor (b IS an 'x') x& b = a; Assigns a reference and will

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

谁都会走 提交于 2019-12-05 04:43:44
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" #include <string.h> #include <iostream> componente::componente() { num_piezas = 0; codigo = 0; strcpy

Interface, Abstract, or just virtual methods?

狂风中的少年 提交于 2019-12-05 03:34:29
I have a bunch of systems, lets call them A, B, C, D, E, F, G, H, I, J . They all have similar methods and properties. Some contain the exact same method and properties, some may vary slightly and some may vary a lot. Right now, I have a lot of duplicated code for each system. For example, I have a method called GetPropertyInformation() that is defined for each system. I am trying to figure out which method would be the best approach to reduce duplicate code or maybe one of the methods below is not the way to go: Interface public Interface ISystem { public void GetPropertyInformation(); /

Is an object allowed to legally change its type during its lifetime in C++?

孤者浪人 提交于 2019-12-05 02:14:19
I have this code: class Class { public: virtual void first() {}; virtual void second() {}; }; Class* object = new Class(); object->first(); object->second(); delete object; that I compile with Visual C++ 10 with /O2 and have this disassembly: 282: Class* object = new Class(); 00403953 push 4 00403955 call dword ptr [__imp_operator new (4050BCh)] 0040395B add esp,4 0040395E test eax,eax 00403960 je wmain+1Ch (40396Ch) 00403962 mov dword ptr [eax],offset Class::`vftable' (4056A4h) 00403968 mov esi,eax 0040396A jmp wmain+1Eh (40396Eh) 0040396C xor esi,esi 283: object->first(); 0040396E mov eax

Virtual function inheritance

烂漫一生 提交于 2019-12-05 01:41:56
I have a confusion about the inheriting the virtual property of a method. Let's suppose we have 4 classes: class A, class B, class C and class D. The classes are inherited by this way: A -> B -> C -> D, where A is the base class. By this time, I'm sure about this: Beginning the class method declaration with virtual in a base class (class A), makes the method virtual for all classes derived from the base class, including the derived ones of the derived classes. (B and C class methods determined to be virtual). The confusion is here. What if, in the base class A, there wouldn't be any virtual

Behavior of virtual function in C++

人盡茶涼 提交于 2019-12-05 01:26:29
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? C++03 §10.3/2: If a virtual member function vf is declared in a class Base and in a class Derived, derived directly or

Changing the params modifier in a method override

拜拜、爱过 提交于 2019-12-05 00:49:44
I'm aware that a params modifier (which turns in one parameter of array type into a so-called "parameter array") is specifically not a part of the method signature. Now consider this example: class Giraffid { public virtual void Eat(int[] leaves) { Console.WriteLine("G"); } } class Okapi : Giraffid { public override void Eat(params int[] leaves) { Console.WriteLine("O"); } } This compiles with no warnings. Then saying: var okapi = new Okapi(); okapi.Eat(2, 4, 6); // will not compile! gives an error( No overload for method 'Eat' takes 3 arguments ). Now, I know that the compiler translates the

C++ override private pure virtual method as public

我与影子孤独终老i 提交于 2019-12-04 23:40:42
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? 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, const/volatile-ness and ref qualifier. It doesn't care about return type, access modifier or other things

QWidget keyPressEvent override

 ̄綄美尐妖づ 提交于 2019-12-04 22:38:50
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 SIGNAL on each event to notify some registered objects. My overridden keyPressEvent in QSGameBoard.cpp

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

大城市里の小女人 提交于 2019-12-04 22:31:29
问题 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 {