使用父类定义的类型的基类

发布于 2024-10-06 18:33:25 字数 1101 浏览 2 评论 0原文

我有一个 Visual Studio 2008 C++ 应用程序,其中基类 A_Base 需要实例化其类型由父类定义的数据成员。例如:

template< typename T >
class A_Base
{
public: 
    typedef typename T::Foo Bar; // line 10

private:
    Bar bar_;
};

class A : public A_Base< A > 
{
public:
    typedef int Foo;
};

int _tmain( int argc, _TCHAR* argv[] )
{
    A a;
return 0;
}

不幸的是,编译器似乎不知道 T::Foo 是什么,直到为时已晚,我收到如下错误:

1>MyApp.cpp(10) : error C2039: 'Foo' : is not a member of 'A'
1>        MyApp.cpp(13) : see declaration of 'A'
1>        MyApp.cpp(14) : see reference to class template instantiation 'A_Base<T>' being compiled
1>        with
1>        [
1>            T=A
1>        ]
1>MyApp.cpp(10) : error C2146: syntax error : missing ';' before identifier 'Bar'
1>MyApp.cpp(10) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>MyApp.cpp(10) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

有没有办法实现这种类型的功能?

谢谢, 保罗·H

I have a Visual Studio 2008 C++ application where a base class A_Base needs to instantiate a data member whose type is defined by a parent class. For example:

template< typename T >
class A_Base
{
public: 
    typedef typename T::Foo Bar; // line 10

private:
    Bar bar_;
};

class A : public A_Base< A > 
{
public:
    typedef int Foo;
};

int _tmain( int argc, _TCHAR* argv[] )
{
    A a;
return 0;
}

Unfortunately, it appears the compiler doesn't know what T::Foo is until it's too late and I get errors like this:

1>MyApp.cpp(10) : error C2039: 'Foo' : is not a member of 'A'
1>        MyApp.cpp(13) : see declaration of 'A'
1>        MyApp.cpp(14) : see reference to class template instantiation 'A_Base<T>' being compiled
1>        with
1>        [
1>            T=A
1>        ]
1>MyApp.cpp(10) : error C2146: syntax error : missing ';' before identifier 'Bar'
1>MyApp.cpp(10) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>MyApp.cpp(10) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

Is there any way to achieve this type of functionality?

Thanks,
PaulH

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

平安喜乐 2024-10-13 18:33:25

A_BaseA 尚未完成的点实例化:

class A : public A_Base< A >

您可以考虑使用特征类:

template<class T> struct traits;

template< typename T >
class A_Base
{
public: 
    typedef typename traits<T>::Foo Bar; // line 10

private:
    Bar bar_;
};

class A; // Forward declare A

template<> // Specialize traits class for A
struct traits<A>
{
    typedef int Foo;
};

class A : public A_Base< A > {};

int main()
{
    A a;
}

A_Base<A> is instantiated at a point where A is not complete yet :

class A : public A_Base< A >

You could consider using a traits class :

template<class T> struct traits;

template< typename T >
class A_Base
{
public: 
    typedef typename traits<T>::Foo Bar; // line 10

private:
    Bar bar_;
};

class A; // Forward declare A

template<> // Specialize traits class for A
struct traits<A>
{
    typedef int Foo;
};

class A : public A_Base< A > {};

int main()
{
    A a;
}
吃兔兔 2024-10-13 18:33:25

您可以尝试以下操作:

template< typename T >
class A_Base
{
public: 
    typedef typename T::Foo Bar; // line 10

private:
    Bar bar_;
};

class A_Policy
{
public:
    typedef int Foo;
};

class A : public A_Base<A_Policy>
{};

int _tmain( int argc, _TCHAR* argv[] )
{
    A a;
return 0;
}

You can try the following:

template< typename T >
class A_Base
{
public: 
    typedef typename T::Foo Bar; // line 10

private:
    Bar bar_;
};

class A_Policy
{
public:
    typedef int Foo;
};

class A : public A_Base<A_Policy>
{};

int _tmain( int argc, _TCHAR* argv[] )
{
    A a;
return 0;
}
往日 2024-10-13 18:33:25

A 依赖于类 A_Base,而类 A_Base 又依赖于类 A...等等。这里有一个递归。您需要在单独的类中声明 Foo

class A;

template<typename T> struct Foo;
template<> struct Foo<A> { typedef int type; };

template< typename T >
class A_Base
{
public: 
    typedef typename Foo<T>::type Bar; // line 10

private:
    Bar bar_;
};

class A : public A_Base< A > 
{
};

另请参阅GotW #79

Class A depends on class A_Base which depends on class A... etc. You have a recursion here. You need to declare Foo in a separate class.

class A;

template<typename T> struct Foo;
template<> struct Foo<A> { typedef int type; };

template< typename T >
class A_Base
{
public: 
    typedef typename Foo<T>::type Bar; // line 10

private:
    Bar bar_;
};

class A : public A_Base< A > 
{
};

See also GotW #79.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文