473,499 Members | 1,483 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with template ctors (smart pointer)

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>::Pointer (Pointer<A>)'
Pointer<A>::Pointer()
Pointer<A>::Pointer(A *)
Pointer<A>::Pointer(Base *)
Pointer<A>::Pointer(Pointer<A> &) <near match>
Pointer<A>::Pointer(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
--
mail<AT>carsten-spiess.de
Jul 22 '05 #1
4 2114

"Carsten Spieß" <me@privacy.net> wrote in message
news:ip********************************@4ax.com...
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


Change to - template <class Q> Pointer(Pointer<Q> const& p){}

^^^^
Now compiles on Comeau online and g++ 3.3.1

-Sharad
Jul 22 '05 #2
Carsten Spieß wrote:
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
gcc 2.72 ?! You really have to upgrade. Get at least 2.95, or better 3.4, or
you'll have a number of problems with standard compilance.
template <class Q> Pointer(Pointer<Q>& p){} // ctor 3
Pointer(Pointer<T>& p){} // ctor 4
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:


gcc 3.3 has no problem with the above, except that ctor 4 should take const
reference. The syntax used in

Pointer<A> p4 = p2; // error

is called copy-initialization. Since p2 is of type Pointer<B>, the compiler
creates temporary of type Pointer<A> (using ctor 3) and then tries to pass
that temporary to copy ctor (ctor 4). The value created by ctor 3 is
temporary, and can't be passed to ctor 4 unless it takes const reference.

And again, upgrade your compiler.

HTH,
Volodya

Jul 22 '05 #3
On Thu, 17 Jun 2004 14:48:42 +0530, Sharad Kala wrote:
template <class Q> Pointer(Pointer<Q>& p){} // ctor 3


Change to - template <class Q> Pointer(Pointer<Q> const& p){}

^^^^
Now compiles on Comeau online and g++ 3.3.1


The sample now compiles too,
but in my real implementation i now get other errors.
I Have to look which are more serious.
Thanks for your help, regards

Carsten
--
mail<AT>carsten-spiess.de
Jul 22 '05 #4
On Thu, 17 Jun 2004 13:42:12 +0400, Vladimir Prus wrote:
gcc 2.72 ?! You really have to upgrade. Get at least 2.95, or better 3.4, or
you'll have a number of problems with standard compilance. I would like to, but it's not possible because in my customer says
i MUST use this compiler.
gcc 3.3 has no problem with the above, except that ctor 4 should take const
reference. Same as Sharad suggested (for ctor 3).
With a few changes in my code it works now.
The syntax used in

Pointer<A> p4 = p2; // error

is called copy-initialization. Since p2 is of type Pointer<B>, the compiler
creates temporary of type Pointer<A> (using ctor 3) and then tries to pass
that temporary to copy ctor (ctor 4). The value created by ctor 3 is
temporary, and can't be passed to ctor 4 unless it takes const reference. Thanks for the explanation now i understand where the problem comes
from.
And again, upgrade your compiler. I can't.
HTH,

Thank again you helped a lot, regards

Carsten
--
mail<AT>carsten-spiess.de
Jul 22 '05 #5

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

Similar topics

5
2008
by: Ales DOLECEK | last post by:
Hello, I'm trying to created base class and template for "smart" pointer pointing to it. It should do simple reference counting and clean up when the reference count reaches zero. The header...
1
2519
by: Tobias Langner | last post by:
I try to program a smart-pointer using policies. I rely heavyly on templates during this. The program compiles fine - but I get a linker error: test.o(.text+0x3e): In function `testSmartPtr()':...
1
1863
by: Marko Pyhajarvi | last post by:
good morning, could anyone help me with this problem? i have made an abstract container from which i have inherited a linkedlist class. this linkedlist uses a smart pointer "counted_ptr<T>"....
5
1452
by: Gonçalo Rodrigues | last post by:
Hi all, (note: newbie alert) Suppose you have a hierarchy of objects that you deal with via smart pointers, e.g. something like: template<typename T> class Ref { private:
0
1087
by: Adriano Coser | last post by:
Hello. I'm trying to compile a DLL with Visual C++ .NET 2003 and I'm facing an error declaring template variables with prototypes. I don´t know if I'll be able to explain my scenario, but...
19
2497
by: Chocawok | last post by:
Some of the classes in my app are graphical. To encapsulate the graphical side of things I had created a class called "sprite" which holds a bit map and knows how to draw itself etc. The...
1
1584
by: Raf256 | last post by:
I have base template class A<B>, and son class B. Inside A<B> constrcutor, can I access a pointer to B, from "this"? like A<B>::A() : pointerToB(static_cast<B*>this) { } I will use the...
6
351
by: jiake2007 | last post by:
Hi all, I'm trying to write a template class that calls a function from one of its template parameters: template <class T> class Foo { public: Foo(T t): t(t) {}
5
2877
by: Jun | last post by:
Hello, I've code like : =========================================== class A{ public : // create print content friend std::ostream& operator<< (std::ostream& os, const A& a);
0
7128
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
7006
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
7169
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,...
0
7215
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6892
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
4597
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
1425
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
661
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
294
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.