473,474 Members | 1,436 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to get address of templated object?

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

Feb 10 '07 #1
3 1548
Ri********@comcast.net 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<chardah;
}

Best

Kai-Uwe Bux
Feb 10 '07 #2
Ri********@comcast.net 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<chardah;
}
Feb 10 '07 #3

Ri********@comcast.net 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/
Feb 11 '07 #4

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

Similar topics

3
by: tirath | last post by:
Hi all, I have a templated class that derives from a non-templated abstract class. How do I then cast a base class pointer to a <templated> derived class pointer in a generalised fashion? ...
6
by: Dan Huantes | last post by:
I was presented a problem today where a class had member variable that was an object of a templated class. The class wanted to instantiate the object as a private member variable and call a...
2
by: | last post by:
Hello All, I am having a lot of difficulty trying to bind a templated column, that is programmatically created for a datagrid, to a datasource column. I have a datasource containing 2 columns,...
6
by: Alex | last post by:
I have been loving the templated datacolumns of the datagrid. I have stumbled onto a problem though that is beyond by knowledge and I was hoping someone here could jumpstart me. My templated...
2
by: Amadeus W. M. | last post by:
I have a bunch of templated functions: template <class Type_t> double f2(Type_t x) { return 2*x; } template <class Type_t> double f3(Type_t x) { return 3*x; }
0
by: Mike | last post by:
Hi. I can't figure out why a button's click event is not firing in a templated control I've created (first time I've tried creating one). Please can someone help? On a point of lesser importance,...
5
RRick
by: RRick | last post by:
This is a problem that arose while using GNU G++3.4.5. The problem is simple: How to get a pointer value from a templated object inside a class? Normally, I would add an '&' can carry on. This...
7
by: Claudius | last post by:
Hello, in my class TopTen I need to define three constructors while only the last one, the most general in terms of templates, should be sufficient in my opinion: template <typename Tnum,...
2
by: domehead100 | last post by:
I have a templated class, CDerived: template <typename TValue, typename TDraw, typename TEdit ...> class CDerived : public CBase { TValue m_Value public: TValue& GetValue() const {
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.