473,385 Members | 1,453 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

protected

When I try to compile the code below with gcc 3.4.1, I get an error.
In order to get the code to compile, I have to specify i's scope by
prepending 'B::' to 'i' in function D::F(). Can someone tell me why
this is true?

The non-template version of this class hierarchy works without the
scope specifier on 'i'.

template<typename T>
class B {
public:
virtual void F () { }
T i;
};

template<typename T>
class D: public B<T> {
public:
virtual void F () { i = 1; } // error: 'i' undeclared
// but 'B::i = 1;' works
};

int main ()
{
D<unsigned> d;
d.F ();
return 0;
}

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #1
5 1229
"Jeff Perry" <js*@mail.utexas.edu> wrote...
When I try to compile the code below with gcc 3.4.1, I get an error.
In order to get the code to compile, I have to specify i's scope by
prepending 'B::' to 'i' in function D::F(). Can someone tell me why
this is true?

The non-template version of this class hierarchy works without the
scope specifier on 'i'.

template<typename T>
class B {
public:
virtual void F () { }
T i;
};

template<typename T>
class D: public B<T> {
public:
virtual void F () { i = 1; } // error: 'i' undeclared
// but 'B::i = 1;' works
'i' is what is known as "dependent name". With templates the scope
of the base class is not searched for name resolution unless you
specifically instruct the compiler to do so by either doing B:: or
this->.
};

int main ()
{
D<unsigned> d;
d.F ();
return 0;
}


V
Jul 22 '05 #2
On 8/3/2004 1:28 AM, Jeff Perry wrote:
When I try to compile the code below with gcc 3.4.1, I get an error.
In order to get the code to compile, I have to specify i's scope by
prepending 'B::' to 'i' in function D::F(). Can someone tell me why
this is true?
I think it is true. In dynamic polymorphism (virtual)
compiler need in that template qualified name
using :: operator.
template<typename T>
class B {
public:
virtual void F () { }
T i;
};


Why you defined F() that way ?
Shouldn't it be pure virtual function ?

virtual void F () = 0;

Regards

--

Mateusz Łoskot
mateusz at loskot dot net

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #3

"Jeff Perry" <js*@mail.utexas.edu> wrote in message
news:18**************************@posting.google.c om...
template<typename T>
class B {
public:
virtual void F () { }
T i;
};

template<typename T>
class D: public B<T> {
public:
virtual void F () { i = 1; } // error: 'i' undeclared
// but 'B::i = 1;' works
};

int main ()
{
D<unsigned> d;
d.F ();
return 0;
}


This issue is now covered by the C++ FAQ:
http://www.parashift.com/c++-faq-lit...html#faq-34.17

Regards,
Sumit.
--
Sumit Rajan <su********@myrealbox.com>

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #4

"Jeff Perry" <js*@mail.utexas.edu> wrote in message
news:18**************************@posting.google.c om...
When I try to compile the code below with gcc 3.4.1, I get an error.
In order to get the code to compile, I have to specify i's scope by
prepending 'B::' to 'i' in function D::F(). Can someone tell me why
this is true?


That's because gcc 3.4.1 implements the two phase name lookup. Changing i to
B<T>::i or this->i makes it a dependent name and delays its lookup. Read
this FAQ -
http://www.parashift.com/c++-faq-lit....html#faq-34.1
7

-Sharad

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #5
Jeff Perry wrote:
When I try to compile the code below with gcc 3.4.1, I get an error.
In order to get the code to compile, I have to specify i's scope by
prepending 'B::' to 'i' in function D::F(). Can someone tell me why
this is true?

<snip>

See question 1 at
<http://www.decadentplace.org.uk/womble/cplusplus/template-faq.html>.

(I've just uploaded that FAQ after working on it occasionally over the
last few months. I'd appreciate comments on anything that should be
added or corrected.)

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: Act | last post by:
Why is it suggested to not define data members as "protected"? Thanks for help!
2
by: yccheok | last post by:
hello, in a singleton design, i trying to make my parent class having protected constructor and destructor. however, the compiler give me error when my child, trying to delete itself through parent...
8
by: Carlos J. Quintero | last post by:
Hi, As you know the current keywords "protected internal" (C#) or "Protected Friend" (VB.Net) means "Protected Or internal" (C#) or "Protected Or Friend" (VB.Net), that is, the member is...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
4
by: Tina | last post by:
This is an issue regarding what exactly is accomplished by using "Protected" when defining a variable. It seems it does much more than just applying Protected status to a variable. I have an...
14
by: mlimber | last post by:
In an article on the safe bool idiom (http://www.artima.com/cppsource/safeboolP.html), Bjorn Karlsson gives the following code (slightly modified): class safe_bool_base { protected: typedef...
6
by: Rick | last post by:
Hi, Can anyone explain to me why the below fails to compile - seeing otherA->f(); as a call to a inaccessible function, while otherB->f(); is ok? It seems you can happily access protected...
16
by: Fir5tSight | last post by:
Hi All, I have a small C#.NET program that is as follows: using System; class A { protected int x = 123; }
13
by: Clive Dixon | last post by:
I am refactoring some code to move some base classes into a separate assembly. One of these base classes has a member property which is 'protected internal'. However when I move these base classes...
8
by: Mayur H Chauhan | last post by:
All, For my knowledge, if I declare Class as follow, then it thows compilation error. Protected Class Book End Class Even same for...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.