How to get address of templated object?

Discussion in 'C++' started by Rickarazzi@comcast.net, Feb 10, 2007.

  1. Guest

    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;
    }
    , Feb 10, 2007
    #1
    1. Advertising

  2. Kai-Uwe Bux Guest

    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
    Kai-Uwe Bux, Feb 10, 2007
    #2
    1. Advertising

  3. 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;
    > }
    >
    Gianni Mariani, Feb 10, 2007
    #3
  4. Grizlyk Guest

    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/
    Grizlyk, Feb 10, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. RA Scheltema
    Replies:
    3
    Views:
    380
    RA Scheltema
    Jan 6, 2004
  2. Marijn
    Replies:
    5
    Views:
    440
    Marijn
    Feb 13, 2004
  3. Replies:
    0
    Views:
    2,212
  4. Amadeus W. M.
    Replies:
    2
    Views:
    383
    Amadeus W. M.
    Jul 4, 2006
  5. Replies:
    1
    Views:
    293
Loading...

Share This Page