473,796 Members | 2,765 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

canonical up-cast idiom for smart pointers?

I'm wondering if there is a way to do this properly. I feel like I am
missing something.

I have two distinct classes. They don't have much at all in common.
I want a vector-like container that contains smart pointers
(boost::shared_ ptr or similar) to a set of these two classes and keeps
them sorted wrt each other.

For example if I insert ClassA, ClassA, ClassB, ClassA, ClassB in that
order, I want to re-visit them in that same order when I iterate
later.

My first thought was to make an artificial base class that has a
'type' argument indicating which "derived" type was stored. Then a
simple vector< shared_ptr<Base gets me my objects back. The
problem, of course, is that I get back pointers to the Base type.

Is there an idiomatic way to go from shared_ptr<Base to
shared_ptr<Deri ved? I note that the compiler happily goes the other
way automatically.

Or maybe there is another idiomatic way to store this data? I can
think of a half-dozen ways (seperate vectors with a third switcher
vector, etc), but they all seem dirty.

Is there a better way?

Tim

Mar 17 '07 #1
3 2015
On Mar 17, 11:19 am, "Tim H" <thoc...@gmail. comwrote:
I have two distinct classes. They don't have much at all
in common.
I want a vector-like container that contains smart
pointers (boost::shared_ ptr or similar) to a set of these
two classes and keeps them sorted wrt each other.
You're contradicting yourself. If those two classes don't
have much in common, why are you trying to store them in
the same vector? Presumably, because you want to iterate
over the vector, retrieve the objects and do-something with
them depending on what they are. The point is,
do-something-after-being-retrieved-from-a-vector is exactly
what they have in common then, even if the implementations
vastly differ between the classes.

And that's exactly the thing that belongs to an abstract
base class that both your classes should inherit from.
For example if I insert ClassA, ClassA, ClassB, ClassA,
ClassB in that order, I want to re-visit them in that
same order when I iterate later.
And do-something, right? So instead of (pseudocode):

if ( ( * base_object ) is-a ClassA object )
base_object -do_some_ClassA_ stuff ( ) ;
else
base_object -do_some_ClassB_ stuff ( ) ;

....make that:

baseObject -do_stuff ( ) ;

And let the objects themselves worry about what exactly
should be done.
My first thought was to make an artificial base class
that has a 'type' argument indicating which "derived"
type was stored.
And why would you want to know? Let the objects handle the
differences. Why implement the polymorphism by hand if you
can have the compiler handle the grisly details for you?
Is there an idiomatic way to go from shared_ptr<Base to
shared_ptr<Deri ved?
Why would you want to? If the objects are so vastly
different you can't do with a pointer to a base class, they
shouldn't be stored together. If they have something in
common that allows them to be processed together, that
something-in-common belongs to the base class, in which
case the base class pointer should suffice (and
implementations will handle the differences).

All in all, it looks like more of a design problem than a
C++ problem to me. Perhaps you should consult the gurus in
comp.object.

--
roy axenov

Mar 17 '07 #2
Tim H wrote:

[snip]
Is there an idiomatic way to go from shared_ptr<Base to
shared_ptr<Deri ved?
There are tr1::dynamic_po inter_cast<and tr1::static_poi nter_cast<>.

Note that the need for up-casting may indicate a design problem.
Best

Kai-Uwe Bux
Mar 17 '07 #3
On Mar 17, 3:22 am, "roy axenov" <r_axe...@mail. ruwrote:
You're contradicting yourself. If those two classes don't
have much in common, why are you trying to store them in
the same vector?
It's a filesystem-like structure. Think directories and files. There
are a whole host of things you do with files that don't apply to
directories, and there are a whole host of things you do with
directories that you don't do to files. And yet, when you run 'ls'
you see them intermixed.

This is not a perfect analogy for the problem at hand, but it's
close. These two (for now, could be more) classes are distinct, but I
want to store them in the order they were discovered wrt each other.
Is there an idiomatic way to go from shared_ptr<Base to
shared_ptr<Deri ved?

