473,790 Members | 3,254 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is heterogeneous container of boost::shared_p tr possible?

1 New Member
Hi, first post and here it goes...

This is the current code (working) that I have. It works but for large number of A & B objects, it's not the most efficient. It's also not really heterogenous either. It's really just a storage of the parent class objects.
Expand|Select|Wrap|Line Numbers
  1. class A
  2. { … }
  3. class B:public A
  4. { … }
  5.  
  6. vector<A*> combined;
  7. vector<A> list_A;
  8. vector<B> list_B;
  9. for(size_t i=0; i<list_A.size();i++)
  10. {
  11.    combined.push_back(new A(list_A[i]));
  12. }
  13. for(size_t i=0; i<list_B.size();i++)
  14. {
  15.    combined.push_back(new B(list_B[i]));
  16. }
  17.  
So instead, I changed to using boost shared pointers for list_A and list_B.
Now, I still need to combined those two different vectos into a single one.
Something like this:
Expand|Select|Wrap|Line Numbers
  1. vector< boost::shared_ptr<A> > list_A;
  2. vector< boost::shared_ptr<B> > list_B;
  3. for(size_t i=0; i<list_A.size();i++)
  4. {
  5.    combined.push_back(list_A[i]);
  6. }
  7. for(size_t i=0; i<list_B.size();i++)
  8. {
  9.    combined.push_back(list_B[i]);
  10. }
  11.  
I’d like to do something like the above but I’m not sure how to define my
vector< … > combined;

Following the original method, I thought about:

Expand|Select|Wrap|Line Numbers
  1. vector<  boost::shared_ptr<A>  > combined; 
  2.  
So how would I populate combined from list_B?
even though B inherits A, you can't just replace boost::shared_p tr<A> with boost::shared_p tr<B> directly right?
Oct 26 '07 #1
1 1982
weaknessforcats
9,208 Recognized Expert Moderator Expert
How does B inherit A ??

If there are no virtual functions in A, then you can have a vector<B>.
Otherwise you cannot since the virtual function in A indicates polymorphism and that indicates using base pointers or referecnes only.

Therefore, if A has a virtual function, then you can have a vector of shhared_ptr. Since all calls use the A interface there's nothing to combine. You put the B objects in the vector as shared_ptr<A>.
Oct 27 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
3153
by: Philippe Guglielmetti | last post by:
I just ported old (VC6) working code to VC7.1 and have trouble with something like: class A; // forward typedef boost::smart_ptr<A> Aptr; class B{ Aptr a; virtual ~B(); // implemented after A has been defined };
6
9059
by: Ryan Mitchley | last post by:
Hi all Given bool bResult; shared_ptr<cSampleData> pNewData; shared_ptr<cBase> pNewBase; where cSampleData is descended from cBase, the following gives me a valid pNewData to the correct type:
2
4838
by: krema2ren | last post by:
Hi I've the following header problem that I need two classes to know each other through a boost::shared_ptr. Does any of you smart guys have a solution? A.h ---------------------- #include "B.h"
2
2138
by: adebaene | last post by:
Hello group, There seems to be a bug int the interop layer in VC2005 when dealing with certain pointer types (or values?) Here is a repro case using Boost version 1.32 and C++/CLI : using namespace System; #pragma unmanaged
6
4036
by: Toby Bradshaw | last post by:
Hi, Consider the following: class A { public: virtual bool foo() = 0; };
2
3082
by: ketan | last post by:
Hi, While reading the Boost::shared_ptr implementation, I came across this method template<class Tclass shared_ptr { ..... template<class Y> shared_ptr(shared_ptr<Yconst & r, boost::detail::const_cast_tag):
4
3964
by: EnsGabe | last post by:
Suppose you have a class heirarchy as such: class Base{ .... }; class Mid1 : public Base{ ....
0
1581
by: phlip | last post by:
Nick Keighley wrote: CC'd to the correct newsgroup. Yes, the destructor of the shared pointer will delete the object. Then its former address will convert to a reference. At some point - possibly the conversion - Undefined Behavior will begin. And then when whoever collects the reference uses it, Undefined Behavior will continue.
10
11623
by: tradevol | last post by:
Hi, I am playing with boost pointer and try to wrap the following codes A* func(){ ... if(condition 1 ){ return a; } else
0
9666
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
9512
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
10419
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
10201
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
9987
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
5552
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4100
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
3709
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2910
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.