473,326 Members | 2,815 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,326 software developers and data experts.

templates and polymorphism

I've created CoutedPtr<Ttype to be able o track how many times
pointer is being reference( for garbage collecting ). The problem is
that the polymorphism doesn't work. e.g. CountedPtr<Bitmap!=
CountedPtr<Resourcealthough Bitmap IS Resource( I mean class
Bitmap : public Resource ). How can I go around this obstacle? If you
need more code just tell me...

Feb 19 '07 #1
8 3747

msc...@gmail.com napsal:
I've created CoutedPtr<Ttype to be able o track how many times
pointer is being reference( for garbage collecting ). The problem is
that the polymorphism doesn't work. e.g. CountedPtr<Bitmap!=
CountedPtr<Resourcealthough Bitmap IS Resource( I mean class
Bitmap : public Resource ). How can I go around this obstacle? If you
need more code just tell me...
Typical aproach is to create CountedPtr<BaseType>. I suppose you have
to provide raw pointer to BaseTypeto constructor of
CountedPtr<BaseType>. So it is simple, just use pointer to derived
type:

class BaseType
{
// Some declarations
};

class DerivedType: public BaseType
{
// Some declarations
};

CountedPtr<BaseTypep = new DerivedType;

And you have polymorphis.

Feb 19 '07 #2
On Feb 19, 1:54 pm, msc...@gmail.com wrote:
I've created CoutedPtr<Ttype to be able o track how many times
pointer is being reference( for garbage collecting ). The problem is
that the polymorphism doesn't work. e.g. CountedPtr<Bitmap!=
CountedPtr<Resourcealthough Bitmap IS Resource( I mean class
Bitmap : public Resource ). How can I go around this obstacle? If you
need more code just tell me...
If every class gets counted properly, and Bitmap is a Resource, then
CountedPtr<BitMap<= CountedPtr<Resource>, where the comparison will
be < unless Bitmap is the only Resource instantiated.
If that is not the case, I assume there is some problem with your
code. If you are using the curiously recurring template pattern your
"example" above misses something. It should be something like:
Bitmap : public Resource, public CountedPtr<BitMap{...
(I assume that in other uses of CountedPtr<you meant e.g.
CountedPtr<BitMap>::value)

/Peter

Feb 19 '07 #3
Typical aproach is to create CountedPtr<BaseType>. I suppose you have
to provide raw pointer to BaseTypeto constructor of
CountedPtr<BaseType>. So it is simple, just use pointer to derived
type:

class BaseType
{
// Some declarations

};

class DerivedType: public BaseType
{
// Some declarations

};

CountedPtr<BaseTypep = new DerivedType;

And you have polymorphis.
Problem is that this way CountedPtr will loose it's meaning, when I
will be assigning raw pointer. The action I want to solve is
CountedPtr<Bitmap= CountedPtr<Resource>. And this makes me think of
implementing somehow dynamic_cast...
Feb 19 '07 #4
If every class gets counted properly, and Bitmap is a Resource, then
CountedPtr<BitMap<= CountedPtr<Resource>, where the comparison will
be < unless Bitmap is the only Resource instantiated.
If that is not the case, I assume there is some problem with your
code. If you are using the curiously recurring template pattern your
"example" above misses something. It should be something like:
Bitmap : public Resource, public CountedPtr<BitMap{...
(I assume that in other uses of CountedPtr<you meant e.g.
CountedPtr<BitMap>::value)

/Peter
Well here is more exact code:

vector< CountedPtr<Bitmap vec;
// Function in my ResourceManager
CounterPtr<ResourceGet( string& key )
// This is the line where compiler says it want to get Bitmap passed,
not Resource
vec.push_back( RCM::Get( myKey ) );


Feb 19 '07 #5
I feel like I should formulate it otherwise. Can I cast templates?

With normal pointers I would do:

Bitmap* b;
Resource* r = new Resource;
b = dynamic_cast<Bitmapr;

But what if I want to do it with CountedPtr<T>?

CountedPtr<Bitmap= CountedPtr<Resource>; is not valid...

Is there a way go around this?

Feb 19 '07 #6
On Feb 19, 6:54 am, msc...@gmail.com wrote:
I've created CoutedPtr<Ttype to be able o track how many times
pointer is being reference( for garbage collecting ). The problem is
that the polymorphism doesn't work. e.g. CountedPtr<Bitmap!=
CountedPtr<Resourcealthough Bitmap IS Resource( I mean class
Bitmap : public Resource ). How can I go around this obstacle? If you
need more code just tell me...
Check out boost's shared_ptr
http://www.boost.org/boost/shared_ptr.hpp

Specifically boost::dynamic_pointer_cast

and

shared_ptr::operator=( const shared_ptr& rhs )

This is some pretty detail oriented stuff. I'd recommend using boost's
implementation of shared_ptr's to create garbage collection.

Feb 19 '07 #7
Ouw! Thanks Paul! Something I'm going to study. I'll try to implement
it by myself. Thanks again.
Feb 19 '07 #8
With normal pointers I would do:
>
Bitmap* b;
Resource* r = new Resource;
b = dynamic_cast<Bitmapr;

But what if I want to do it with CountedPtr<T>?

CountedPtr<Bitmap= CountedPtr<Resource>; is not valid...

Is there a way go around this?
Hi.

If Resource is a base class of the derived class Bitmap then the above
example should always fail. (As in a resource is not a bitmap. A
bitmap is a resource. Maybe you meant to write 'Resource* r = new
Bitmap' on the second line, then the example would make sense.)

However if you wanted to allow assignemnt the other way, from wrapper
of derived pointer to wrapper of base pointer.. as in
CountedPtr<Resource*= CountedPtr<Bitmap*>
just as there is an imlicit conversion from a naked derived pointer to
a base pointers. as in
Resource* = Bitmap*

Then you can overload the CountedPtr copy constructor to do just
that.

<CODE>

template <class T>
struct CountedPtr {

CountedPtr(T t):t_(t) {}

template <class T1>
CountedPtr(CountedPtr<T1&t1):t_(t1.t_) {} // copy constructor for
different template type. So CountedPtr<T1to CountedPtr<Twill work
if there is a conversion from T1 to T

T t_;
};
struct Base{};
struct Derived: public Base {};

inline void Test() {
CountedPtr<Base*cb(0);
CountedPtr<Derived*cd(0);
cb = cd; // works
}

</CODE>

Regards Patrik


Feb 20 '07 #9

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

Similar topics

5
by: SainTiss | last post by:
Hi, Is there an extra overhead in using virtual functions and templates? Or is is just the same overhead as with regular classes? Either way, if you'd want to avoid it, will you always end up...
5
by: Ray Gardener | last post by:
Would templates be necessary if C++ had all numeric types inherit from a base "number" class and had all types inherit from a base "object" class? (Sorry if this has already been discussed to...
2
by: kelvSYC | last post by:
I'm trying to program something along the lines of a "trading card" idea: I have a single copy of the "card" in the program, yet there may be multiple "instances" of the "card", with differing...
1
by: sapropel | last post by:
ive been trying to understand Inheritance, Polymorphism and templates, and so ive tried to make a test using all of those. it is about a base class taht receives two params on the template and two...
10
by: makc.the.great | last post by:
now that I am looking at templates, there's the question. why same effect couldn't/shouldn't be achieved with inheritance? provided the difference of the two, when and where do I use templates...
4
by: qning88 | last post by:
I would like to find out how I can effectively make use of templates in theexample below. In Class A, I have 3 overloaded member functions "send" for handling the different messages. Although...
2
by: glen stark | last post by:
I have a templated function template<class T> void update_E(T& updateable) { .... } What I would like to do is store a vector of updateable objects, and for each of them call the update...
2
by: sarathy | last post by:
Hi all, I need a small clarification reg. Templates and Polymorphism. I believe templates is really a good feature, which can be used to implement generic functions and classes. But i doubt...
5
by: Zeppe | last post by:
Hi all! my problem is this one, I think that it could be a common one, maybe a pattern, so if you can help me somehow it would be great. Let's suppose I have a class Base class Base { //...
3
by: Johs | last post by:
I have read that when you are using templates you are making generic programs. But I don't see whats so generic about templates. You can make generic programs without templates through the use of...
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: 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...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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)...
1
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.