By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,837 Members | 1,648 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,837 IT Pros & Developers. It's quick & easy.

A problem with conversation in template

P: n/a
the code is as follows:

#include<iostream>
using namespace std;

template<class T>
class Myauto_ptr
{
public:
Myauto_ptr(T*ptr=0):_Myptr(ptr){}
~Myauto_ptr(){ delete _Myptr;}

template<class another_T>
operator Myauto_ptr<another_T>(){ return
Myauto_ptr<another_T>(_Myptr);}

T& operator*(){ return *_Myptr;}
T* operator->(){ return _Myptr;}
private:
T * _Myptr;
};
class base
{
public:
void print(){cout<<"I am base!"<<endl;}
};
class Tuple:private base{};
void Test( const Myauto_ptr<base&pmp, int num)
{
cout<<"in Test"<<endl;
}

int main()
{
Myauto_ptr<Tuplept1(new Tuple);
Test( pt1, 10); //Myauto_ptr<Tupleconverts to
Myauto_ptr<base,but Tuple private inherits from base ,why such
conversation is successful(in VC)

Tuple * ptrT = new Tuple;
base * ptrB = ptrT; //it's compiler error, but why "Test( pt1,
10)" is successful?
return 0;
}
I just compile the code in VC7&&VC8. well, in gcc, Test( pt1, 10)
results in a compile error. So, it's a VC's bug? Or ,the gcc is wrong?
Or both are OK. Is there someone can tell me why?

Dec 28 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
mi*********@gmail.com wrote:
....
I just compile the code in VC7&&VC8. well, in gcc, Test( pt1, 10)
results in a compile error. So, it's a VC's bug? Or ,the gcc is wrong?
Or both are OK. Is there someone can tell me why?
gcc 4.1.1 is correct.
VC8 is wrong.
comeau 4.3.8 is correct.

Basically, the conversion operator template "Myauto_ptr" is accessing
base via private inheritance which it is not allowed to do.

As an aside, you'll want to avoid the conversion operator for smart
pointers. They get in the way with ambiguous conversions, the best way
is to overload the constructor.
Dec 28 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.