Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to get address of templated object?

Reply
Thread Tools

How to get address of templated object?

 
 
Rickarazzi@comcast.net
Guest
Posts: n/a
 
      02-10-2007
This is a problem that arose while using GNU G++ 3.4.5 under Linux.
The problem is: How to get a pointer value from a templated object
inside a class? Normally, I would add an '&' can carry on. This does
not seem to work.

Below is the example I used and some of the things I tried and the gnu
errors I got. There are only two classes, Base and Derived, where
Base defines the buffer, and Derived tries to set a pointer to it.

Is this a gnu g++ issue or is there some syntax I missing?

Rick


template <typename CharT>
class Base
{
public:
CharT buffer_;
};


template <typename CharT>
class Derived: public Base<CharT>
{
public:
Derived( void)
{
CharT * ptr = & buffer_; // error: `buffer_' was not
declared in this scope
CharT * ptr = & Base<CharT>::buffer_; // error: cannot
convert `char Base<char>::*' to `char*' in initialization
CharT * ptr = & (Base<CharT>::buffer_); // error: same as
last one

// This works but sure is ugly
CharT & ref = Base<CharT>::buffer_;
CharT * ptr = & ref;
}
};


int main( void)
{
Derived<char> dah;
}

 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      02-10-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> template <typename CharT>
> class *Base
> {
> public:
> CharT *buffer_;
> };
>
>
> template <typename CharT>
> class *Derived: * public Base<CharT>
> {
> public:
> Derived( void)
> {
> CharT * ptr = *& buffer_; * // error: `buffer_' was not
> declared in this scope
> CharT * ptr = *& Base<CharT>::buffer_; *// error: cannot
> convert `char Base<char>::*' to `char*' in initialization
> CharT * ptr = *& (Base<CharT>::buffer_); // error: *same as
> last one
>
> // This works but sure is ugly
> CharT & ref = *Base<CharT>::buffer_;
> CharT * ptr = & ref;


try:

CharT * ptr = & this->buffer_;


> }
> };
>
>
> int main( void)
> {
> Derived<char> dah;
> }



Best

Kai-Uwe Bux
 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      02-10-2007
(E-Mail Removed) wrote:
> This is a problem that arose while using GNU G++ 3.4.5 under Linux.
> The problem is: How to get a pointer value from a templated object
> inside a class? Normally, I would add an '&' can carry on. This does
> not seem to work.


If the member is in another dependant base class, then the compiler
can't deduce what buffer_ is and so you get an error. Using, this->,
while redundant in non templated classes, tells the compiler that
buffer_ is dependant on the template parameters.

>
> Below is the example I used and some of the things I tried and the gnu
> errors I got. There are only two classes, Base and Derived, where
> Base defines the buffer, and Derived tries to set a pointer to it.
>
> Is this a gnu g++ issue or is there some syntax I missing?


I think g++ is right.

>
> Rick
>
>
> template <typename CharT>
> class Base
> {
> public:
> CharT buffer_;
> };
>
>
> template <typename CharT>
> class Derived: public Base<CharT>
> {
> public:
> Derived( void)
> {


CharT * ptr = & this->buffer_;

> CharT * ptr = & buffer_; // error: `buffer_' was not
> declared in this scope
> CharT * ptr = & Base<CharT>::buffer_; // error: cannot
> convert `char Base<char>::*' to `char*' in initialization
> CharT * ptr = & (Base<CharT>::buffer_); // error: same as
> last one
>
> // This works but sure is ugly
> CharT & ref = Base<CharT>::buffer_;
> CharT * ptr = & ref;
> }
> };
>
>
> int main( void)
> {
> Derived<char> dah;
> }
>

 
Reply With Quote
 
Grizlyk
Guest
Posts: n/a
 
      02-10-2007

(E-Mail Removed) wrote:
>
> template <typename CharT>
> class Base
> {
> public:
> CharT buffer_;
> };
>
>
> template <typename CharT>
> class Derived: public Base<CharT>
> {
> public:

using Base<CharT>::buffer_;

http://www.parashift.com/c++-faq-lite/templates.html
[35.19] Why am I getting errors when my template-derived-class uses a member
it inherits from its template-base-class?

> Derived( void)
> {
> CharT * ptr = & buffer_;
> // error: `buffer_' was not declared in this scope


--
Maksim A. Polyanin

"In thi world of fairy tales rolls are liked olso"
/Gnume/


 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
templated function as parameter of another templated function Amadeus W. M. C++ 2 07-04-2006 09:59 PM
ASP.NET Templated User Controls - Limit child controls allowable within a templated control JohnyStyles@gmail.com ASP .Net 0 05-29-2006 06:00 PM
Subtypes of templated types (in templated functions) Marijn C++ 5 02-13-2004 09:50 AM
implementing a templated struct within a templated struct RA Scheltema C++ 3 01-06-2004 11:25 AM



Advertisments