Sign in to follow this  
waZim

Inheritance and Templates Problem

Recommended Posts

Hi all, I want to inherit from the engine classes i am using, and i have a problem.. some of the operators return by value in the base classes of the engine.. lets say i have a vector in the engine.
class EngineVector
{
  EngineVector operator * (EngineVector& other) const;
}
and i have this one
class MyVectorWrapper : public EngineVector
{
  MyVectorWrapper operator * (MyVectorWrapper& other) const;
}
What would be the MyVectorWrapper operator's Body, if i want to use the EngineVector operator in it? is there any templates based solution to this? something like..
template <class T>
class MyVectorWrapper
{
  MyVectorWrapper operator * (MyVectorWrapper& other) const;
}

// and when i want to use it .
MyVectorWrapper<EngineVector> vector;

the basic problem that i have is wrapping the things in the engine with my classes.. so that the end user who use the wrapper, don't need to know and use the engine classes. and use the generic interface that i provide.. any comments on wrapping would be greatly appriciated. .. waZim

Share this post


Link to post
Share on other sites
Hi,

the following should be adaptable to your problem:



#include <iostream>

class A
{
public:
explicit A(int v) : _value(v) {}

A operator*(const A& other) const
{
return A(_value * other._value);
}

int getValue() const { return _value; }

protected:
int _value;
};

class B : public A
{
public:
B(int v) : A(v) {}
explicit B(const A& a) : A(a.getValue()) {}

void print() const
{
std::cout << _value << std::endl;
}

template <typename T>
B operator*(const T& other) const
{
return B( *static_cast<const A*>(this) * *static_cast<const A*>(&other) );
}
};

int main(int,char**)
{
B b1 = B(5);
B b2 = B(-7);

B b3 = b1 * b2;
b3.print();

return 0;
}




Share this post


Link to post
Share on other sites
Quote:
Original post by waZim
the basic problem that i have is wrapping the things in the engine with my classes.. so that the end user who use the wrapper, don't need to know and use the engine classes. and use the generic interface that i provide.. any comments on wrapping would be greatly appriciated.

Would the pImpl idiom not solve this problem better?

Perhaps your inheritance heirarchy is backwards, in that your interface should be an abstract base class and your private implementation should derive from it?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this