in order to use the original parent methods. In which case I think a casting will be better.
I've got confused as to why are you holding an object in both wrappers too.
Another idea
you can essentially specify whether you want the Wrapper class to be virtual or not
Then ¿how is that you modify the methods?
1 2 3 4 5 6 7 8 9
class base{
public:
void bar(){}
};
class derived: public virtual_wrapper<foo>{
public:
void bar(){} //this is not in the parent
};
It may have more sense in that case to do
1 2 3 4 5
template <class Parent>
class virtual_wrapper: public Parent{
public:
virtual ~virtual_wrapper(){}
};
But then ¿what about the non-virtual version?
So, ¿what is the purpose? ¿how is it used?
¿Can you please post a better example?
With my code all methods can become virtual or remian non-virtual depending on the needs of the programmer. I used a Wrapper class as an example just because...it was an example. The fact that it holds an object servers no purpose but to aid in the demonstration of the ability to choose between virtual and non-virtual.
¿How will you virtualize all the methods in std::vector<int> ?
¿Or how will you de-virtualize them from cant_touch_this ?
This is my point; there is a performance gain with non-virtual classes, but there is a design gain with virtual classes. The code duplication is a problem, but do you know a better way to have a class be 100% virtual or 0% at different points of the code?
Devirtualization is a task done best by the compiler or runtime not a library. Anyway, I can't see the point. If you care for performance, you should write your code as cleanly as possible, profile it, and *then* optimize.
The simplest solution would be to just rename the function so that you have a non-virtual and virtual function. The virtual one could just call the nonvirtual one:
If you don't like that solution, another one would be to explicitly call the version you want with the scope operator:
#include <iostream>
usingnamespace std;
class A
{
public:
virtualvoid foo()
{
cout << "A::foo";
}
};
class B : public A
{
public:
virtualvoid foo()
{
cout << "B::foo";
}
};
void callfoo(A& a)
{
a.A::foo();
}
int main()
{
B b;
callfoo(b); // prints "A::foo"
cin.get();
return 0;
}
But again, this is kind of ridiculous and impractical. Calling a virtual function non-virtually has a huge potential to break code and probably shouldn't be done ever.
I don't care so much about performance as I do about having to deal with classes that aren't completely virtual. The problem I'm trying to find a good solution for is that there are a LOT of classes out there that can't be modified and aren't virtual for performance reasons but would really be more beneficial of they were virtual.
The problem I'm trying to find a good solution for is that there are a LOT of classes out there that can't be modified and aren't virtual for performance reasons but would really be more beneficial of they were virtual.
Have you any specific examples? Because maybe there are other approaches...
The problem I'm trying to find a good solution for is that there are a LOT of classes out there that can't be modified and aren't virtual for performance reasons but would really be more beneficial of they were virtual.
Have you considered Java interface design? You can have an interface and at the same time you can inherit concrete class.
e.g
class A extends B implements C
This approach depends on those C++ class makers making an "interface" for you to implement though.