virtual-functions

Simulating a virtual static member of a class in c++?

Is there anyway to have a sort of virtual static member in C++? For example: class BaseClass { public: BaseClass(const string& name) : _name(name) {} string GetName() const { return _name; } virtual void UseClass() = 0; private: const string _name; }; class DerivedClass : public BaseClass { ...

At as deep of a level as possible, how are virtual functions implemented?

We all know what virtual functions are in C++, but how are they implemented at a deep level? Can the vtable be modified or even directly accessed at runtime? Does the vtable exist for all classes, or only those that have at least one virtual function? Do abstract classes simply have a NULL for the function pointer of at least one ent...

Where do "pure virtual function call" crashes come from?

I sometimes notice programs that crash on my computer with the error: "pure virtual function call". How do these programs even compile when an object cannot be created of an abstract class? ...

If classes with virtual functions are implemented with vtables, how is a class with no virtual functions implemented?

In particular, wouldn't there have to be some kind of function pointer in place anyway? ...

Performance penalty for working with interfaces in C++?

Is there a runtime performance penalty when using interfaces (abstract base classes) in C++? ...

What are the dangers of making a method virtual?

I've been doing some mocking with RhinoMocks and it requires that mocked methods be made virtual. This is fine except we have a custom framework which contains the methods that I want to mock which are currently not marked as virtual. I can't forsee any problem with making these methods virtual but I was wondering what are some potentia...

Is there any reason not to make a member function virtual?

Is there any real reason not to make a member function virtual in C++? Of course, there's always the performance argument, but that doesn't seem to stick in most situations since the overhead of virtual functions is fairly low. On the other hand, I've been bitten a couple of times with forgetting to make a function virtual that should ...

Virtual functions and performance - C++

In my class design, I use abstract classes and virtual functions extensively. I had a feeling that virtual functions affects the performance. Is this true? But I think this performance difference is not noticebale and looks like I am doing premature optimization. Right? ...

Arduino C++ code: can you use virtual functions and exceptions?

Following up on this comment from the question Writing firmware: assembly or high level?: When compiling C++ code for the Arduino platform, can you use virtual functions, exceptions,etc. Or would you want to (have to) use a subset of C++ (as described in the comment) Any other caveats when programming for the Arduino platform? ...

Overriding public virtual functions with private functions in C++

Is there is any reason to make the permissions on an overridden C++ virtual function different from the base class? Is there any danger in doing so? For example: class base { public: virtual int foo(double) = 0; } class child : public base { private: virtual int foo(double); } The C++ faq says that it is a b...

Class design suggestions - C++

Background I am working on a phonetic converter program which converts english text into equivalant regional language text. Regional languages will have more characters than english letters and regional language fonts uses almost all positions (1-255) in a font. My program supports different fonts and I have created a font class which...

Safely override C++ virtual functions

I have a base class with a virtual function and I want to override that function in a derived class. Is there some way to make the compiler check if the function I declared in the derived class actually overrides a function in the base class? I would like to add some macro or something that ensures that I didn't accidentally declare a ne...

simics or tau?

Is there somebody that use SIMICS or TAU? are good tools to profiling my code? I see that SIMICS is also a virtual platform hardware dipendent so i can run different simulation on different hardware while tau is a simple profiling tool. Is there somebody that can give me more information about these? thanks in advance marco ...

Are there alternatives to polymorphism in C++?

The CRTP is suggested in this question about dynamic polymorphism. However, this pattern is allegedly only useful for static polymorphism. The design I am looking at seems to be hampered speedwise by virtual function calls, as hinted at here. A speedup of even 2.5x would be fantastic. The classes in question are simple and can be cod...

Template or abstract base class?

If I want to make a class adaptable, and make it possible to select different algorithms from the outside -- what is the best implementation in C++? I see mainly two possibilities: Use an abstract base class and pass concrete object in Use a template Here is a little example, implemented in the various versions: Version 1: Abstract...

in .NET when you pass a class instance/interface as a parameter do you pass one object or the full vtable

If you are passing an interface or an instance of a class as a parameter, are we passing many objects or the full vtable, because once you call a method on the instance it need to recurse the vtable and call the appropriate one right? How does this work? ...

C++: Multiple inheritance + virtual function mess

I have a diamond multiple inheritance scenario like this: A / \ B C \ / D The common parent, A, defines a virtual function fn(). Is it possible for both B and C to define fn()? If it is, then the next question is - can D access both B and C's fn() without disambiguation? I'm assuming there is some syntax for this....

What is (or should be) the cyclomatic complexity of a virtual function call?

Cyclomatic Complexity provides a rough metric for how hard to understand a given function is, or how much potential for containing bugs it has. In the implementations I've read about, usually all of the basic control flow constructs (if, case, while, for, etc.) increase the complexity of a function by 1. It appears to me given that cyclo...

What's the advantage of this indirect function call?

I found the following code in a library: class Bar { public: bool foo(int i) { return foo_(i); } private: virtual bool foo_(int i) = 0; }; Now I'm wondering: Why would you use this indirection? Could there be any reasons why the above would be better than the simple alternative: class Bar { public: virtual bool foo(int i) ...

What is the performance cost of having a virtual method in a C++ class?

Having at least one virtual method in a C++ class (or any of its parent classes) means that the class will have a virtual table, and every instance will have a virtual pointer. So the memory cost is quite clear. The most important is the memory cost on the instances (especially if the instances are small, for example if they are just m...