派生模板类访问基类成员数据 [英] Derived template-class access to base-class member-data

查看:102
本文介绍了派生模板类访问基类成员数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是

 

code> template< class T>
class Foo {

public:
Foo(const foo_arg_t foo_arg):_foo_arg(foo_arg)
{
/ * do foo * /
}
T Foo_T; // TypeA或TypeB - TBD
foo_arg_t _foo_arg;
};

template< class T>
class Bar:public Foo< T> {
public:
Bar(const foo_arg_t bar_arg,const a_arg_t a_arg)
:Foo< T>(bar_arg)// base-class initializer
{

Foo< T> :: Foo_T = T(a_arg);
}

Bar(const foo_arg_t bar_arg,const b_arg_t b_arg)
:Foo< T>(bar_arg)
{
Foo< T> Foo_T = T(b_arg);
}

void BarFunc();

};

template< class T>
void Bar< T> :: BarFunc(){
std :: cout< _foo_arg<< std :: endl; //这不工作 - 编译器错误是:错误:'_foo_arg'未在此范围中声明
std :: cout<< Bar< T> _ _ foo_arg<< std :: endl; // This works!
}



当访问模板类的基类的成员时,我必须使用 Bar< T> :: _ foo_arg 的模板样式语法来显式限定成员。有办法避免这种情况吗?可以在模板类方法中使用using语句/指令来简化代码?



编辑:


范围问题通过使用this->语法限定变量来解决。

解决方案

可以使用 this-> ,以表明您指的是该类的成员:

  void Bar< T> :: BarFunc(){
std :: cout< this-> _foo_arg<< std :: endl;
}

或者,您也可以使用使用

  void Bar< T> :: BarFunc(){
使用Bar< ; T> :: _ foo_arg; //在g ++中不能工作,IIRC
std :: cout<< _foo_arg<< std :: endl;
}

这使得编译器清楚地知道成员名称取决于模板参数以便它在正确的位置搜索该名称的定义。有关详情,请参阅 C ++ Faq Lite中的此条目


This question is a furtherance of the one asked in this thread.

Using the following class definitions:

template <class T>
class Foo {

public:
    Foo (const foo_arg_t foo_arg) : _foo_arg(foo_arg)
    {
        /* do something for foo */
    }
    T Foo_T;        // either a TypeA or a TypeB - TBD
    foo_arg_t _foo_arg;
};

template <class T>
class Bar : public Foo<T> {
public:
    Bar (const foo_arg_t bar_arg, const a_arg_t a_arg)
    : Foo<T>(bar_arg)   // base-class initializer
    {

        Foo<T>::Foo_T = T(a_arg);
    }

    Bar (const foo_arg_t bar_arg, const b_arg_t b_arg)
    : Foo<T>(bar_arg)
    {
        Foo<T>::Foo_T = T(b_arg);
    }

    void BarFunc ();

};

template <class T>
void Bar<T>::BarFunc () {
    std::cout << _foo_arg << std::endl;   // This doesn't work - compiler error is: error: ‘_foo_arg’ was not declared in this scope
    std::cout << Bar<T>::_foo_arg << std::endl;   // This works!
}

When accessing the members of the template-class's base-class, it seems like I must always explicitly qualify the members using the template-style syntax of Bar<T>::_foo_arg. Is there a way to avoid this? Can a 'using' statement/directive come into play in a template class method to simplify the code?

Edit:

The scope issue is resolved by qualifying the variable with this-> syntax.

解决方案

You can use this-> to make clear that you are referring to a member of the class:

void Bar<T>::BarFunc () {
    std::cout << this->_foo_arg << std::endl;
}

Alternatively you can also use "using" in the method:

void Bar<T>::BarFunc () {
    using Bar<T>::_foo_arg;             // Might not work in g++, IIRC
    std::cout << _foo_arg << std::endl;
}

This makes it clear to the compiler that the member name depends on the template parameters so that it searches for the definition of that name in the right places. For more information also see this entry in the C++ Faq Lite.

这篇关于派生模板类访问基类成员数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