Name of this pattern? (virtual-switch)

I was just thinking that it was annoying that there was no way to toggle whether a class was all virtual or not, and then I thought of this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>

template<typename T, typename U = void>
class Wrapper
{
	T t;
public:
	Wrapper() : t() {}
	T &get()
	{
		return(t);
	}
};
template<typename U>
class Wrapper<void, U>
{
protected:
	U u;
public:
	Wrapper() : u() {}
	virtual U &get()
	{
		return(u);
	}
	virtual ~Wrapper(){}
};

class RandomIntWrapper : public Wrapper<void, int>
{
	int u2;
public:
	RandomIntWrapper() : Wrapper(), u2() {}
	virtual int &get()
	{
		if((u > 500 || u < 100) && u2 < 30)
		{
			return(u);
		}
		return(u2);
	}
};

int main()
{
	Wrapper<int> i;
	i.get() = 7;
	++i.get();
	std::cout << i.get() << std::endl;
	RandomIntWrapper j;
	Wrapper<void, int> &k = j;
	k.get() = 99;
	std::cout << ++k.get() << std::endl;
	std::cout << ++k.get() << std::endl;

	std::cin.ignore();
}
8
100
1
As you can see, you can essentially specify whether you want the Wrapper class to be virtual or not for polymorphic/performance reasons.

What is this pattern called?
Last edited on
Confusing.
Confusing? It's cleanly formatted in 56 lines, the only confusing part is line 35, which is not what I am trying to show...

So, you have a name for it? (Besides confusing?)
¿Code duplication?
I really fail to see the problem that you are "solving".

At first I thought that you intended to
1
2
Wrapper<foo> *non_virtual;
Wrapper<void, foo> *virtualized;
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?
How is this useful in practice?
+1 ne555

I'm not sure how this could be useful.
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.
But that's contradictory. Why would you ever want to call a virtual function as if it weren't virtual? What would be the point?

Besides, wouldn't it just be more practical to give the virtual/nonvirtual functions different names?
That is what I fail to see.
You just created 2 "identical" classes. And you copy-paste the code, so it has to be updated.

¿How will you virtualize all the methods in std::vector<int> ?
¿Or how will you de-virtualize them from cant_touch_this ?
¿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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
using namespace std;

class A
{
public:
    virtual void foo()
    {
        cout << "A::foo";
    }
};

class B : public A
{
public:
    virtual void 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.
Last edited on
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.
Topic archived. No new replies allowed.