virtual-functions

Is it possible to break code by adding a new virtual function in the base class?

怎甘沉沦 提交于 2019-12-23 20:32:58
问题 Is it possible to have the observed behavior of a program changed by simply adding a new virtual function to a base class? I mean that no other change must be made to the code. 回答1: #include <stdlib.h> struct A { #if ADD_TO_BASE virtual void foo() { } #endif }; struct B : A { void foo() { } }; struct C : B { void foo() { abort(); } }; int main() { C c; B& b = c; b.foo(); } Without the virtual function the base class b.foo() is a non-virtual call to B::foo() : $ g++ virt.cc $ ./a.out With the

Does derived class' member functions inherit virtualness from base class?

不打扰是莪最后的温柔 提交于 2019-12-23 18:27:31
问题 Say we have the following two classes, A is the base class with virtual destructor and B is the derived class whose destructor doesn't have 'virtual' qualifier. My question is, if I going to derive more classes from B, will B's destructor automatically inherit the virtualness or I need to explicitly put 'virtual' before '~B() {...}' class A { public: A() { std::cout << "create A" << std::endl;}; virtual ~A() { std::cout << "destroy A" << std::endl;}; }; class B: A { public: B() { std::cout <<

Overriding virtual methods in PyGObject

懵懂的女人 提交于 2019-12-23 16:12:37
问题 I'm trying to implement the Heigh-for-width Geometry Management in GTK with Python for my custom Widget. My widget is a subclass from Gtk.DrawingArea and draws some parts of an Image. As I understood the GTK Docs (link above) I have to implement the following 4 methods: GtkWidgetClass.get_preferred_width() GtkWidgetClass.get_preferred_height() GtkWidgetClass.get_preferred_height_for_width() GtkWidgetClass.get_preferred_width_for_height() Now wondering where to implement this in Python. I

Can CUDA kernels be virtual functions?

会有一股神秘感。 提交于 2019-12-23 16:08:19
问题 The question is quite straighforward, but let me give an overview of my framework. I have an abstract class AbstractScheme representing a type of computation (a kind of discretization for an equation, but this is not important). Each implementation has to provide a method to return the name of the scheme and has to implement a protected function which is the CUDA kernel. The base abstract class provides a public method which calls the CUDA kernel and returns how long it took for the kernel to

Question on Virtual Methods

对着背影说爱祢 提交于 2019-12-23 12:35:41
问题 IF both methods are declared as virtual, shouldn't both instances of Method1() that are called be the derived class's Method1()? I am seeing BASE then DERIVED called each time. I am doing some review for an interview and I want to make sure I have this straight. xD class BaseClass { public: virtual void Method1() { cout << "Method 1 BASE" << endl; } }; class DerClass: public BaseClass { public: virtual void Method1() { cout << "Method 1 DERVIED" << endl; } }; DerClass myClass; ((BaseClass

Resolving the “most derived” method in a virtual override

时光怂恿深爱的人放手 提交于 2019-12-23 05:47:15
问题 I have a simple base class and derived class: class Base { public virtual void Write(int value) { Console.WriteLine("base int: {0}", value); } public virtual void Write(string value) { Console.WriteLine("base string: {0}", value); } } class Derived : Base { public override void Write(int value) { Console.WriteLine("derived int: {0}", value); } public virtual void Write(IEnumerable enumerable) { Console.WriteLine("derived IEnumerable: {0}", enumerable); } public virtual void Write(object o) {

Forcing a derived class to overload a virtual method in a non-abstract base class

此生再无相见时 提交于 2019-12-23 05:08:56
问题 I'm writing some code using a simple clone pattern, I'd like it if I were able to force derived classes to override that clone pattern, but retain the ability to use my base class. (So I don't want to declare the clone method to be pure virtual.) Is there anyway to enforce this restriction at the compiler level? 回答1: Unfortunately there is just no way to make this happen in C++. You can't force a non-abstract method to be overridden in child classes. However, I might note that concrete base

Virtual Function Table entry from class that is not related

本秂侑毒 提交于 2019-12-23 04:45:56
问题 I am browsing through VFTs (VMTs) of a simple C++ Windows program (I don't have a source code, only binary), compiled by Visual Studio with some sort of optimization on. I noticed that is uses inheritance and polymorphism. I found the location of struct s_RTTIBaseClassArray for each class that the program has. In that location there is an array of pointers to struct _s_RTTIBaseClassDescriptor . The array of base class descriptors should give you information about all the classes that the

Virtual function declared in a derived class

旧街凉风 提交于 2019-12-23 01:14:27
问题 Here I have declared another virtual function in Derived class. #include <iostream> using namespace std; class A{ string s; public: A(const string& name) : s(name){} virtual void f1(){cout << "f1 of base class" << endl;}; }; class B : public A{ string p; public: B(const string& name) : A(name){} virtual void f2(){cout << "virtual function of derived class" << endl;} void f1(){cout << "f1 of derived class";} }; int main() { A* arr[] = {new A("John"),new B("Bob")}; arr[0]->f1(); arr[1]->f1();

Force derived class to override at least one virtual function

强颜欢笑 提交于 2019-12-22 08:39:22
问题 Imagine this simple base class: struct simple_http_service { virtual reply http_get(…); virtual reply http_post(…); virtual reply http_delete(…); // etc. }; I'd like to prevent the user from deriving from this class without overriding at least one of these and prevent them from instantiang simple_http_service Is there some nice way to do this? 回答1: That sounds like a really odd constraint. By all means protect the user from incorrect usage, but don't try to prohibit things that you just "can