i am trying to solve this for some time and i hope to find some help here. I have a base class and inside the base class i need a template method. But this method can not be virtual (which i know) so i can not override it in the child class, but is there any alternative or workaround? Some kind of design pattern? Thanks
#include <iostream>
#include <vector>
#include <map>
class ABase {
public:
ABase() :
id(s_id++)
{}
size_t static s_id;
size_t id;
};
size_t ABase::s_id(0);
class A1 : public ABase {
public:
};
class A2 : public ABase {
public:
};
class BBase {
public:
};
class B1 : public BBase {
public:
};
class B2 : public BBase {
public:
};
class ComboBase {
public:
// this cannot be virtual :(
// so i cannot store b in bs in the child class
/* virtual */ template<typename T>
void addB(T b) {
}
};
template<class T, class T2>
class Combo : public ComboBase {
public:
// override of template method is not possible
/*
template<typename T>
void addB(T b) override {
bs.push_back(b);
}
*/
size_t id;
std::shared_ptr<T> a;
std::vector<std::shared_ptr<T2>> bs;
};
int main() {
std::map<size_t, std::shared_ptr<ComboBase>> combos;
auto combo1 = std::make_shared<Combo<A1, B1>>();
auto combo2 = std::make_shared<Combo<A2, B2>>();
combos[combo1->id] = combo1;
combos[combo2->id] = combo2;
// here is the problem... later i the code
// i would like to add Bs to the existing combo but i cannot use
// virtual templates
auto b1 = std::make_shared<B1>();
auto b2 = std::make_shared<B2>();
auto it1 = combos.find(0);
if (it1 != combos.end()) {
it1->second->addB(b1);
it1->second->addB(b2);
}
auto it2 = combos.find(0);
if (it2 != combos.end()) {
it2->second->addB(b1);
it2->second->addB(b2);
}
}
Related
I have several disparate templated pure abstract classes. I derive from these to get a bunch of classes, and from there, I can use those to make a bunch of objects. I would like to put all of these objects into a container. However, they are all of different types. I am wondering how to accomplish this late-stage polymorphism.
Say this is my pre-existing code that I have right now:
#include <iostream>
template<typename T>
class A{
public:
A() : m_num(1.0) {};
virtual ~A() {};
virtual void printNum() const = 0;
protected:
T m_num;
};
template<typename T>
class B{
public:
B() : m_num(2.0) {};
virtual ~B() {};
virtual void printTwiceNum() const = 0;
protected:
T m_num;
};
class A_example : public A<int>
{
public:
A_example() : A<int>() {};
void printNum() const { std::cout << m_num << "\n"; };
};
class B_example : public B<int>
{
public:
B_example() : B<int>() {};
void printTwiceNum() const { std::cout << 2*m_num << "\n"; };
};
int main(){
A_example first;
B_example second;
first.printNum();
second.printTwiceNum();
return 0;
}
With more classes, it could get pretty messy inside of main(). Ideally I could jut iterate over the container and call print() on each element. My first thought is to use a std::vector<unique_ptr<Base>>. This seems to work:
#include <iostream>
#include <vector> // new include
#include <memory> // new include
#include <utility> // new include
// new Base class here
class Base{
public:
virtual ~Base(){};
};
template<typename T>
class A : public Base{ // new inheritance here
public:
A() : m_num(1.0) {};
virtual ~A() {};
virtual void printNum() const = 0;
protected:
T m_num;
};
template<typename T>
class B : public Base{ // new inheritance here as well
public:
B() : m_num(2.0) {};
virtual ~B() {};
virtual void printTwiceNum() const = 0;
protected:
T m_num;
};
class A_example : public A<int>
{
public:
A_example() : A<int>() {};
void printNum() const { std::cout << m_num << "\n"; };
};
class B_example : public B<int>
{
public:
B_example() : B<int>() {};
void printTwiceNum() const { std::cout << 2*m_num << "\n"; };
};
int main(){
std::vector<std::unique_ptr<Base>> v;
v.emplace_back( new A_example() );
v.emplace_back( new B_example() );
//v[0]->printNum(); // nope
//v[1]->printTwiceNum(); // nope
return 0;
}
This is cool because I didn't have to change A_example or B_example, and all I changed in A and B was that I added : public Base. However, I have no idea how to call each elements print*** function. Is there any way to call the printNum() and printTwiceNum() functions, and for them to be automatically recognized?
The simplest approach is to just make a virtual function Base::print and have your derived classes implement it. But that's not always appropriate.
Another approach is to branch on dynamic_cast conversions. The premise there is that some functions are only available on some classes. But this can get hairy especially when using class templates, as you must handle all expected template parameters.
To generalize this, you can use interface classes. Let's say you have lots of different classes but only a small number of print variations. In that case, it may make sense to do this:
class PrintNumInterface {
public:
virtual void printNum() const = 0;
};
class PrintTwiceNumInterface {
public:
virtual void printTwiceNum() const = 0;
};
template<typename T> class A : public Base, public PrintNumInterface { ... };
template<typename T> class B : public Base, public PrintTwiceNumInterface { ... };
And now, no matter how many additional classes or template expansions you have to deal with, you only need to handle these interfaces:
for (auto& p : v)
{
if (PrintNumInterface* iface = dynamic_cast<PrintNumInterface*>(p.get())
iface->printNum();
else if (PrintTwiceNumInterface* iface = dynamic_cast<PrintTwiceNumInterface*>(p.get())
iface->printTwiceNum();
}
How to do multiple inheritance just for function?
must share data of the base class
no virtual function (assume that vtable is expensive)
avoid virtual inheritance
implementation must be able to reside in .cpp
c++14 is allowed
Here are similar questions :-
Multiple inheritance in diamond shape with functions only - use virtual inheritance. Virtual inheritance is generally bad and expensive.
multiple inheritance without virtual inheritance - focuses on syntax and compiling rather than programming technique.
Multilevel inheritance in c++ (CRTP) , CRTP and multilevel inheritance , Eliminate redundancy with CRTP and multiple inheritance (C++03) and Using CRTP with virtual inheritance - implementation must be in header
Here is a sample code (coliru demo) :-
class O{
protected: int database=0;
};
class A : public O{
public: void print(){
std::cout<<database<<std::endl;
}
};
class B : public O{
public: void set(int s){
database=s+1;
}
};
class AB : public O{
public: void print(){//duplicate
std::cout<<database<<std::endl;
}
public: void set(int s){//duplicate
database=s+1;
}
};
//AB ab; ab.set(1); ab.print(); // would print 2
Here is my attempt (wandbox demo). I abuse CRTP :( :-
class O{
public: int database=0;
};
template<class T>class OA{
public: void print(){
std::cout<<static_cast<T*>(this)->database<<std::endl;
}
};
template<class T>class OB{
public: void set(int s){
static_cast<T*>(this)->database=s+1;
}
};
class A :public O,public OA<A>{};
class B :public O,public OB<B>{};
class AB :public O,public OA<AB>,public OB<AB>{};
It works, but it looks inelegant.
Furthermore, implementation must be in header (because OA and OB are template classes).
Are there better approaches? Or is this the way to go?
Sorry if it is too newbie question or already asked. I am a C++ beginner.
Edit
Give extended example of using please.
In ECS, it would be useful in some cases :-
class O{
protected: EntityHandle e;
};
class ViewAsPhysic : public O{ //A
public: void setTransform(Transformation t){
Ptr<PhysicTransformComponent> g=e;
g->transform=t;
}
};
class ViewAsLight : public O{ //B
public: void setBrightness(int t){
Ptr<LightComponent> g=e;
g->clan=t;
}
};
class ViewAsLightBlock : public O{ //AB
//both functions
};
The problem here is that the database field is member of class O. So without virtual inheritance, A and B will have each their own copy of database. So you must find a way to force A and B to share same value. You could for example use a reference field initialized in a protected constructor:
#include <iostream>
class O{
int _db;
protected: int &database;
O(): database(_db) {};
O(int &db): database(db) {};
};
class A : public O{
public: void print(){
std::cout<<database<<std::endl;
}
A() {} // public default ctor
protected: A(int& db): O(db) {}; // protectect ctor
};
class B : public O{
public: void set(int s){
database=s+1;
}
B() {} // public default ctor
protected: B(int& db): O(db) {}; // protectect ctor
};
class AB : public A, public B {
int _db2;
public: AB(): A(_db2), B(_db2) {}; // initialize both references to same private var
};
int main() {
AB ab;
ab.set(1);
ab.print();
return 0;
}
displays as expected:
2
Above code uses no virtual inheritance, no virtual function and no templates, so method can safely implemented in cpp files. The class AB actually uses methods from its both parents and has still a coherent view on its underlying data. In fact it simulates an explicit virtual inheritance by building the common data in the most derived class and injecting in through protected constructors in its parents.
Something like this?
must share data of the base class - check
no virtual function (assume that vtable is expensive) - check
avoid virtual inheritance - check
implementation must be able to reside in .cpp- check
c++14 is allowed - check. c++11 used.
#include <iostream>
class O {
protected:
int database = 0;
};
/*
* the concept of implementing print for a base class
*/
template<class...Bases>
struct implements_print : Bases... {
void print() const {
std::cout << this->database << std::endl;
}
};
/*
* The concept of implementing set for a base class
*/
template<class...Bases>
struct implements_set : Bases... {
void set() {
++this->database;
}
};
struct B : implements_set<O> {
};
struct A : implements_print<O> {
};
struct AB : implements_set<implements_print<O>> {
};
int main() {
A a;
a.print();
B b;
b.set();
AB ab;
ab.set();
ab.print();
}
Another way, using composition and an access class to provide access to the protected member. This example shows how to defer the work on database to another compilation unit:
#include <iostream>
/*
* this stuff in cpp
*/
namespace implementation
{
void print(const int& database) {
std::cout << database << std::endl;
}
void set(int& database) {
++database;
}
}
/*
* this stuff in header
*/
struct OAccess;
class O {
private:
int database = 0;
friend OAccess;
};
struct OAccess {
template<class Host>
constexpr decltype(auto) database(Host &host) const { return (host.database); } // note: () makes reference
template<class Host>
constexpr decltype(auto) database(Host const &host) const { return (host.database); } // note: () makes reference
};
/*
* the concept of implementing print for a derived class
*/
template<class Host>
struct implements_print {
void print() const {
OAccess access;
implementation::print(access.database(self()));
}
private:
decltype(auto) self() const { return static_cast<Host const &>(*this); }
};
/*
* The concept of implementing set for a derived class
*/
template<class Host>
struct implements_set {
void set() {
OAccess access;
implementation::set(access.database(self()));
}
private:
decltype(auto) self() { return static_cast<Host &>(*this); }
};
template<template<class> class...Impls>
struct OImpl : Impls<OImpl<Impls...>> ..., O {
};
using B = OImpl<implements_set>;
using A = OImpl<implements_print>;
using AB = OImpl<implements_print, implements_set>;
int main() {
A a;
a.print();
B b;
b.set();
AB ab;
ab.set();
ab.print();
}
We start with defining the concepts of things that can print and things that can be set:
namespace util {
template<class Base, class Derived, class R=void>
using if_base = std::enable_if_t< std::is_base_of< std::decay_t<Base>, std::decay_t<Derived>>::value, R >;
struct stub {};
}
namespace concepts {
template<class Token>
void do_print(Token, util::stub const&)=delete;
template<class Token>
void do_set(Token, util::stub&, int)=delete;
struct has_print {
struct token { friend struct has_print; private: token(int){} };
template<class T>
friend util::if_base<has_print, T> print(T const& t) {
do_print(get_token(), t);
}
private: static token get_token() { return 0; }
};
struct has_set {
struct token { friend struct has_set; private: token(int){} };
template<class T>
friend util::if_base<has_set, T> set(T& t, int x) {
do_set(get_token(),t, x);
}
private: static token get_token() { return 0; }
};
}
We then declare O and the operations you can support on it:
namespace DB {
class O;
void do_print(::concepts::has_print::token, O const& o);
void do_set(::concepts::has_set::token, O& o, int);
class O{
protected: int database=0;
friend void do_print(::concepts::has_print::token, O const&);
friend void do_set(::concepts::has_set::token, O&, int);
};
class A : public O, public concepts::has_print {
};
class B : public O, public concepts::has_set {
};
class AB : public O, public concepts::has_print, concepts::has_set {
};
}
void DB::do_print(::concepts::has_print::token, O const& o ) { std::cout << o.database << std::endl; }
void DB::do_set(::concepts::has_set::token, O& o, int x) { o.database = x+1; }
The hard part of this is the access control.
I ensure it isn't possible to call do_set except through has_set::set.
That is what all those tokens are about. You can strip them out and their overhead if you are willing to just say "don't call the do_ functions" (and maybe give them another name, like private_impl_set).
Live example.
To start discussion.
class O
{
// no virtual destructor. So cant use polymorphic deletion
// like :
// O *o = new AB;
// delete o;
protected: int database=0;
};
class A : virtual public O{
public: void print(){
std::cout<<database<<std::endl;
}
};
class B : virtual public O{
public: void set(int s){
database=s+1;
}
};
class AB : protected A, protected B{}; // no vtable
void foo() {
AB ab;
ab.print(); // won't perform virtual call.
}
Well, as you know, the design pattern Visitor has a "problem" similar to Abstract Factory problem: the more visitable classes I made, the more specific "visit" methods must create.
In the case of an abstract factory I made a solution using prototype of a product to "configure" the factory:
factory.h
class ExtensibleFactory
{
public:
~ExtensibleFactory();
void insertProductType(const string &nome, IProductPrototype *product);
void removeProductType(const string &nome);
IProductPrototype *createProduct(const string &nome);
private:
map<string, IProductPrototype *> m_productsHash;
};
factory.cpp
#include "extensiblefactory.h"
#include "iproductprototype.h"
ExtensibleFactory::~ExtensibleFactory()
{
for(map<string, IProductPrototype *>::iterator iter = this->m_productsHash.begin(); iter != this->m_productsHash.end(); ++iter)
{
delete iter->second;
}
this->m_productsHash.clear();
}
void ExtensibleFactory::insertProductType(const string &nome, IProductPrototype *product)
{
this->m_productsHash.insert(make_pair(nome, product));
}
void ExtensibleFactory::removeProductType(const string &nome)
{
delete this->m_productsHash[nome];
this->m_productsHash.erase(nome);
}
IProductPrototype *ExtensibleFactory::createProduct(const string &nome)
{
if ( this->m_productsHash.find(nome) == this->m_productsHash.end() )
{
return 0;
}
return this->m_productsHash[nome]->clone();
}
main.cpp
SanduichePrototype *sanduiche = new SanduichePrototype;
CarroPrototype *carro = new CarroPrototype;
ExtensibleFactory *fabrica = new ExtensibleFactory;
fabrica->insertProductType("sanduba", sanduiche);
fabrica->insertProductType("automovel", carro);
IProductPrototype *carro1 = fabrica->createProduct("automovel");
IProductPrototype *carro2 = fabrica->createProduct("automovel");
IProductPrototype *sanduiche1 = fabrica->createProduct("sanduba");
IProductPrototype *sanduiche2 = fabrica->createProduct("sanduba");
Now, consider this visitor and its elements:
ivisitor.h
class ElementA;
class ElementB;
class IVisitor
{
public:
virtual void visit(ElementA *elementA) = 0;
virtual void visit(ElementB *elementB) = 0;
};
ielement.h
class IVisitor;
class IElement
{
public:
virtual void accept(IVisitor *visitor) = 0;
};
elementa.h
class ElementA : public IElement
{
public:
virtual void accept(IVisitor *visitor);
};
elementb.h
class ElementB : public IElement
{
public:
virtual void accept(IVisitor *visitor);
};
If I want to add more elements I will have to add more methods do IVisitor interface.
I wish to know if it's possible to "configure" a visitor in runtime, in other words, I want to know if there are any solution to emulate the act of adding more methods to the IVisitor interface by configuring it just like I did to Factory pattern and, if so, which will be the possible solutions.
The action (visitor) object you want to pass around to objects will have to have hardwired at least knowledge of some common base class whose functionality it can use, and then as I see it there's not much point in dynamically registering visitable classes, because you need a dynamic dispatch anyway, e.g. dynamic_cast, and with that the need to list all supported classes in the common visitor interface, disappears.
Consider first a slight refactoring of your visitor pattern code – except for generality and naming and access it's the same as your code:
// Static visitor pattern.
template< class Visitable >
class Visitor_
{
template< class > friend class Visitable_impl_;
private:
virtual void visit( Visitable& ) {}
};
class A;
class B;
class I_visitor
: public Visitor_<A>
, public Visitor_<B>
{};
class I_visitable
{
public:
virtual void accept( I_visitor& ) = 0;
};
template< class Visitable >
class Visitable_impl_
: public I_visitable
{
public:
void accept( I_visitor& v )
override
{
static_cast<Visitor_<Visitable>&>( v ) // Cast for access.
.visit( static_cast<Visitable&>( *this ) ); // Cast for overload res.
}
};
class A: public Visitable_impl_<A> {};
class B: public Visitable_impl_<B> {};
#include <iostream>
using namespace std;
auto main()
-> int
{
class Action
: public I_visitor
{
private:
void visit( A& ) override { cout << "Visited an A." << endl; }
};
I_visitable&& a = A();
I_visitable&& b = B();
Action x;
a.accept( x ); b.accept( x );
}
Now we just replace the first static_cast with a dynamic_cast, and voilà:
// Dynamic visitor pattern.
template< class Visitable >
class Visitor_
{
template< class > friend class Visitable_impl_;
private:
virtual void visit( Visitable& ) {}
};
struct I_visitor { virtual ~I_visitor(){} }; // Note: no mention of A or B.
class I_visitable
{
public:
virtual void accept( I_visitor& ) = 0;
};
template< class Visitable >
class Visitable_impl_
: public I_visitable
{
public:
void accept( I_visitor& v )
override
{
if( auto p_visitor = dynamic_cast<Visitor_<Visitable>*>( &v ) )
{
p_visitor->visit( static_cast<Visitable&>( *this ) ); // Cast for overload res.
}
}
};
class A: public Visitable_impl_<A> {};
class B: public Visitable_impl_<B> {};
#include <iostream>
using namespace std;
auto main()
-> int
{
class Action
: public I_visitor
, public Visitor_<A>
{
private:
void visit( A& ) override { cout << "Visited an A." << endl; }
};
I_visitable&& a = A();
I_visitable&& b = B();
Action x;
a.accept( x ); b.accept( x );
}
How can one implement a method which calls another method recursively for all base classes? Conceptually:
class C {
public:
magical void recursiveWork() {
thisClass::doWork();
if (baseClassExists) {
baseClass::recursiveWork();
}
}
void doWork() {
printf("bar");
}
}
class D : public C {
public:
void doWork() {
printf("foo");
}
}
D d;
d.recursiveWork(); // prints "foobar"
You could use something to indicate the base class, like base_type below, and then recursively check if DoIt exists. (in this case I checked if base_type is different from self, in case of the most base class). Of course, it won't work with multiple inheritance (it should have something to identify all base_type's), and be careful with object slicing when going up on base classes.
#include <iostream>
#include <utility>
class A1
{
public:
using base_type = A1;
virtual ~A1(){}
virtual void DoIt(){ std::cout << "A1\n"; }
};
class A : public A1
{
public:
using base_type = A1;
virtual void DoIt(){
std::cout << "A\n";
}
};
class B : public A
{
public:
using base_type = A;
virtual void DoIt(){
std::cout << "B\n";
}
};
template<class...> using void_t = void;
template<class, class = void>
struct has_do_it : std::false_type { constexpr static bool value = false; };
template<class T>
struct has_do_it<T, void_t<decltype(std::declval<T&>().DoIt())>> : std::true_type { constexpr static bool value = true; };
template<typename T>
void DoItRec(T t)
{
if (has_do_it<T>::value) {
t.DoIt();
if(!std::is_same<T, typename T::base_type>::value)
{
typename T::base_type& base = t; // get base part (object slicing)
DoItRec(base);
}
}
}
int main()
{
B b;
DoItRec(b);
return 0;
}
Live example
Is there any way to do this automatically for all derived classes, that i don't have to create function applyPack for all nested classes.
This is piece of my code:
/** every class has registered id with this function */
template<typename T>
uint getID() {
static uint id = registerClass<T>();
return id;
}
class TemplatesPack {
public:
template<typename T>
typename T::Template get();
};
class Object {
public:
virtual void applyPack(TemplatesPack *p) { this->setTemplate(p->get<Object>()); };
};
class Object2: public Object {
public:
void applyPack(TemplatesPack *p) { this->setTemplate(p->get<Object2>()); };
};
class Object3: public Object {
public:
void applyPack(TemplatesPack *p) { this->setTemplate(p->get<Object3>()); };
};
class Object4: public Object2 {
public:
void applyPack(TemplatesPack *p) { this->setTemplate(p->get<Object4>()); };
};
I've read something about type traits but i don't want to have class Object templated. Can be this done with c++ and templating some functions in class TemplatesPack or with c++0x? s
Edit:
changed the answer to make the Object untouched.
template<class T>
class Base<T> : public Object
{
public:
virtual void applyPack(TemplatePack *p) { this->setTemplate(p->get<T>()); };
};
class Object2 : public Base<Object2>
{
// ...
};
Edit: for the case of Object4, maybe the following will help:
template<class S, class D>
class Base<S, D> : public S
{
public:
virtual void applyPack(TemplatePack *p) { this->setTemplate(p->get<D>()); };
};
class Object2 : public Base<Object, Object2> { /* ... */ };
class Object3 : public Base<Object, Object3> { /* ... */ };
class Object4 : public Base<Object2, Object4> { /* ... */ };
You could use virtual inheritance and the dominance rule, if you don't want to templatize Base
template<typename Derived, typename Base = void>
struct applyer : virtual applyer<Base, typename Base::base_type> {
virtual void applyPack(TemplatesPack *p) {
dynamic_cast<Derived*>(this)->setTemplate(p->get<Derived>());
};
typedef Base base_type;
};
template<typename Derived>
struct applyer<Derived, void> {
virtual void applyPack(TemplatesPack *p) {
dynamic_cast<Derived*>(this)->setTemplate(p->get<Derived>());
};
};
Now you can do it as follows
class Object : virtual public applyer<Object> {
};
class Object2: public Object, virtual public applyer<Object2, Object> {
};
class Object3: public Object, virtual public applyer<Object3, Object> {
};
The second argument respectively is the direct base class, which can be omitted if there is none. For instance if you derive from Object3, you need to do that as follows
class Object3_1: public Object3, virtual public applyer<Object3_1, Object3> {
};