C
Carsten Spieß
Hello all,
i have a problem with a template constructor
I reduced my code to the following (compiled with gcc 2.7.2) to show
my problem:
// a base class
class Base{};
// two derived classes
class A : public Base{};
class B : public Base{};
// template smart pointer like (for Base derived classes)
template<class T> class Pointer
{
public:
Pointer(){} // ctor 0
Pointer(T* p){} // ctor 1
Pointer(Base* p){} // ctor 2
template <class Q> Pointer(Pointer<Q>& p){} // ctor 3
Pointer(Pointer<T>& p){} // ctor 4
Pointer<T>& operator=(T* p){return *this;}
Pointer<T>& operator=(Base* p){return *this;}
template <class Q> Pointer<T>& operator=(Pointer<Q> &p){return
*this;}
Pointer<T>& operator=(Pointer<T> &p){return *this;}
protected:
T* m_p;
};
my test function which doesn't compile:
void x()
{
Pointer<A> p1;
Pointer<B> p2;
Pointer<A> p3 = p1; // no error
Pointer<A> p4 = p2; // error
Pointer<A> p5 (p2); // no error;
Pointer<A> p6;p6 = p2; // no error
}
I assumed that ctor 3 should be used. But the compiler says:
no matching function for call to `Pointer<A>:
ointer (Pointer<A>)'
Pointer<A>:
ointer()
Pointer<A>:
ointer(A *)
Pointer<A>:
ointer(Base *)
Pointer<A>:
ointer(Pointer<A> &) <near match>
Pointer<A>:
ointer(Pointer<A> &) <near match>
When i remove ctor 4 it compiles, but i need ctor 4.
Did i make an mistake in defining the template ctors 3 or 4?
Thanks for your help, regards
Carsten
i have a problem with a template constructor
I reduced my code to the following (compiled with gcc 2.7.2) to show
my problem:
// a base class
class Base{};
// two derived classes
class A : public Base{};
class B : public Base{};
// template smart pointer like (for Base derived classes)
template<class T> class Pointer
{
public:
Pointer(){} // ctor 0
Pointer(T* p){} // ctor 1
Pointer(Base* p){} // ctor 2
template <class Q> Pointer(Pointer<Q>& p){} // ctor 3
Pointer(Pointer<T>& p){} // ctor 4
Pointer<T>& operator=(T* p){return *this;}
Pointer<T>& operator=(Base* p){return *this;}
template <class Q> Pointer<T>& operator=(Pointer<Q> &p){return
*this;}
Pointer<T>& operator=(Pointer<T> &p){return *this;}
protected:
T* m_p;
};
my test function which doesn't compile:
void x()
{
Pointer<A> p1;
Pointer<B> p2;
Pointer<A> p3 = p1; // no error
Pointer<A> p4 = p2; // error
Pointer<A> p5 (p2); // no error;
Pointer<A> p6;p6 = p2; // no error
}
I assumed that ctor 3 should be used. But the compiler says:
no matching function for call to `Pointer<A>:
Pointer<A>:
Pointer<A>:
Pointer<A>:
Pointer<A>:
Pointer<A>:
When i remove ctor 4 it compiles, but i need ctor 4.
Did i make an mistake in defining the template ctors 3 or 4?
Thanks for your help, regards
Carsten