Accessing data members from templated copy constructor

Discussion in 'C++' started by Alexandre Tolmos, Aug 7, 2003.

  1. Hi all,

    I can't compile the following code with Gcc 3.3 (compiles with
    CodeWarrior 8):

    template <typename T = int>
    class Boule
    {
    friend class Boule<int>;
    friend class Boule<float>;
    protected:
    T _data; // Line #7
    inline Boule() {}
    template <typename U>
    inline Boule(const Boule<U>& b) : _data(b._data) {} // Line # 10
    };

    template <typename T = int>
    class Rouston : protected Boule<T>
    {
    public:
    inline Rouston() {}
    template <typename U>
    inline Rouston(const Rouston<U>& r) : Boule<T>(static_cast<const
    Boule<U>&>(r)) {}
    };

    The compiler reports the following errors:

    Test-2.cpp:7: error: `int Boule<int>::_data' is protected
    Test-2.cpp:10: error: within this context

    I suspect a bug in the compiler... Is there any workaround ?

    Any help welcome !

    -------------------------------------------------------------------------
    Alexandre Tolmos
    E-mail:
    ICQ: 92964905
    -------------------------------------------------------------------------
    "Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn."
    -------------------------------------------------------------------------
     
    Alexandre Tolmos, Aug 7, 2003
    #1
    1. Advertising

  2. "Alexandre Tolmos" <> wrote...
    > Sorry, I forgot to include the main function:
    > [...]


    If you compile your program:
    ----------------------------------------
    template <typename T = int>
    class Boule
    {
    friend class Boule<int>;
    friend class Boule<float>;
    protected:
    T _data; // Line #7
    inline Boule() {}
    template <typename U>
    inline Boule(const Boule<U>& b) : _data(b._data) {}
    };

    template <typename T = int>
    class Rouston : protected Boule<T>
    {
    public:
    Rouston() {}
    template <typename U>
    Rouston(const Rouston<U>& r)
    : Boule<T>(static_cast<const Boule<U>&>(r)) {} // 27
    };

    int main(int, char*[])
    {
    Rouston<> r1;
    Rouston<float> r2(r1); // 33
    return 0;
    }
    -----------------------------------------------
    using Comeau C++, you get more meaningful diagnostic:

    "ComeauTest.c", line 27: error: conversion to inaccessible base class
    "Boule<int>"
    is not allowed
    : Boule<T>(static_cast<const Boule<U>&>(r)) {}
    ^
    detected during instantiation of "Rouston<T>::Rouston(const
    Rouston<U> &) [with T=float, U=int]" at line 33
    ------------------------------------------------

    As you can see, you're trying to convert Rouston<int> to Boule<int>.
    That conversion is not accessible to Rouston<float>.

    Victor
     
    Victor Bazarov, Aug 8, 2003
    #2
    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. John Harrison
    Replies:
    18
    Views:
    559
    Pete Becker
    Aug 11, 2003
  2. Pete
    Replies:
    4
    Views:
    404
  3. Replies:
    5
    Views:
    452
    Chris Theis
    Mar 15, 2005
  4. cinsk
    Replies:
    35
    Views:
    2,656
    James Kanze
    Oct 11, 2010
  5. Andy
    Replies:
    11
    Views:
    620
    Alf P. Steinbach /Usenet
    Oct 5, 2010
Loading...

Share This Page