Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > portected access: template derivation vs. class derivation

Reply
Thread Tools

portected access: template derivation vs. class derivation

 
 
Steven T. Hatton
Guest
Posts: n/a
 
      08-19-2004
In the following code, the class B will not compile if the function da is
uncommented. I took exactly the same code, and turned it into templates.
Never used the template parameter in anything but the baseclass name in the
derived class, and the code compiles without any complaints about protected
access. Why? What is different about the templated code?

class A{
public:
A(const int& x_=0): x(x_){}
protected:
int x;
};

class B: public A{
public:
B(const int& x_=0): A(x_){}
/*
B& da(const int& da_)
{
this->a.x += da_; // error: protected access
return *this;
}
*/
protected:
A a;
};

/*Same as above but with templates*/
template<typename T>
class AT{
public:
AT(const int& x_=0): x(x_){}
protected:
int x;
};

template<typename T>
class BT: public AT<T>{
public:
BT(const int& x_=0): AT<T>(x_){}

BT& da(const int& da_)
{
this->a.x += da_; // works fine
return *this;
}

protected:
AT<T> a;
};

--
STH
Hatton's Law: "There is only One inviolable Law"
KDevelop: http://www.kdevelop.org SuSE: http://www.suse.com
Mozilla: http://www.mozilla.org
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-19-2004
Steven T. Hatton wrote:
> In the following code, the class B will not compile if the function da is
> uncommented. I took exactly the same code, and turned it into templates.
> Never used the template parameter in anything but the baseclass name in the
> derived class, and the code compiles without any complaints about protected
> access. Why? What is different about the templated code?


The templated code is not really "compiled" unless you attempt to
instantiate the template. Did you? Where is the rest of the code?
Was there anything else? If there wasn't, then all the compiler did
for your templates was checking the syntax. Names were not resolved,
access rights weren't checked, etc.

>
> class A{
> public:
> A(const int& x_=0): x(x_){}
> protected:
> int x;
> };
>
> class B: public A{
> public:
> B(const int& x_=0): A(x_){}
> /*
> B& da(const int& da_)
> {
> this->a.x += da_; // error: protected access
> return *this;
> }
> */
> protected:
> A a;
> };
>
> /*Same as above but with templates*/
> template<typename T>
> class AT{
> public:
> AT(const int& x_=0): x(x_){}
> protected:
> int x;
> };
>
> template<typename T>
> class BT: public AT<T>{
> public:
> BT(const int& x_=0): AT<T>(x_){}
>
> BT& da(const int& da_)
> {
> this->a.x += da_; // works fine
> return *this;
> }
>
> protected:
> AT<T> a;
> };
>


V
 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      08-19-2004
Steven T. Hatton wrote:

> In the following code, the class B will not compile if the function da is
> uncommented. I took exactly the same code, and turned it into templates.
> Never used the template parameter in anything but the baseclass name in
> the derived class, and the code compiles without any complaints about
> protected
> access. Why? What is different about the templated code?
>
> class A{
> public:
> A(const int& x_=0): x(x_){}
> protected:
> int x;
> };
>
> class B: public A{
> public:
> B(const int& x_=0): A(x_){}
> /*
> B& da(const int& da_)
> {
> this->a.x += da_; // error: protected access
> return *this;
> }
> */
> protected:
> A a;
> };
>
> /*Same as above but with templates*/
> template<typename T>
> class AT{
> public:
> AT(const int& x_=0): x(x_){}
> protected:
> int x;
> };
>
> template<typename T>
> class BT: public AT<T>{
> public:
> BT(const int& x_=0): AT<T>(x_){}
>
> BT& da(const int& da_)
> {
> this->a.x += da_; // works fine
> return *this;
> }
>
> protected:
> AT<T> a;
> };
>


Hi,


did you actually instantiate the template in main(), or did you just
compile this file as a compilation unit? In the later case, it would be
unwise for the compiler to complain: If this template is used as part of a
bigger programm, some other part might provide a specialization for AT<>
where the member x is not protected. For those types, BT<> should not cause
any troubles at all. Beware that the interface of AT<T> may depend on T.

Rest assured, you will see tons of inscrutable error messages when you
actually instantiate the template so that access violations happen.


Best

Kai-Uwe Bux
 
Reply With Quote
 
Jonathan Turkanis
Guest
Posts: n/a
 
      08-19-2004

"Steven T. Hatton" <> wrote in message
news:uIOdneY0ZqPkmrjcRVn-...
> In the following code, the class B will not compile if the function

da is
> uncommented. I took exactly the same code, and turned it into

templates.
> Never used the template parameter in anything but the baseclass name

in the
> derived class, and the code compiles without any complaints about

protected
> access. Why? What is different about the templated code?


Try compiling your code with this main function:

int main()
{
BT<int> b;
b.da(int());
}

You should get the same error. The reason the templated version
compiles in your original that dependent names (such as this->a)
aren't looked up until instantiation, and that no instantation occurs
in your example.

Jonathan


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      08-19-2004
Jonathan Turkanis wrote:
> "Victor Bazarov" <> wrote in message
> news:SS7Vc.111$ o.net...
>
>>Steven T. Hatton wrote:

