• Advertisement
Sign in to follow this  

Inheritance and Templates Problem

This topic is 3136 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement