473,320 Members | 2,110 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.

DynPtr: A pointer with implicit dynamic cast

Here's a little utility I thought I'd share.

(For those familiar with ATL::CComQIPtr, the concept is similar).
====

#ifndef DYNPTR_H_INCLUDED
#define DYNPTR_H_INCLUDED

/*
DynPtr - A pointer with an implicit dynamic-cast

Purpose: When code requires a lot of dynamic_casting,
it can be difficult to read, so DynPtr is a pointer
that implicitly
dynamic_casts, to save the bother of writing dynamic_cast all
the time.

e.g.

Integer *i = dynamic_cast<Integer*>(value);
Integer *ci = dynamic_cast<const Integer*>(value);

vs

DynPtr<Integer> i(value);
DynPtr<const Integer> ci(value);

Written by Calum Grant. http://visula.org/calum
Public domain - free of copyright.
*/

template<class T>
class DynPtr
{
T *p;
public:
DynPtr() : p(0) { }

DynPtr(const DynPtr &q) : p(q.get()) { }
explicit DynPtr(T *q) : p(q) { }

template<class Q>
explicit DynPtr(const DynPtr<Q> &q) : p(dynamic_cast<T*>(q.get())) { }

template<class Q>
explicit DynPtr(Q *q) : p(dynamic_cast<T*>(q)) { }

DynPtr &operator=(const DynPtr &q) { p = q.get(); return *this; }
DynPtr &operator=(T *p) { p = q; return *this; }

template<class Q> DynPtr<T> &operator=(const DynPtr<Q> &q)
{
p = dynamic_cast<T*>(q.get());
return *this;
}

template<class Q> DynPtr<T> &operator=(Q *q)
{
p = dynamic_cast<T*>(q);
return *this;
}

operator bool() const { return p!=0; }
T *operator->() const { return p; }
T &operator*() const { return p; }
T *get() const { return p; }
};

#endif
Dec 6 '05 #1
2 1889

Calum Grant wrote:
template<class T>
class DynPtr
{
T *p;
public:
DynPtr() : p(0) { }

DynPtr(const DynPtr &q) : p(q.get()) { }
explicit DynPtr(T *q) : p(q) { }

template<class Q>
explicit DynPtr(const DynPtr<Q> &q) : p(dynamic_cast<T*>(q.get())) { }

template<class Q>
explicit DynPtr(Q *q) : p(dynamic_cast<T*>(q)) { }

DynPtr &operator=(const DynPtr &q) { p = q.get(); return *this; }
DynPtr &operator=(T *p) { p = q; return *this; }

template<class Q> DynPtr<T> &operator=(const DynPtr<Q> &q)
{
p = dynamic_cast<T*>(q.get());
return *this;
}

template<class Q> DynPtr<T> &operator=(Q *q)
{
p = dynamic_cast<T*>(q);
return *this;
}

operator bool() const { return p!=0; }
T *operator->() const { return p; }
T &operator*() const { return p; }
This doesn't look like it should compile.
Did you mean 'return *p;' maybe?
T *get() const { return p; }
};

#endif


Cheers,
Andre

Dec 6 '05 #2

Calum Grant wrote:

/*
DynPtr - A pointer with an implicit dynamic-cast

Purpose: When code requires a lot of dynamic_casting,


IMHO, one should reconsider the design.

Dec 7 '05 #3

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

Similar topics

4
by: Bren | last post by:
Hi all, Given this situation: class Base { typedef void (Base::*FN_FOO)(); virtual void Foo() = 0; // pure virtual void GetFoo(FN_FOO pfnFoo) = 0; // pure virtual
5
by: Ingo Nolden | last post by:
Hi there, I am writing a smart pointer that is similar to the boost intrusive ptr. I am trying to make it behave like a c++ pointer including the implicit and explicit casting behaviour. Below...
5
by: verec | last post by:
I just do not understand this error. Am I misusing dynamic_cast ? What I want to do is to have a single template construct (with no optional argument) so that it works for whatever T I want to...
89
by: Sweety | last post by:
hi, Is main function address is 657. its show in all compiler. try it & say why? bye,
35
by: tuko | last post by:
Hello kind people. Can someone explain please the following code? /* Create Storage Space For The Texture */ AUX_RGBImageRec *TextureImage; /* Line 1*/ /* Set The Pointer To NULL...
16
by: junky_fellow | last post by:
According to Section A6.6 Pointers and Integers (k & R) " A pointer to one type may be converted to a pointer to another type. The resulting pointer may cause addressing exceptions if the...
7
by: WaterWalk | last post by:
Hello. I thought I understood member function pointers, but in fact I don't. Consider the following example: class Base { public: virtual ~Base() {} }; class Derived : public Base {
17
by: arnuld | last post by:
I have checked the FAQ: http://c-faq.com/malloc/mallocnocast.html FAQ discusses a special case when programmer has forgotten to do #include <stdlib.h>. I am including this header and I am not...
7
by: * Tong * | last post by:
Hi, I couldn't figure out how to properly type cast in this case: $ cat -n type_cast.c 1 #include <stdio.h> 2 3 typedef unsigned char Byte; 4 typedef signed char Small_Int; 5
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
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
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: 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...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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: 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...

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.