473,503 Members | 8,131 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Accessing data members from templated copy constructor

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: kt***@free.fr
ICQ: 92964905
-------------------------------------------------------------------------
"Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn."
-------------------------------------------------------------------------

Jul 19 '05 #1
1 2007
"Alexandre Tolmos" <kt***@free.fr> 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
Jul 19 '05 #2

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

Similar topics

5
3632
by: Suzanne Vogel | last post by:
Hi, Given: I have a class with protected or private data members, some of them without accessor methods. It's someone else's class, so I can't change it. (eg, I can't add accessor methods to the...
2
1906
by: ferdinand.stefanus | last post by:
Hi, I have some questions regarding templated class constructor: #include <iostream> using namespace std; template<typename T> class Foo { public:
5
4219
by: kalita | last post by:
template<class T> class A { template<class Y> A(const A<Y> &) { // whatever } };
7
2445
by: Full Decent | last post by:
aclass A, B; Can A access B's privates all the time, or only in a copy constructor? -------and--------- This code: #include <iostream> class A
6
1511
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...
4
4973
by: marora | last post by:
I have created class definition which contains a charater pointer as one of it's data memeber. The objective is to read some data from a file, and assign it to a data member; Size of data is...
10
2065
by: asdf | last post by:
data members defined after a private label are not accessible to code that uses the class. So, can I define the private data members as const?
6
1547
by: Richard Thompson | last post by:
Hi - I have a program which was previously working (but wasn't well tested). I've added a new function call, and it now doesn't compile. The call requires a copy constructor, but the compiler...
7
1720
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,...
3
1981
by: mweltin | last post by:
I have been looking in the archives and as of yet have not found an answer to my problem. Class B has four members, and class A is derived from class B. Class A only adds one new member to class...
0
7207
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
7093
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
7291
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
7012
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
7468
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
4690
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
3171
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1522
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 ...
1
748
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.