473,399 Members | 4,254 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,399 software developers and data experts.

How to get address of templated object?

RRick
463 Expert 256MB
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 does not seem to work.

Take a look at the example and some of the things I tried. There are only two classes, Base and Derived, where Base defines the buffer, and Derived tries to set a pointer to it.

If you take away the template specification, everything works like it should.

Any ideas?

Rick

Expand|Select|Wrap|Line Numbers
  1. template <typename CharT>
  2. class  Base
  3. {
  4. public:
  5.     CharT  buffer_;
  6. };
  7.  
  8.  
  9. template <typename CharT>
  10. class  Derived:   public Base<CharT>
  11. {
  12. public:
  13.     Derived( void)
  14.     {   
  15.         CharT * ptr =  & buffer_;   // error: `buffer_' was not declared in this scope
  16.         CharT * ptr =  & Base<CharT>::buffer_;  // error: cannot convert `char Base<char>::*' to `char*' in initialization 
  17.         CharT * ptr =  & (Base<CharT>::buffer_); // error:  same as last one
  18.  
  19.         // This works but sure is ugly
  20.         CharT & ref =  Base<CharT>::buffer_;
  21.         CharT * ptr = & ref;
  22.     }
  23. };
  24.  
  25.  
  26. int main( void)
  27. {
  28.     Derived<char> dah;
  29.  
Feb 10 '07 #1
5 1202
AdrianH
1,251 Expert 1GB
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 does not seem to work.

Take a look at the example and some of the things I tried. There are only two classes, Base and Derived, where Base defines the buffer, and Derived tries to set a pointer to it.

If you take away the template specification, everything works like it should.

Any ideas?

Rick

Expand|Select|Wrap|Line Numbers
  1. template <typename CharT>
  2. class  Base
  3. {
  4. public:
  5.     CharT  buffer_;
  6. };
  7.  
  8.  
  9. template <typename CharT>
  10. class  Derived:   public Base<CharT>
  11. {
  12. public:
  13.     Derived( void)
  14.     {   
  15.         CharT * ptr =  & buffer_;   // error: `buffer_' was not declared in this scope
  16.         CharT * ptr =  & Base<CharT>::buffer_;  // error: cannot convert `char Base<char>::*' to `char*' in initialization 
  17.         CharT * ptr =  & (Base<CharT>::buffer_); // error:  same as last one
  18.  
  19.         // This works but sure is ugly
  20.         CharT & ref =  Base<CharT>::buffer_;
  21.         CharT * ptr = & ref;
  22.     }
  23. };
  24.  
  25.  
  26. int main( void)
  27. {
  28.     Derived<char> dah;
  29.  
Yeah, I came across this reciently. A lazy way would be to use 'this->buffer_' to access derrived member. 'corse it won't work if you have a buffer_ declared in derrived class.

Also, what is all the 'ptr's and 'ref's in your code for? That may have something to do with it.

Hope this helps.


Adrian
Feb 13 '07 #2
AdrianH
1,251 Expert 1GB
Yeah, I came across this reciently. A lazy way would be to use 'this->buffer_' to access derrived member. 'corse it won't work if you have a buffer_ declared in derrived class.

Also, what is all the 'ptr's and 'ref's in your code for? That may have something to do with it.

Hope this helps.


Adrian
Ignore my question on the ptr's and ref's. I wasn't reading right.

Anyway, yeah, use either this->buffer_ or get the reference to a var and get the address of that like you did. It is a little messy, but it doesn't add extra object (assembly) code then doing it directly.

Hope this helps.


Adrian
Feb 13 '07 #3
RRick
463 Expert 256MB
This problem arose from a template constructor where I had to pass the pointer of a templated object found in the base class to another templated object found in the derived object. The real problem was getting the compiler to accept the pointer referencing. What you saw posted was the stripped down information.

For some reason and in some cases, templates don't use the specialization information in derived classes when refering to some base class methods. As to the particular details of why this happens, I can't say. My eyes tend to glaze over in deep discussions about template instantiation and specialization.

I did find out that the compiler can get speciaization information from the this pointer. I will remember "this" trick in the future.
Feb 13 '07 #4
AdrianH
1,251 Expert 1GB
This problem arose from a template constructor where I had to pass the pointer of a templated object found in the base class to another templated object found in the derived object.
Man, that sounds really convoluted. :)

The real problem was getting the compiler to accept the pointer referencing. What you saw posted was the stripped down information.
Thank goodness. :D

For some reason and in some cases, templates don't use the specialization information in derived classes when refering to some base class methods. As to the particular details of why this happens, I can't say. My eyes tend to glaze over in deep discussions about template instantiation and specialization.
Yeah, I can't understand it either. Why you can do it with a reference but not a pointer is beyond me.

I did find out that the compiler can get speciaization information from the this pointer. I will remember "this" trick in the future.
Glad to have helped.

Oh and I found another way of doing it:
Expand|Select|Wrap|Line Numbers
  1. CharT* ptr = &static_cast<CharT&>(Base<CharT>::buffer_);
Messy eh? To get the pointer must be due to a parsing problem.


Adrian
Feb 13 '07 #5
RRick
463 Expert 256MB
This problem arose from trying to modify an ostream by supplying it with another streambuffer. Before we go too far, realize that a very nice book (C++ Standard Library by Josuttis) did all the hard work; I just copied his solution. Specifically, I needed a streambuffer that could handle unix file descriptors (i.e. for pipes, sockets, etc.), Gnu has some extensions for this, but I needed something for more than one compiler.

I've never looked at the c++ io objects very closely (mainly due to fear) but they have a nice design. They break IO into formatting and device control. The ios objects deal with formatting and the streambuffer objects deal with the device. If you look at ostream, it is simply a char instantiation of basic_ostream with a "pointer" to a basic_streambuff. That's where the trouble started. I had a class derived from basis_ostream, that needed to pass a pointer to a derived streambuffer to the base class. Its the crossing from derived to base class that caused the problem (I think).
Feb 14 '07 #6

Sign in to post your reply or Sign up for a free account.

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,...
0
by: John Crowley | last post by:
I keep running into this over and over again... I want a block server control that renders a header and footer, and child controls in between. But I don't want a templated control, for the...
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,...
3
by: Rickarazzi | last post by:
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. ...
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
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
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,...

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.