Derived template-class access to base-class member-data

前端 未结 3 646
天涯浪人
天涯浪人 2020-11-22 00:45

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

Using the following class definitions:

template 
class Foo {

public:
         


        
3条回答
  •  长发绾君心
    2020-11-22 01:24

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

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

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

    void Bar::BarFunc () {
        using Bar::_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.

提交回复
热议问题