473,699 Members | 2,790 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=(Point er<Q> &p){return
*this;}
Pointer<T>& operator=(Point er<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>::Po inter (Pointer<A>)'
Pointer<A>::Poi nter()
Pointer<A>::Poi nter(A *)
Pointer<A>::Poi nter(Base *)
Pointer<A>::Poi nter(Pointer<A> &) <near match>
Pointer<A>::Poi nter(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 2138

"Carsten Spieß" <me@privacy.net > wrote in message
news:ip******** *************** *********@4ax.c om...
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
2022
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 looks like this: class base_t { int _ref_count; public: base_t(void): _ref_count(0) {};
1
2526
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()': /home/tobias/project/Heart/heartofoak/test.cpp:23: undefined reference to `SmartPtr<LongWrapper, RefCount>::SmartPtr(LongWrapper*)' collect2: ld returned 1 exit status I think the linker does not find the Constructor of the...
1
1873
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>". there is a problem now, that destructor definition doesn't go through compilation. below is the code and comment from the compiler. the problem seems to be that in destructor pointers next_ and object_ are not members of counted_ptr. what confuses...
5
1475
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
1104
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 let's try. The class TSample has a pointer (my smart pointer implementation) to a IList (an indirect list based on the same smart pointers) to the type TFontCache.
19
2514
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 classes that are graphical contain a sprite object.
1
1600
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 pointer.
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
2907
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
8704
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8623
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8894
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7776
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5879
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4390
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2360
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2015
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.