www.digitalmars.com         C & C++   DMDScript  

c++ - error when accessing a base class

reply Alok <alokgovil hotmail.com> writes:
Hi,

With reference to the following FAQ item:

 The compiler gives me an error when accessing a base class?

 With the code:
 template <class T> struct Base
 {
       int m_member;
 };

 template <class T> struct Derived : public Base<T>
 {
       Derived() : m_member(0) {}
 };

 the compiler gives an error that the m_member or any other
 members of Base<T> are not found. Although other compilers accept
 such code, it is incorrect according to the C++98 Standard
 14.6.2-2 and 14.6.2.1-1. Base<T> is a dependent type, and so it
 is not in scope for template class Derived.
As I understand, base class members are not accessible only when the derived class (template) has members with the same name. Is this incorrect? Being unable to access base class sounds naive! Best regards - Alok
Aug 20 2007
next sibling parent Walter Bright <newshound1 digitalmars.com> writes:
Alok wrote:
 Hi,
 
 With reference to the following FAQ item:
 
 The compiler gives me an error when accessing a base class?

 With the code:
 template <class T> struct Base
 {
       int m_member;
 };

 template <class T> struct Derived : public Base<T>
 {
       Derived() : m_member(0) {}
 };

 the compiler gives an error that the m_member or any other
 members of Base<T> are not found. Although other compilers accept
 such code, it is incorrect according to the C++98 Standard
 14.6.2-2 and 14.6.2.1-1. Base<T> is a dependent type, and so it
 is not in scope for template class Derived.
As I understand, base class members are not accessible only when the derived class (template) has members with the same name. Is this incorrect? Being unable to access base class sounds naive!
It's incorrect when talking about template classes. See the references in C++98.
Aug 25 2007
prev sibling parent =?UTF-8?B?RGllZ28gU8OhbmNoZXo=?= <diegos intersoft.com.ar> writes:
Alok wrote:
 Hi,
 
 With reference to the following FAQ item:
 
 The compiler gives me an error when accessing a base class?

 With the code:
 template <class T> struct Base
 {
       int m_member;
 };

 template <class T> struct Derived : public Base<T>
 {
       Derived() : m_member(0) {}
 };

 the compiler gives an error that the m_member or any other
 members of Base<T> are not found. Although other compilers accept
 such code, it is incorrect according to the C++98 Standard
 14.6.2-2 and 14.6.2.1-1. Base<T> is a dependent type, and so it
 is not in scope for template class Derived.
As I understand, base class members are not accessible only when the derived class (template) has members with the same name. Is this incorrect? Being unable to access base class sounds naive! Best regards - Alok
There are some problems: 1) Neigther 'Base' nor 'Derived' (This types are structers) 2) The Base's member 'm_member' is private. 3) The error is beacause when you create a instance of Derived this struct/class haven't a public constructor. Diego
Aug 30 2007