How can I override inherited functions from a template class? - c++

I'm trying to override an inherited function from a base class which is also a class template, but I can't get it to work.
Up to now the base class is:
template<typename T, typename instrument>
class Probe {
private:
instrument probe;
public:
Probe(instrument _probe) : probe{_probe} {}
virtual T read() const;
instrument getProbe() const {return this->probe;}
};
While the derived class is:
class TestProbe : public Probe<int,int> {
public:
TestProbe(int _probe) : Probe(_probe) {}
virtual int read() const override {return getProbe();} // later will have a more complex expression
};
The error I get whenever I try to compile is: undefined reference to Probe<int, int>::read() const

Related

Making sure derived class implements atleast one of two methods from abstract class (C++)

How do I ensure my derived class implements at least one of two chosen methods in the base class?
class base {
public:
virtual int sample()=0;
virtual Eigen::VectorXf sample()=0;
};
class Derived : Base {
int sample() override {return 1;}
}
This code returns an error, as the sample method is not implemented with the VectorXf return type. However, my intention is that only one of these need to be implemented. The only reason they are seperate in the base class is that they have different return type. How can I do this in C++?
Overloading by return type is not possible. You may use std::variant instead:
#include <variant>
class Base {
public:
virtual std::variant<int, Eigen::VectorXf> sample()=0;
};
class Derived : public Base {
std::variant<int, Eigen::VectorXf> sample() override {return 1;}
};
If one is restricted to C++11, then there are many alternatives.
Implement and use something like variant: a class that has a enumerator selecting between two active types, and a union to contain these types.
Use Boost variant.
std::pair
Implement a hierarchy of classes (a simplification of std::any), and return on the right pointer to object:
class AbstractBase {
public:
virtual ~AbstractBase() = 0;
template <class T>
const T* get() const;
};
template <class T>
class ValueWrapper : public AbstractBase {
public:
ValueWrapper(const T& value) : m_value(value) {}
const T & getValue() const { return m_value; }
private:
T m_value;
};
template <class T>
inline const T * AbstractBase::get() const {
auto child = dynamic_cast<ValueWrapper<T> const*>(this);
return child ? &child->getValue() : nullptr;
}
class Base {
public:
virtual std::unique_ptr<AbstractBase> sample()=0;
};
The question is, why would you need this?

Interfaces and public methods

