473,568 Members | 2,795 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<Bitm ap!=
CountedPtr<Reso urcealthough 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 3770

msc...@gmail.co m 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<Bitm ap!=
CountedPtr<Reso urcealthough 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<Base Type>. I suppose you have
to provide raw pointer to BaseTypeto constructor of
CountedPtr<Base Type>. So it is simple, just use pointer to derived
type:

class BaseType
{
// Some declarations
};

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

CountedPtr<Base Typep = new DerivedType;

And you have polymorphis.

Feb 19 '07 #2
On Feb 19, 1:54 pm, msc...@gmail.co m 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<Bitm ap!=
CountedPtr<Reso urcealthough 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<BitM ap<= CountedPtr<Reso urce>, 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<BitM ap{...
(I assume that in other uses of CountedPtr<you meant e.g.
CountedPtr<BitM ap>::value)

/Peter

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

class BaseType
{
// Some declarations

};

class DerivedType: public BaseType
{
// Some declarations

};

CountedPtr<Base Typep = 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<Bitm ap= CountedPtr<Reso urce>. 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<BitM ap<= CountedPtr<Reso urce>, 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<BitM ap{...
(I assume that in other uses of CountedPtr<you meant e.g.
CountedPtr<BitM ap>::value)

/Peter
Well here is more exact code:

vector< CountedPtr<Bitm ap vec;
// Function in my ResourceManager
CounterPtr<Reso urceGet( 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<Bi tmapr;

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

CountedPtr<Bitm ap= CountedPtr<Reso urce>; is not valid...

Is there a way go around this?

Feb 19 '07 #6
On Feb 19, 6:54 am, msc...@gmail.co m 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<Bitm ap!=
CountedPtr<Reso urcealthough 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::ope rator=( 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<Bi tmapr;

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

CountedPtr<Bitm ap= CountedPtr<Reso urce>; 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<Reso urce*= CountedPtr<Bitm ap*>
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(Coun tedPtr<T1&t1):t _(t1.t_) {} // copy constructor for
different template type. So CountedPtr<T1to CountedPtr<Twil l 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<Deri ved*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
4476
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 with code duplication? For example: let's say you've got two template specializations, which differ only slightly... So you put the common...
5
1411
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 death) Ray
2
2149
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 information (such as the owner of the "card") in each instance. struct Person; Person Bob; Person Joe;
1
1677
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 derivies class's that print those into console or into a file. template <class T, class S> class Base{ protected: T t;
10
9609
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 instead of inheritance, and other way around? thoughts, please.
4
1488
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 the messages are different, the way to handle the message is the same. Hence I'm thinking of using templates to reduce the number of lines in the...
2
735
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 function. What and how many updateable objects area actually created is determined at runtime.
2
3378
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 whether it should not be used in certain cases. Consider the case when all the params to a template function/class are similar. My questions is that...
5
2031
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 { // impl. };
3
3816
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 function overload and the virtual functionality. As I understand when you use templates you just increase the runtime performance since the code...
0
7693
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...
0
7916
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7962
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...
1
5498
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5217
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...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2101
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 we have to send another system
1
1207
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
932
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...

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.