accessing const array reference in inherited-templated class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • hweekuan@yahoo.com

    #1

    accessing const array reference in inherited-templated class

    Hi,

    I got an error with gnu C++ but not with intel C++ (icc). The distilled
    code is given below, a variable "T" is stored as a const T& in the base
    class and when the derived class try to access it, the compiler gives
    an error. Compile error with gnu C++ and version of C++ compiler is
    also given below. I don't know if its compiler bug.

    Any help and comments will be very much appreciated. Thank you.

    ----------------------------------------------------------------
    #include <iostream>
    #include <vector>

    template<class T> class baseClass {
    protected:
    baseClass(const T& r):p(r) { }
    const T& p;
    };

    template<class T> struct derivedClass : public baseClass<T> {

    derivedClass(co nst T& u) : baseClass<T>(u) { }

    void f() { std::cout<<base Class<T>::p[0]<<std::endl; } // error here
    };

    typedef std::vector<dou ble> V;

    int main() {

    V v(2,1);

    derivedClass<V> dc1(v);

    dc1.f();
    }
    ------------------------------------------
    // compile error
    inheritance.cc: In member function `void derivedClass<T> ::f() [with T =
    V]':
    inheritance.cc: 27: instantiated from here
    inheritance.cc: 16: error: invalid types `const V&[int]' for array
    subscript

    // compiler specs
    Reading specs from /usr/libexec/gcc/darwin/ppc/3.3/specs
    Thread model: posix
    gcc version 3.3 20030304 (Apple Computer, Inc. build 1493)

  • Alf P. Steinbach

    #2
    Re: accessing const array reference in inherited-templated class

    * hweekuan@yahoo. com:[color=blue]
    >
    > I got an error with gnu C++ but not with intel C++ (icc). The distilled
    > code is given below, a variable "T" is stored as a const T& in the base
    > class and when the derived class try to access it, the compiler gives
    > an error. Compile error with gnu C++ and version of C++ compiler is
    > also given below. I don't know if its compiler bug.
    >
    > Any help and comments will be very much appreciated. Thank you.[/color]

    I can't see anything technically wrong, and it compiles fine with MSVC 7.1,
    MinGW g++ 3.4.4, and especially, Comeau Online 4.3.3.

    Perhaps just update the compiler.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?

    Comment

    • hweekuan@yahoo.com

      #3
      Re: accessing const array reference in inherited-templated class

      thanks for the reply.
      i have not visited comp.lang.c++ for sometime, what is "Comeau Online
      4.3.3" ?

      Comment

      • Alf P. Steinbach

        #4
        Re: accessing const array reference in inherited-templated class

        * hweekuan@yahoo. com:[color=blue]
        > thanks for the reply.
        > i have not visited comp.lang.c++ for sometime, what is "Comeau Online
        > 4.3.3" ?[/color]

        Comeau is a compiler front-end that transforms your compiler into a
        standard-conforming one.

        Comeau Online is the online version, available on the web (just google),
        and is the nearest thing to an official "is it standard code" test.

        I guess Greg Comeau will correct me if this is not a 100% explanation...


        Cheers,

        - Alf

        --
        A: Because it messes up the order in which people normally read text.
        Q: Why is it such a bad thing?
        A: Top-posting.
        Q: What is the most annoying thing on usenet and in e-mail?

        Comment

        • Bob Hairgrove

          #5
          Re: accessing const array reference in inherited-templated class

          On 8 Dec 2005 18:44:34 -0800, hweekuan@yahoo. com wrote:
          [color=blue]
          > void f() { std::cout<<base Class<T>::p[0]<<std::endl; } // error here[/color]

          Like the others said, it should work. Perhaps you could try
          parentheses as a workaround (don't know if it helps, though):

          void f() {
          std::cout << (baseClass<T>:: p)[0] << std::endl;
          }

          You should include <ostream> as well as <iostream> if you are using
          std::endl ... most of the time, it seems to work without, but
          according to the standard it should be included as well.

          --
          Bob Hairgrove
          NoSpamPlease@Ho me.com

          Comment

          Working...