ZeroCool
ZeroCool

Reputation: 1500

Recursive template classes

I have two classes that need to keep references of each other as follow:

template <typename S>
class H { 
public:
  H(const S& s): s_{s} {}
private:
  const S& s_;
}

And then

template <typename H>
class S { 
public:
  S(const H& h): h_{h} {}
private:
  const H& h_;
}

There is no way I can define anything like

Service<ItsHandler<Service<....> 

Any better idea?

Upvotes: 0

Views: 175

Answers (1)

ted
ted

Reputation: 4975

How about Abstract base classes and polymorphism?:

class baseHandler {
public:
    virtual func1() = 0;
    virtual ~baseHandler() = 0;
};

template <typename S>
class H : public baseHandler {
public:
  H(const S& s): s_{s} {}
private:
  const S& s_;
};

//template <typename H>
class S {
public:
  S(const baseHandler& h): h_{h} {}
private:
  const baseHandler& h_;
};

Upvotes: 3

Related Questions