Why would you want to?
I don't want to, but that's why I am here. This feels like wrong
design, and I am looking for a better answer. That said, it should be
*possible* because I know through my own meta-data that it's safe, and
in the end it's just a pointer.
All in all, it looks like more of a design problem than a
C++ problem to me. Perhaps you should consult the gurus in
comp.object.
Might just do that. Thanks.

Tim

Mar 17 '07 #4

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

Similar topics

17
4245
by: Douglas Alan | last post by:
Is there a canonical way of iterating over the lines of a file that are null-separated rather than newline-separated? Sure, I can implement my own iterator using read() and split(), etc., but considering that using "find -print0" is so common, it seems like there should be a more cannonical way. |>oug
7
3980
by: Matthias Czapla | last post by:
Hi! Whats the canonical way for handling raw data. I want to read a file without making any assumption about its structure and store portions of it in memory and compare ranges with constant byte sequences. _I_ would read it into arrays of unsigned char and use C's memcmp(), but as you see Im a novice C++ programmer and think that theres some better, typically used, way. Regards
3
10035
by: deko | last post by:
I have a (Access 2003) contact management database where the user can double-click a contact's phone number in a form and have the Windows Phone Dialer dial the number. The problem is the number has to be in canonical format or dialing rules won't be applied (cf. MSKB Article 318575). I don't want to use an Input Mask because users like to put comments after the number, like: "985-983-0098 ext. 980 - Mike B." I thought there might be a...
1
1793
by: Juan R. | last post by:
Introduction I am developing the CanonML language (version 1.0) as a way to generate, store, and publish canonical science documents on the Internet. This language will be the basis for the next version 2.0 of the website of the Center for CANONICAL |SCIENCE). The current preliminary version -in proof stage- has been developed on XHTML 1.1 + MathML 2.0 language without semantics (e.g. there exists not use of &lt;h1> or &lt;p>). We wait see the...
0
1668
by: Juan R. | last post by:
I have updated some basic requirements for a generic mathematical markup language for scientific requirements at the next link. http://canonicalscience.blogspot.com/2006/04/scientific-language-canonml-is.html] Some requirements fit into the XML model and could be considered for debate for the future mathML specifications. Other requirements do not fit and will be developed in alternative mathematical approaches to those from the w3c...
1
1749
by: Juan R. | last post by:
The initial CanonMath program presented here http://canonicalscience.blogspot.com/2006/02/choosing-notationsyntax-for-canonmath.html] was discussed with several specialists, including father of XML-MAIDEN project (which provided many interesting ideas over original desing). The initial CanonMath program (was abandoned) was presented at the w3c mailing list for mathematics. There was little discussion but subsequent discussion on others...
5
2021
by: wpmccormick | last post by:
What is the cleanest way to gain access to object methods and properties across classes and files in the same namespace? Example: A form object frmForm in file frmForm.cs creates obj1 defined in file obj1.cs, which in turn creates obj2 and obj3 defined in obj2.cs and obj3.cs, respectively. What is the canonical way for frmForm to access obj2 and obj3? For obj3 to access frmForm?
1
2548
by: zzz | last post by:
Hi all, I was recently reading the book "Write Great code by ryndall Hyde" in this in chapter 8 the following are given. given n input variables there are two raised to two raised to n unique Boolean functions ex:- for 2 i/p variables there are 16 different functions. then he mentions about canonical forms. he says about sum of min terms
2
4828
by: Jeffrey Walton | last post by:
Hi All, BMP Strings are a subset of Universal Strings.The BMP string uses approximately 65,000 code points from Universal String encoding. BMP Strings: ISO/IEC 10646, 2-octet canonical form, Universal String: ISO/ IEC 10646, 4-octet canonical form. An excellent discussion occured with respect to BMP Strings and .Net (see http://groups.google.com/group/microsoft.public.dotnet.languages.csharp/browse_thread/thread/f18fcb62156a1a0c/)....
18
6066
by: Andrus | last post by:
Marc, Thank you very much. I have issue on implementing add row properly using this. User presses down arrow in last row in grid starting adding new row. Then user changes its mind desiding that new row should not added and presses up arrow. DataGridView does not show this unfinished row anymore.
0
9673
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
10003
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
9047
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7546
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5440
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
5568
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4114
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
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2924
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.