>
>
>
>>The templated code is not really "compiled" unless you attempt to
>>instantiate the template. Did you?

>
>
> So is the dependent/non-dependent distinction a red herring here?


I am not sure I understand the question. Could you rephrase it?

V
 
Reply With Quote
 
Steven T. Hatton
Guest
Posts: n/a
 
      08-19-2004
Victor Bazarov wrote:

> Steven T. Hatton wrote:
>> In the following code, the class B will not compile if the function da is
>> uncommented. I took exactly the same code, and turned it into templates.
>> Never used the template parameter in anything but the baseclass name in
>> the derived class, and the code compiles without any complaints about
>> protected
>> access. Why? What is different about the templated code?

>
> The templated code is not really "compiled" unless you attempt to
> instantiate the template. Did you? Where is the rest of the code?
> Was there anything else? If there wasn't, then all the compiler did
> for your templates was checking the syntax. Names were not resolved,
> access rights weren't checked, etc.


Yes, but your question gave me an idea I hadn't tried. I hadn't actually
invoke any functions on the template. When I did, the compiler rejected it.

int main() {
BT<int> bt(5);
bt.da(1); // comment this out and it compiles
}

g++ -omain2 main2.cc
main2.cc: In member function `BT<T>& BT<T>::da(const int&) [with T = int]':
main2.cc:51: instantiated from here
main2.cc:30: error: `int AT<int>:' is protected
main2.cc:40: error: within this context

--
STH
Hatton's Law: "There is only One inviolable Law"
KDevelop: http://www.kdevelop.org SuSE: http://www.suse.com
Mozilla: http://www.mozilla.org
 
Reply With Quote
 
Jonathan Turkanis
Guest
Posts: n/a
 
      08-19-2004

"Victor Bazarov" <> wrote in message
news:SS7Vc.111$ o.net...
> Steven T. Hatton wrote:



> The templated code is not really "compiled" unless you attempt to
> instantiate the template. Did you?


So is the dependent/non-dependent distinction a red herring here?

> Where is the rest of the code?
> Was there anything else? If there wasn't, then all the compiler did
> for your templates was checking the syntax. Names were not

resolved,
> access rights weren't checked, etc.


Jonathan


 
Reply With Quote
 
Jonathan Turkanis
Guest
Posts: n/a
 
      08-19-2004

"Victor Bazarov" <> wrote in message
news:928Vc.114$ o.net...
> Jonathan Turkanis wrote:


> > So is the dependent/non-dependent distinction a red herring here?

>
> I am not sure I understand the question. Could you rephrase it?


Yes. I thought at first the reason access was not being checked was
that in the line

this->a.x += da_

the name a is qualified. But I see that Comeau compiles the code
wuthout the 'this->', as long as the template is not instantiated.

Jonathan


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      08-19-2004
Steven T. Hatton wrote:
> Victor Bazarov wrote:
>
>
>>Steven T. Hatton wrote:
>>
>>>In the following code, the class B will not compile if the function da is
>>>uncommented. I took exactly the same code, and turned it into templates.
>>>Never used the template parameter in anything but the baseclass name in
>>>the derived class, and the code compiles without any complaints about
>>>protected
>>>access. Why? What is different about the templated code?

>>
>>The templated code is not really "compiled" unless you attempt to
>>instantiate the template. Did you? Where is the rest of the code?
>>Was there anything else? If there wasn't, then all the compiler did
>>for your templates was checking the syntax. Names were not resolved,
>>access rights weren't checked, etc.

>
>
> Yes, but your question gave me an idea I hadn't tried. I hadn't actually
> invoke any functions on the template. When I did, the compiler rejected it.
>
> int main() {
> BT<int> bt(5);
> bt.da(1); // comment this out and it compiles
> }


Right. Instantiating a class template is not the same as instantiating
a member of a class template. If you don't call the function, it won't
be instantiated, and will not really be compiled (names resolved, access
checked, etc). So, to make sure a member of a template class is correct,
you got to try to use that member.

V
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      08-19-2004
Jonathan Turkanis wrote:
> "Victor Bazarov" <> wrote in message
> news:928Vc.114$ o.net...
>
>>Jonathan Turkanis wrote:

>
>
>>>So is the dependent/non-dependent distinction a red herring here?

>>
>>I am not sure I understand the question. Could you rephrase it?

>
>
> Yes. I thought at first the reason access was not being checked was
> that in the line
>
> this->a.x += da_
>
> the name a is qualified. But I see that Comeau compiles the code
> wuthout the 'this->', as long as the template is not instantiated.


Aha. So, dependent/non-dependent distinction is not a red herring,
then, right?

V
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Declaring a template class with two template params a friend in anon-template class A L C++ 1 08-25-2010 07:25 AM
Operator overloading vs. class derivation/inheritance Massimo Soricetti C++ 6 02-02-2006 06:22 PM
derivation of a template class maadhuu C++ 3 05-17-2005 08:39 PM
A parameterized class (i.e. template class / class template) is not a class? christopher diggins C++ 16 05-04-2005 12:26 AM
Why is Python not supporting full derivation of built-in file class? Pierre Rouleau Python 2 04-25-2005 01:31 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57