I have a struct that I use a pure abstract interface (only public methods and they're all =0), an abstract class to hide implementation details and two child classes that inherit from it.
I'd like to expose some public methods in those child classes as they only make sense in that context but marking them as public doesn't work as the compiler only seems to be able to see public methods in the interface. How can I make public methods in the child classes accessible?
Update
Interface:
class Result {
public:
virtual ~Result() noexcept = default;
protected:
Result() = default;
};
Abstract class:
template <typename T>
class AbstractResult : public Result {
public:
AbstractResult();
virtual ~AbstractResult() noexcept = default;
};
First child:
class AResult : public AbstractResult<PGResult> {
public:
PGResult() = default;
virtual ~PGResult() noexcept = default;
void add_server_status(const char status) noexcept;
void add_command_complete(const CommandComplete command_complete) noexcept;
void add_columns(const vector<Column> columns) noexcept;
void add_error(const Error error) noexcept;
void add_notification(const Notification notification) noexcept;
};
I'd like to create an instance of Result and call add_columns(...) on it which is forbidden by the compiler:
unique_ptr<Result> result.reset(new AResult);
result->add_columns(...)
It looks to me like when you create it you know the type, so stash that off before assigning it into the unique_ptr:
std::unique_ptr<AResult> temp(new AResult);
temp->add_columns(...);
unique_ptr<Result> result(std::move(temp));

Calling derived template class method from base non-template class

I'm trying to create the following flow:
Having a 'Base' class, with no params or functionality, just so i can hold
Base pointers in a method.
Its derived class, is a template, which implements operator() on given template argument object type.
I'm trying, by using a pointer to base class, call the derived class operator(), in run-time.
I've tried implementing it using CRTP
(https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern)
But that doesn't seem to work the-other-way-around - when the derived class is a class template.
Here is my code:
class Base {};
template<typename Ref_Obj, typename Obj_Type>
class Derived : public Base {
private:
typedef bool (Ref_Obj::*Func_p)(Obj_Type) const;
Func_p m_func;
const Ref_Obj& m_db;
public:
Derived<Ref_Obj, Obj_Type>(const Ref_Obj& db, Func_p func): m_db(base), m_func(filter) {}
inline bool operator()(const Obj_Type& obj) const {
return (m_db.*m_func)(obj);
}
}
Now, usage is in another class template, that contains vector of Base class pointers, as follows, and have its own operator() :
template<typename Obj_Type2> /* Upon usage, Obj_Type2 is the same as Obj_Type, this is just to differ the two in the code here */
class BlaBla {
private:
std::vector<const Base *> m_vec;
public:
/* relevant constructors ... */
inline bool operator()(const Obj_Type2 &obj) const {
for(std::vector<const Base *>::const_iterator it = m_vec.begin(); it != m_vec.end(); ++it) {
/*** This is the problematic line V ***/
if( (*it).operator()(obj) ) { /* DO SOMETHING */ }
}
}
Of course the compiler is complaining that there is no matching function to call for in the problematic line that is marked in the code below, but i can't figure out a way to do the relevant call.
1st Solution that came to mind, is to create a virtual operator()(...), with a specific object type e.g. virtual operator()(const uint32_t &obj) in Base class, which works, but my intention is that operator()(...) will receive a variety of object types, and stating a virtual method for each of them is not elegant and seems to break all the template concept i want to implement here.
2nd Solution that came to mind, is somehow passing Ref_Obj and Obj_Type typenames to Base class, to be used in sort of interface method, that will use static_cast to call the appropriate Derived operator (As in CRTP) - But that doesn't seem to work because Derived class is a class template, and also BlaBla class doesn't directly know Ref_Obj typename.
Is there any other way to make the appropriate call to Deriver operator()
There is no clear clean way to do this. The fundamental problem is that there is no such thing as a virtual template method.
The cleanest way I can think of doing this that implements complete type erasure is with a dynamic cast, and a virtual method:
class Base {
public:
virtual bool operator()(const Base &) const=0;
};
template<typename Ref_Obj, typename Obj_Type>
class Derived : public Base {
private:
typedef bool (Ref_Obj::*Func_p)(Obj_Type) const;
Func_p m_func;
const Ref_Obj& m_db;
public:
Derived<Ref_Obj, Obj_Type>(const Ref_Obj& db, Func_p func): m_db(base), m_func(filter) {}
inline bool operator()(const Base& obj) const override {
const Obj_Type *derived_obj=dynamic_cast<const Obj_Type *>(&obj);
if (!derived_obj)
{
throw; // Or maybe return false, or something...
}
return (m_db.*m_func)(*derived_obj);
}
};
Obj_Type must also be derived from Base. This gets the job done at runtime, but there is no compile-time type-checking.
The other approach is to bite the bullet, and forego 100% type erasure:
template<typename Obj_Type>
class Base {
public:
virtual bool operator()(const Obj_Type &) const=0;
};
template<typename Ref_Obj, typename Obj_Type>
class Derived : public Base<Obj_Type> {
private:
typedef bool (Ref_Obj::*Func_p)(Obj_Type) const;
Func_p m_func;
const Ref_Obj& m_db;
public:
Derived<Ref_Obj, Obj_Type>(const Ref_Obj& db, Func_p func): m_db(base), m_func(filter) {}
inline bool operator()(const Obj_Type& obj) const override {
return (m_db.*m_func)(obj);
}
};
So, you can still abstract away operator() on some object to its pure interface, and define the details in the subclass.
Or, another alternative would be a combination of the two:
class SuperBase {
public:
virtual bool operator()(const Base &) const=0;
};
template<typename Obj_Type>
class Base : public SuperBase {
public:
virtual bool operator()(const Obj_Type &) const=0;
bool operator()(const Base &obj) const override
{
// Do the dynamic cast check, and forward it to the other
// operator().
}
};

calling a template function of the base class

class Base
{
protected:
template<typename TFunc>
void m_vProcess(TFunc& func) const
{
BasePrivateClass<TFunc> baseprivateobj(func);
// rest of code
}
private:
template<typename TFunc>
class BasePrivateClass
{
public:
BasePrivateClass(TFunc& obj) :m_obj(oParam)
{}
private:
TFunc& m_obj;
};
};
and this is my derived class:
class Derived: public Base
{
private:
class DerivedPrivateClass
{
public:
explicit DerivedPrivateClass(int **p): m_objDerived(poParam)
{}
private:
int** m_objDerived;
};
};
From a function in class Derived, I try this:
DerivedPrivateClass obj(param);
this->m_vProcess(obj);
I get a linker error of multiply defined symbols - symbols are defined in both base and derived class.
I have no idea what causes the linker error. Can anyone help?
As I commented.. a bit hard to read.. but from the looks of it, a main issue would be that the m_vProcessAnimParams function you are trying to use is private in Base, so Dervied cannot access it..

C++ mixins via templates: why doesn't this work?

I've got a interface that's implemented as an abstract base class with a number of pure virtual public methods. These pure virtual functions can be implemented using a template since the differences between subclasses aren't large - so my idea was to use multiple inheritance to mix-in the appropriately templated helper-class that provides the implementation. However, the compiler complains that the base class is abstract; it isn't considering the helper mix-in's implementation so thinks there's no implementation of a required method.
For example:
class TrivialList {
int count;
public:
TrivialList(int count) : count(count){}
virtual double Average() const=0;
int Count() const {return count;}
virtual ~TrivialList(){}
};
template<typename TIndexable> class AverageHelper {
public:
double Average() const {
TIndexable const & self = static_cast<TIndexable const &>(*this);
double sum=0.0;
for(int i=0;i<self.Count();++) sum += self.Get(i);
return sum / self.Count();
}
};
class IndexableList : public TrivialList, public AverageHelper<IndexableList> {
std::vector<double> backend;
public:
IndexableList(int count) : TrivialList(count), backend(count) { }
double & Get(int i) { return backend[i];}
double const & Get(int i) const { return backend[i];}
};
IndexableList * MakeList() {return new IndexableList(5);} //error!
// cannot instantiate abstract class
I'm using MSC 10.0 (Visual Studio 2010); the code fails with a similar error using g++ 4.5.
Get or the real-world equivalents in my project cannot be virtual because they're extremely minor operations that need to be inlined for adequate performance (think put-pixel/get-pixel) - so I need the generic algorithms to be templated rather than generic via virtual function calls.
For implementing mix-ins via templates, you need the template implementing the abstract function to derive from the abstract base class.
So you may fix your code by changing it the following way:
// ...
template<typename TIndexable> class AverageHelper : public TriviaList{
// ...
class IndexableList : public AverageHelper<IndexableList> {
In general, if you want to provide more than one mix-in, you may either use a virtual inheritance in order not multiplying the instances of the base classes, or to use chain inheritance as in the following sample:
class Abstract {
public:
virtual void foo() = 0;
virtual void bar() = 0;
};
template<class Base>
class FooImpl : Base {
public:
void foo() { /* default foo implementation */ }
};
template<class Base>
class BarImpl : Base {
public:
void bar() { /* default bar implementation */ }
};
class Derived : public BarImpl<FooImpl<Abstract> > {
// You have both foo() and bar() implementations available
};
It doesn't work because AverageHelper<>::Average() doesn't override TrivialList::Average(). In order to override a virtual function, the overriding class must inherit from the class containing the function to be overridden.
You could change your template thus:
template<typename TIndexable, typename Base >
class AverageHelper : public Base {
public:
template< typename T >
AverageHelper(T arg) : Base(arg) {}
// ...
};
class IndexableList : public AverageHelper<IndexableList,TrivialList> {
public:
IndexableList(int count) : AverageHelper<IndexableList,TrivialList>(count) {}
// ...
};
You might want to virtually derive from TrivialList:
template<typename TIndexable, typename Base >
class AverageHelper : virtual public Base {
// ...
};