473,786 Members | 2,445 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Preserve dynamic type information

Hello,

I'm trying to build a set of classes that act as the handlers for a
simple expression parser, and i'm wondering how to preserve the
dynamic type information from derived classes. An example will help
illustrate what I have in mind...

class Numeric
{
// contains discriminated union to hold data and identify data
type

virtual TypeDescritor Type() const;

virtual bool IsPromotableTo( const Numeric &numeric);

virtual Numeric Promote(const Numeric &rhs) const;

virtual Numeric Add(const Numeric &rhs) const;

friend Numeric operator+(const Numeric &lhs, const Numeric &rhs);
}

Numeric operator+(const Numeric &lhs, const Numeric &rhs)
{
if(lhs.IsPromot ableTo(rhs)){
return rhs.Add(lhs.Pro mote(rhs));
}
else if(rhs.IsPromot ableTo(lsh)){
return lhs.Add(rhs.Pro mote(rhs));
}

// handle stuff that can't be added together
}

class Rational : public Numeric
{
TypeDescritor Type() const;

bool IsPromotableTo( const Numeric &numeric);

virtual Numeric Promote(const Numeric &rhs) const;

virtual Numeric Add(const Numeric &rhs) const;
};

The functions in the derived class are returning an instance of that
derived class by value
so I don't get a memory leak or reference to a destructed object, but
the declared return
type is the base class. So when I return something like a Rational
through the base
type Numeric, I lose the dynamic 'Rationalness' and the returned value
is type as a Numeric.
So subsequent use of the returned value, even though the returned
value was originally
decalred as a Rational, will result in dispatching functions called on
the returned Rational
always getting serviced in the base Numeric.

I'm sure I'm doing something that's been done a million times before,
but I can't figure out a way to preserve the dynamic type information
for derived classes without returning values
by reference. However doing so can lead to memory leaks or dangling
references. Is there
a common pattern to handle this kind of thing?

Thanks,

Jul 13 '07 #1
2 1773
Hello,

On Jul 13, 11:38 pm, doomer <john.dum...@co mcast.netwrote:
Hello,

I'm trying to build a set of classes that act as the handlers for a
simple expression parser, and i'm wondering how to preserve the
dynamic type information from derived classes. An example will help
illustrate what I have in mind...

class Numeric
{
// contains discriminated union to hold data and identify data
type

virtual TypeDescritor Type() const;

virtual bool IsPromotableTo( const Numeric &numeric);

virtual Numeric Promote(const Numeric &rhs) const;

virtual Numeric Add(const Numeric &rhs) const;

friend Numeric operator+(const Numeric &lhs, const Numeric &rhs);

}

Numeric operator+(const Numeric &lhs, const Numeric &rhs)
{
if(lhs.IsPromot ableTo(rhs)){
return rhs.Add(lhs.Pro mote(rhs));
}
else if(rhs.IsPromot ableTo(lsh)){
return lhs.Add(rhs.Pro mote(rhs));
}

// handle stuff that can't be added together

}

class Rational : public Numeric
{
TypeDescritor Type() const;

bool IsPromotableTo( const Numeric &numeric);

virtual Numeric Promote(const Numeric &rhs) const;

virtual Numeric Add(const Numeric &rhs) const;

};

The functions in the derived class are returning an instance of that
derived class by value
so I don't get a memory leak or reference to a destructed object, but
the declared return
type is the base class. So when I return something like a Rational
through the base
type Numeric, I lose the dynamic 'Rationalness' and the returned value
is type as a Numeric.
So subsequent use of the returned value, even though the returned
value was originally
decalred as a Rational, will result in dispatching functions called on
the returned Rational
always getting serviced in the base Numeric.

I'm sure I'm doing something that's been done a million times before,
but I can't figure out a way to preserve the dynamic type information
for derived classes without returning values
by reference. However doing so can lead to memory leaks or dangling
references. Is there
a common pattern to handle this kind of thing?
If you want run-time polymorphism, you need return by reference or
pointer of some kind. This way, you're slicing an object, which is
what you don't want.

Or you can put everything in one omnipotent class, which is not really
an OO solution.

