virtual-functions

virtual functions in C++

In my C++ program: #include<iostream.h> class A { public: virtual void func() { cout<<"In A"<<endl; } }; class B:public A { public: void func() { cout<<"In B"<<endl; } }; class C:public B { public: void func() { cout<<"In C"<<endl; } }; int main() { B *p...

pure virtual functions

In the C++ program: #include<iostream.h> class A { public: virtual void func()=0; }; class B:public A { public: void show() { func(); ...

Why aren't hot-swappable vtables a popular language feature?

In object-oriented programming, it's sometimes nice to be able to modify the behavior of an already-created object. Of course this can be done with relatively verbose techniques such as the strategy pattern. However, sometimes it would be nice to just completely change the type of the object by changing the vtable pointer after instant...

Virtual Methods or Function Pointers

When implementing polymorphic behavior in C++ one can either use a pure virtual method or one can use function pointers (or functors). For example an asynchronous callback can be implemented by: Approach 1 class Callback { public: Callback(); ~Callback(); void go(); protected: virtual void doGo() = 0; }; //Constructo...

Virtual tables on anonymous classes

I have something similar to this in my code: #include <iostream> #include <cstdlib> struct Base { virtual int Virtual() = 0; }; struct Child { struct : public Base { virtual int Virtual() { return 1; } } First; struct : public Base { virtual int Virtual() { return 2; } } Second; }; int main() { Child child; ...

C++ virtual function not called in subclass

Hi, Consider this simple situation: A.h class A { public: virtual void a() = 0; }; B.h #include <iostream> class B { public: virtual void b() {std::cout << "b()." << std::endl;}; }; C.h #include "A.h" #include "B.h" class C : public B, public A { public: void a() {std::cout << "a() in C." << std::endl;}; }; in...

Can you cache a virtual function lookup in C++?

Say I have a virtual function call foo() on an abstract base class pointer, mypointer->foo(). When my app starts up, based on the contents of a file, it chooses to instantiate a particular concrete class and assigns mypointer to that instance. For the rest of the app's life, mypointer will always point to objects of that concrete type. I...

Inherit from two polymorphic classes

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 ()...

C++ virtual function call versus boost::function call speedwise

Hello, I wanted to know how fast is a single-inheritance virtual function call when compared to one same boost::function call. Are they almost the same in performance or is boost::function slower? I'm aware that performance may vary from case to case, but, as a general rule, which is faster, and to a how large degree is that so? Thank...

How does the following foward-declared multi-inheritance pointer converted code work?

In the followint code, how does the pointer conversion & multi-inheritance play together? class Foo { public: virtual void someFunc(); }; class Bar; void someWork(Bar *bar) { ((Foo*) bar)->someFunc(); } class Bar: public Zed, public Foo { ... virtual void someFunc() { ... do something else ... } } Bar bar; int main() { som...

Virtual functions with two operands that can take many different types

Let me start with a concrete example. In C++, I have a hierarchy of classes under the abstract base class CollisionVolume. Any collision volume needs to be able to detectCollision with any other volume. This collision code is specialized based on the two subclasses in presence, but it is commutative: detectCollision(a, b) == detectCollis...

The misspelt virtual function problem

I've been caught by this problem more than once: class A{ public: virtual ~A() {} virtual int longDescriptiveName(){ return 0; } }; class B: public A{ public: virtual int longDescriptveName(){ return 1; } // Oops }; If the function is pure virtual, the compiler catches the error. But if it's not this can be a terrible bug to track d...

Are there cases where a class declares virtual methods and the compiler does not need to use a vptr?

I was wondering if there is a possible optimization where the compiler does not need to assign a vptr to an instantiated object even though the object's type is a class with virtual methods. For example consider: #include <iostream> struct FooBase { virtual void bar()=0; }; struct FooDerived : public FooBase { virtual void bar() {...

Resolve C++ virtual functions from base class

Hi, Sorry if this is a dupe, I cant find an answer quite right. I want to call a function from a base class member, and have it resolve to the subclass version. I thought declaring it virtual would do it, but it isn't. Here's my approach: class GUIWindow { public: GUIWindow() { SetupCallbacks(); } virtual void...

Template member function virtual?

I heard, templated member functions of a class can't be virtual. Is it true? BTW, if they can be, then please give an example of such a scenario when I would need such function? ...

Calling virtual functions inside member functions

I'm reading Thinking in C++ by Bruce Eckel. In Chapter 15 (Volume 1) under the heading "Behaviour of virtual functions inside constructor", he goes What happens if you’re inside a constructor and you call a virtual function? Inside an ordinary member function you can imagine what will happen – the virtual call is resolved a...

Exception specification when overriding a virtual function

Consider the following code: class A { public: virtual void f() throw ( int ) { } }; class B: public A { public: void f() throw ( int, double ) { } }; When compiled, it says that derived class B has a looser throw specifier compared to A. What is the importance of this? If we try to exchange their exception specification, su...

Can someone explain C++ Virtual Methods?

I'm learning C++ and I'm just getting into Virtual Functions/Methods. From what I've read (in the book and online), Virtual Methods are methods in the a base class that you can override in derived classes. But earlier in the book, when learning about basic inheritance, I was able to override base methods in derived classes without usin...

Virtual Classes Segmentation Faults

I'm working on a graphic application. It makes significant use of virtual classes. I'm getting some segmentation faults that I'm having trouble debugging. The primary classes in this application are: Shape (a virtual class) Rectangle Polygon Circle Picture (essentially a collection of shapes) Here is a shortened copy of the applic...

C++ vs. C++/CLI: Const qualification of virtual function parameters

[All of the following was tested using Visual Studio 2008 SP1] In C++, const qualification of parameter types does not affect the type of a function (8.3.5/3: "Any cv-qualifier modifying a parameter type is deleted") So, for example, in the following class hierarchy, Derived::Foo overrides Base::Foo: struct Base { virtual void Fo...