Templated Proxy design pattern

♀尐吖头ヾ 提交于 2019-12-13 09:02:59

问题


I have a simple and reproducible code, which looks like so:

template <typename T>
class Proxy {
private:
    Wrap<T> &self; // If I comment this, it will work
public:
    Proxy() {}
};

template <typename T>
class Wrap {
    T *p;
public:
    Proxy<T> foo() {
        return Proxy<T>();
    }
};

int main()
{
    return 0;
}

The error I get is:

'Wrap' does not name a type

If I comment Wrap<T> &self, then it will work, but this is not what I need. I need Wrap<T> to be a member of Proxy class. How can I achieve this?


回答1:


You could add forward declaration of Wrap before the class definition of Proxy, otherwise the compiler can't know that it's the name of a class template. It's worth noting that reference data member doesn't require the type to be complete type, so forward declaration is enough here.

// forward declaration
template <typename T>
class Wrap;

template <typename T>
class Proxy {
private:
    Wrap<T> &self;
public:
    Proxy() {}
};

BTW the following issue would be that the reference data member self is not initialized.

LIVE of Clang

error: constructor for 'Proxy' must explicitly initialize the reference member 'self'




回答2:


You need to add a forward declaration:

template<class T> class Wrap;

template <typename T>
class Proxy {
private:
    Wrap<T> &self;
public:
    Proxy() {}
};

template <typename T>
class Wrap {
    T *p;
public:
    Proxy<T> foo() {
        return Proxy<T>();
    }
};

int main()
{
    return 0;
}

The code is just copied from the OP's question. But you still need to pay a attention to member Wrap<T> &self. As a reference, it should be initialized in constructor.



来源:https://stackoverflow.com/questions/48608414/templated-proxy-design-pattern

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!