How to get address of templated object?

R

Rickarazzi

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;
}
 
K

Kai-Uwe Bux

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
 
G

Gianni Mariani

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_;
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top