473,320 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Smart pointers and inheritance

Hi everybody:

I have implemented a Ptr<Ttemplate class that I want to use in my
framework.

Let's say I have:

template <class T>
class Ptr<T>;

class A;
class B : public A;

class C
{
public:
...
int GetValue(Ptr<Aval);
};
I want to instance my objects using my smart pointer:

Ptr<Bb(new B());

If I would use a standard pointer, the calling to int GetValue(A* val);
would be:

c->GetValue(b); //valid using standard pointers

but it does not compile using my notation because there is no match
between

Ptr<Aand Ptr<B>.

How can I implement my smart pointer to be able to call

c->GetValue(b);

using my smart pointers?

Regards,
Ernesto

Dec 18 '06 #1
4 2174
Hi

Ernesto Bascón wrote:
Hi everybody:

I have implemented a Ptr<Ttemplate class that I want to use in my
framework.
[...]
Ptr<Bb(new B());

If I would use a standard pointer, the calling to int GetValue(A* val);
would be:

c->GetValue(b); //valid using standard pointers

but it does not compile using my notation because there is no match
between

Ptr<Aand Ptr<B>.
The usual way is to add a templated constructor that takes a smart-pointer
to any other type U and let the compiler find out if the conversion from U*
to T* is legal:

template<typeame UPtr<T>(Ptr<Uother)
: pointer(other.pointer), // this only compiles if there is a conversion
from U* to T*!
counter(other.counter) // assuming external reference count
{
inc(counter);
}

Markus

Dec 18 '06 #2
Ernesto Bascón napsal:
Hi everybody:

I have implemented a Ptr<Ttemplate class that I want to use in my
framework.

Let's say I have:

template <class T>
class Ptr<T>;

class A;
class B : public A;

class C
{
public:
...
int GetValue(Ptr<Aval);
};
I want to instance my objects using my smart pointer:

Ptr<Bb(new B());
You probably need Ptr<Ab(new B());
>
If I would use a standard pointer, the calling to int GetValue(A* val);
would be:

c->GetValue(b); //valid using standard pointers

but it does not compile using my notation because there is no match
between

Ptr<Aand Ptr<B>.

How can I implement my smart pointer to be able to call

c->GetValue(b);

using my smart pointers?

Regards,
Ernesto
Dec 18 '06 #3
Ernesto Bascón wrote:
Hi everybody:

I have implemented a Ptr<Ttemplate class that I want to use in my
framework.
....
>
How can I implement my smart pointer to be able to call

c->GetValue(b);

using my smart pointers?
This is usually implemented using a template copy constructor and
assignment operator of the smart pointer.

See Austria's smart pointer.

http://austria.sourceforge.net/dox/h...1_1Ptr.html#a3
Dec 18 '06 #4

Ernesto Bascón wrote:
Hi everybody:

I have implemented a Ptr<Ttemplate class that I want to use in my
framework.

Let's say I have:

template <class T>
class Ptr<T>;

class A;
class B : public A;

class C
{
public:
...
int GetValue(Ptr<Aval);
};
I want to instance my objects using my smart pointer:

Ptr<Bb(new B());
Ptr< A p_b( new B );
>
If I would use a standard pointer, the calling to int GetValue(A* val);
would be:

c->GetValue(b); //valid using standard pointers

but it does not compile using my notation because there is no match
between

Ptr<Aand Ptr<B>.

How can I implement my smart pointer to be able to call

c->GetValue(b);

using my smart pointers?

Regards,
Ernesto
Dec 18 '06 #5

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

Similar topics

9
by: christopher diggins | last post by:
I would like to survey how widespread the usage of smart pointers in C++ code is today. Any anecdotal experience about the frequency of usage of smart pointer for dynamic allocation in your own...
27
by: Susan Baker | last post by:
Hi, I'm just reading about smart pointers.. I have some existing C code that I would like to provide wrapper classes for. Specifically, I would like to provide wrappers for two stucts defined...
4
by: A_StClaire_ | last post by:
I read a section of my text on smart "counting" pointers and found it confusing so I decided to get hands-on. however I'm getting "Debug Assertion Failed... Expression:...
8
by: Axter | last post by:
I normally use a program call Doxygen to document my source code.(http://www.stack.nl/~dimitri/doxygen) This method works great for small and medium size projects, and you can get good...
92
by: Jim Langston | last post by:
Someone made the statement in a newsgroup that most C++ programmers use smart pointers. His actual phrase was "most of us" but I really don't think that most C++ programmers use smart pointers,...
33
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the...
54
by: Boris | last post by:
I had a 3 hours meeting today with some fellow programmers that are partly not convinced about using smart pointers in C++. Their main concern is a possible performance impact. I've been explaining...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.