1

So say I have a class A:

class A{
  public:
    void DoSomething();
    void DoSomethingElse(int x);
};

And I want to have another class Proxy:

class Proxy{
    A* a;
  public:
    void DoSomething(){ a->DoSomething(); }
    void DoSomethingElse(int x){ a->DoSomethingElse(x); }
};

But the hitch is that I want to be able to make Proxy a templated class so that I can do this kind of transform to any class....

Is there some sort of trick I can do?

A detailed description:

Basically this proxy would take every single method in the class and create a method with the same name and use the pointer to complete the method....You cannot use inheritance because that increases the size, which is actually what I am trying to avoid here.

I am basically asking is there something akin to overriding the dot operator like in this question: Why can't you overload the '.' operator in C++? (The answer in there was "no")

Community
  • 1
  • 1
DarthRubik
  • 3,927
  • 1
  • 18
  • 54
  • @kfsone I am not sure I understand your question – DarthRubik May 07 '16 at 23:17
  • You want just a templated class? Or maybe you are getting at inheritance. – Trevor Hickey May 07 '16 at 23:20
  • @TrevorHickey Well the major difference between this and inheritance is the __size__ of the resulting class, which is why I am doing it this way. (the size of proxy is sizeof(ptr_t)) – DarthRubik May 07 '16 at 23:22
  • You could provide `operator->`, but other than that, no you can't. – milleniumbug May 07 '16 at 23:27
  • What kind of transform do you want to do ? – Johan Boulé May 07 '16 at 23:30
  • @JohanBoule I am trying to take each method of class `A`, create an associated method, which calls original `A` method....the reason is size, inheriting `A` could cause a huge size, while just doing this transform (with a pointer to `A`) has a size of a ptr – DarthRubik May 07 '16 at 23:34
  • @JohanBoule I edited my post a little to clairfy – DarthRubik May 07 '16 at 23:34
  • The only way I could think of would be code generation (parse the original class and create a proxy class from it). In other languages you could probably use runtime reflection to perform that task, but not in C++. And inheritance and templates won't help you, because they don't deliver you a method list. – Matthias247 May 07 '16 at 23:36
  • @Matthias247 That is what I was afraid of....but I will do that if I need to – DarthRubik May 07 '16 at 23:37
  • 3
    Why do you want to encapsulate the pointer rather than using it directly, or use a smart pointer ? We're probably not seeing the whole picture that leads to your question. – Johan Boulé May 07 '16 at 23:43
  • Hurb Sutter uses a functor based approach to do something like this. You can see an SO question about it here: http://stackoverflow.com/questions/16859519/how-to-wrap-calls-of-every-member-function-of-a-class-in-c11 – NathanOliver May 09 '16 at 11:49

2 Answers2

0

Consider two types with the same functionality.

struct A{
    void DoSomething(){
      std::cout << "A::DoSomething\n";
    }
    void DoSomethingElse(int x){
      std::cout << "A::DoSomething(int)\n";
    }
};

struct B{
    void DoSomething(){
      std::cout << "B::DoSomething\n";
    }
    void DoSomethingElse(int x){
      std::cout << "B::DoSomething(int)\n";
    }
};

Your templated proxy could be the following:

template <typename T>
class Proxy{
    T a;
  public:
    void DoSomething(){ a.DoSomething(); }
    void DoSomethingElse(int x){ a.DoSomethingElse(x); }
};

It would be used like so:

int main(){
  Proxy<B> b;
  Proxy<A> a;

  a.DoSomething();
  a.DoSomethingElse(0);

  b.DoSomething();
  b.DoSomethingElse(0);
}

result:

A::DoSomething
A::DoSomething(int)
B::DoSomething
B::DoSomething(int)
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
  • 1
    I guess I might not have stressed this enough but this has to work with **any** class you name it....std::vector, myRandomClass, etc.. This only works with those that have those particular functions....I will try to make this more clear in the question – DarthRubik May 07 '16 at 23:29
  • 1
    So if the class doesn't have the particular method, what do you expect Proxy to do when you call it? I think I see what you are getting at. You want proxy to inherit the methods from whatever it derives from. – Trevor Hickey May 07 '16 at 23:30
  • 1
    Exactly, except with out inheriting all of the size that is associated with inheriting – DarthRubik May 07 '16 at 23:36
0

What you are asking for is not possible in C++, but it's also not useful, because a Proxy instance would not also be an A instance -- you could not pass Proxy instances to methods that required As.

In fact, there wouldn't be any thing that could possibly call your Proxy object until you wrote those calls. Given that you have to write all the proxy calls, is it really so much extra work to write the proxy methods you're calling?

Matt Timmermans
  • 53,709
  • 3
  • 46
  • 87
  • Well actually it could be useful if you gave another template parameter of which is a base class of `A`, but yeah I do see your point – DarthRubik May 08 '16 at 00:29