Regards
Jiri Palecek

Jul 13 '07 #2
doomer schrieb:
Hello,

I'm trying to build a set of classes that act as the handlers for a
simple expression parser, and i'm wondering how to preserve the
dynamic type information from derived classes. An example will help
illustrate what I have in mind...

class Numeric
{
[...]
virtual TypeDescritor Type() const;

virtual bool IsPromotableTo( const Numeric &numeric);

virtual Numeric Promote(const Numeric &rhs) const;

virtual Numeric Add(const Numeric &rhs) const;

friend Numeric operator+(const Numeric &lhs, const Numeric &rhs);
}
[...]
The functions in the derived class are returning an instance of that
derived class by value
so I don't get a memory leak or reference to a destructed object, but
the declared return
type is the base class.
If you return a derived type by value, the object will be copied into a new
object with base class type. You loose all additional information stored in
the derived class. This is called slicing.

If you want polymorphism, you have to use a pointer or reference.

For eleminating the memory problem, you could use a smart pointer like
TR1's shared_ptr (also found in Boost).

--
Thomas
http://www.netmeister.org/news/learn2quote.html
Jul 13 '07 #3

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

Similar topics

4
4431
by: Leslaw Bieniasz | last post by:
Cracow, 20.10.2004 Hello, As far as I understand, the generic programming basically consists in using templates for achieving a static polymorphism of the various code fragments, and their reuse for various template parameters. I wonder if there exist techniques for achieving a dynamic polymorphism using the generic programming. Is this possible? If yes, can anyone show me simple examples in C++
1
17673
by: Nathan Bloomfield | last post by:
Does anyone know if there is any documentation which relates to Access2k + ? or can anyone help adjust the code? I am having trouble converting the DAO references. TITLE :INF: How to Create a Dynamic Crosstab Report PRODUCT :Microsoft Access PROD/VER:1.00 1.10 OPER/SYS:WINDOWS
9
2634
by: Gibby Koldenhof | last post by:
Hiya, Terrible subject but I haven't got a better term at the moment. I've been building up my own library of functionality (all nice conforming ISO C) for over 6 years and decided to adopt a more OO approach to fit my needs. Altough I used an OO approach previously for some subparts of the library it became somewhat difficult to maintain all those parts since they really are the same thing coded for each part (code duplication). So...
8
10606
by: Tee | last post by:
Hi, How do we increase the size of array on runtime and preserve the previous data? I don't want to use ArrayList because the array could be a multi-dimension array. Thanks, Tee
3
3968
by: watcher | last post by:
when we redefine an array ,how can we preserve the old data? string a = new string; a = "old data"; when we redefine the array a as following: a = new string; the data of a was lost
6
33118
by: John Grandy | last post by:
Does C# have an equivalent for VB.NET's Redim Preserve ? ReDim Preserve increases the final dimension of any array while preserving the array's contents (however, the type of the array may not be changed).
11
3228
by: Brian W | last post by:
Yet another editor problem To reproduce do the following 1) Open a Webform and switch to HTML edit mode 2) Enter the Following (include spaces) This is some text before <asp:hyperlink id="hl1" runat="server " navigateurl="http://www.microsoft.com">This is my link</asp:hyperlink> And this is my text after the Hyperlink
2
7053
by: assgar | last post by:
Hi Developemnt on win2003 server. Final server will be linux Apache,Mysql and PHP is being used. I use 2 scripts(form and process). The form displays multiple dynamic rows with chechboxs, input box for units of service, description of the service and each row has its own dropdown list of unit fees that apply. Each dynamically created row will return 3 values fee1_choice, fee1_unit and fee1_money. Note The above informaton is...
0
3396
bmallett
by: bmallett | last post by:
First off, i would like to thank everyone for any and all help with this. That being said, I am having a problem retrieving/posting my dynamic form data. I have a form that has multiple options within options. I have everything being dynamically named from the previously dynamically named element. (I hope this makes sense.) I am not able to retrieve any of the dynamically created values. I can view them on the source page but can't pull them...
0
9647
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
10360
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...
1
10108
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9960
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
6744
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
5397
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...
0
5532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4064
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
3668
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.