473,805 Members | 2,026 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_INCLUD ED
#define DYNPTR_H_INCLUD ED

/*
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<In teger*>(value);
Integer *ci = dynamic_cast<co nst 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=(cons t DynPtr &q) { p = q.get(); return *this; }
DynPtr &operator=(T *p) { p = q; return *this; }

template<class Q> DynPtr<T> &operator=(cons t 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 1916

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=(cons t DynPtr &q) { p = q.get(); return *this; }
DynPtr &operator=(T *p) { p = q; return *this; }

template<class Q> DynPtr<T> &operator=(cons t 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
12208
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
3154
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 is the part more or less relevant to my problem. Version 1 is fine for casting to a derived class, but it must be called explicitly even though it is used to cast to a base class.
5
8734
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 use it with. Now, if that T happens to be some subclass of a known base class (object, in this case), I want to perform some extra stuff ... I've read Faq#35, and the most natural solution would have
89
6539
by: Sweety | last post by:
hi, Is main function address is 657. its show in all compiler. try it & say why? bye,
35
2907
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 */ memset(TextureImage,0,sizeof(void *)*1); /* Line 2*/ According to my knowledge in the first line
16
2317
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 subject pointer does not refer to an object suitably aligned in storage. It is guaranteed that a pointer to an object may be converted to a pointer to an object whose type requires less or equally strict storage alignment and back again without change;...
7
3818
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
2529
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 doing any explicit cast: #include <stdlib.h>
7
3953
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
9716
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9596
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10604
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10356
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10103
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6874
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4316
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
2
3839
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.