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

You can use this make clear that you are referring to a member of the class: void Bar::BarFunc () { std::cout _foo_arg.

You can use this-> make clear that you are referring to a member of the class: void Bar::BarFunc () { std::cout _foo_arg.

The link to the Faq is very useful: it also shows where this issue can unvisibly cause unwanted behaviour. – xtofl Jul 13 '09 at 19:00 Any idea why this is true? (the FAQ does not answer this completely) – Catskul Oct 1 '10 at 1:26.

Appears to work fine in Visual C++ 2008. I've added some dummy definitions for the types you mentioned but gave no source for. The rest is exactly as you put it.

Then a main function to force BarFunc to be instantiated and called. #include class streamable {}; std::ostream &operator 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 Bar : public Foo { public: Bar (const foo_arg_t bar_arg, const a_arg_t a_arg) : Foo(bar_arg) // base-class initializer { Foo::Foo_T = T(a_arg); } Bar (const foo_arg_t bar_arg, const b_arg_t b_arg) : Foo(bar_arg) { Foo::Foo_T = T(b_arg); } void BarFunc (); }; template void Bar::BarFunc () { std::cout BarFunc(); }.

G++ hands out a lot of errors regarding the definitions up top. However, the scope problem still remains with: "error: ‘_foo_arg’ was not declared in this scope," due to the first call to _foo_arg in the BarFunc() definition. – Shamster Jul 13 '09 at 17:41 Do you mean my dummy type declarations give you errors on gcc?

– Daniel Earwicker Jul 13 '09 at 17:51 yes, the dummy types at top, but the scope error remains, as well. – Shamster Jul 13 '09 at 17:52 I believe g++ may be correct re: your original issue. The IBM compiler kicked up the same fuss, IIRC.

– Daniel Earwicker Jul 13 '09 at 17:53 What are the error messages? – Daniel Earwicker Jul 13 '09 at 17:54.

Using this-> as the other answers state works. However, if you're calling a derived member function with this->_foo_arg as argument, i.e. , printValue( this->_foo_arg ); where the header for printValue uses a call-by-reference parameter printValue( const foo_arg_t &valueToPrint ); you might get a compiler error.To avoid that, try using call-by-value: printValue( foo_arg_t valueToPrint ).

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::_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?

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

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions