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

Smart pointers and inheritance

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

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 discussion thread is closed

Replies have been disabled for this discussion.