Accessing protected fields in subclass?

C

carl

I have a base class with a protected field. I have created a subclass where
I would like to access and use this protected field:


// Base class
class R {
protected:
std::string image_path;
};


// Subclass
class L : public R {
public:
void test(){
std::cout << image_path << std::endl;
}

};

But that is a no go, it complains that the image_path field is unknown. In
the book Accelerated C++ pp 229 it says that subclasses can see protected
fields from the base class so what is wrong with the above?
 
C

carl

Christian Hackl said:
carl ha scritto:


The code above is correct, and even the most broken compiler should accept
it, so I assume you have not posted your actual code.



I use g++ 4.4.1 on Ubuntu 9.10 and the error is:

error: 'image_path' was not declared in this scope


I have also tried to declare test() in the base class but that does not
help.
 
P

Paul Bibbings

carl said:
I have a base class with a protected field. I have created a subclass
where I would like to access and use this protected field:


// Base class
class R {
protected:
std::string image_path;
};


// Subclass
class L : public R {
public:
void test(){
std::cout << image_path << std::endl;
}

};

But that is a no go, it complains that the image_path field is
unknown. In the book Accelerated C++ pp 229 it says that subclasses
can see protected fields from the base class so what is wrong with the
above?

11:56:20 Paul Bibbings@JIJOU
/cygdrive/d/CPPProjects/CLCPP $cat file.cpp
#include <string>
#include <iostream>

// Base class
class R {
protected:
std::string image_path;
};


// Subclass
class L : public R {
public:
void test(){
std::cout << image_path << std::endl;
}
};

int main()
{
return 0;
}


11:56:33 Paul Bibbings@JIJOU
/cygdrive/d/CPPProjects/CLCPP $i686-pc-cygwin-g++-4.4.1 -o test file.cpp

11:56:53 Paul Bibbings@JIJOU
/cygdrive/d/CPPProjects/CLCPP $ls -l | grep test
-rwxr-xr-x+ 1 Paul Bibbings None 2103026 Mar 1 11:56 test.exe

Works for me. I would suggest that, in your *actual* code, you are
doing something importantly different to what is suggested by the code
you have posted here.

Regards

Paul Bibbings
 
M

Michael Doubez

carl ha scritto:






The code compiles fine even with a lower version of g++. So, again, you
have not posted the same code that you feed to your compiler. You
probably got something wrong with include files. Perhaps you are
including a wrong version of R which does not have any image_path field.

Or he has a name dependent lookup failure; this would be the case if
the class were templated.
template<class T>
class R
{...};

template<class T>
class L : public R {
public:
void test(){
// image_path is not known in scope: use 'this->image_path' instead
std::cout << image_path << std::endl;
}
};
 
C

carl

carl ha scritto:






The code compiles fine even with a lower version of g++. So, again, you
have not posted the same code that you feed to your compiler. You
probably got something wrong with include files. Perhaps you are
including a wrong version of R which does not have any image_path field.

Or he has a name dependent lookup failure; this would be the case if
the class were templated.
template<class T>
class R
{...};

template<class T>
class L : public R {
public:
void test(){
// image_path is not known in scope: use 'this->image_path' instead
std::cout << image_path << std::endl;
}
};



Thanks that solved the problem! I omitted the template declariation since I
was trying to make as minimal an example as possible but unfortunately that
was exactly where the error was!

But why is it necessay to access the protected field through the this
pointer when using templates?
 
R

Robert Fendt

Thanks that solved the problem! I omitted the template declariation since I
was trying to make as minimal an example as possible but unfortunately that
was exactly where the error was!

This is precisely the reason why *two* people already suggested
that the code you posted is not the code you where trying to
compile. In such a case it is extremely helpful (i.e.,
necessary) to post code that actually shows the problem: among
other things, it avoids a lot of guessing. So please do so next
time, right from the start. It also enables people to help you
much quicker.
But why is it necessay to access the protected field through the this
pointer when using templates?

Because in the case of templates and inheritance, the name
lookup mechanism gets in your way. "image_path" is not marked as
a name depending on the template parameter, so the compiler
tries to resolve it before even trying to instantiate the
template (and thus looking at any inherited fields).

There is more than one way to tell the compiler that a name is a
'dependent name', i.e. it should wait and see if it can resolve
the name depending on the template's definition, parameters and
base classes (if any). Using "this->" is one of the simplest
(and it does not meddle with virtual calls and such, so it is
usually the recommended way). You might also want to have a look
at

http://www.parashift.com/c++-faq-lite/templates.html#faq-35.19

Regards,
Robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top