473,785 Members | 2,789 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Arrays of smart pointers to base class

Hi all.

I am sure this is an oldie, but I can't find a useful suggestion on
how to solve it.

I have a class hierarchy of classes derived from a base class.
I would like to set up a vector of smart pointers to the base
class, and access the virtual functions of any class in the
hierarchy through the virtual functions.

For some reason the compiler doesn't like the way I do things
(code showing intentions of use below).

Any pointers to standard solutions are highly appreciated.

Rune

/////////////////////////////////////////////////////////////////////////////////////////
#include <boost/scoped_ptr.hpp>
#include <iostream>
#include <vector>

class base{
public:
virtual void report() const { std::cout << "Base Class Report" <<
std::endl;};
virtual ~base(){std::co ut << "Base Class Destructor" << std::endl;};
};

class derivedA : public base{
virtual void report() const{std::cout << "Derived A Class Report" <<
std::endl;};
virtual ~derivedA(){std ::cout << "Derived A Class Destructor" <<
std::endl;};
};

class derivedB : public base{
virtual void report() const{std::cout << "Derived B Class Report" <<
std::endl;};
virtual ~derivedB(){std ::cout << "Derived B Class Destructor" <<
std::endl;};
};

int main(int argc,char* argv[])
{
std::vector<boo st::scoped_ptr< base v;
v.push_back(boo st::scoped_ptr< base>(new base)); // Compile error
v.push_back(boo st::scoped_ptr< base>(new derivedA)); // Compile error
v.push_back(boo st::scoped_ptr< base>(new derivedB)); // Compile error

std::vector<boo st::scoped_ptr< base::iterator i;
for (i=v.begin();i! =v.end();++i)
(*i)->report();

return 0;
}
Sep 11 '08 #1
1 2527
On 11 Sep, 15:21, Rune Allnor <all...@tele.nt nu.nowrote:
Hi all.

I am sure this is an oldie, but I can't find a useful suggestion on
how to solve it.

I have a class hierarchy of classes derived from a base class.
I would like to set up a vector of smart pointers to the base
class, and access the virtual functions of any class in the
hierarchy through the virtual functions.

For some reason the compiler doesn't like the way I do things
(code showing intentions of use below).

Any pointers to standard solutions are highly appreciated.

Rune

///////////////////////////////////////////////////////////////////////////*//////////////
#include <boost/scoped_ptr.hpp>
#include <iostream>
#include <vector>

class base{
public:
* * * * virtual void report() const { std::cout << "Base Class Report" <<
std::endl;};
* * * * virtual ~base(){std::co ut << "Base Class Destructor" << std::endl;};

};

class derivedA : public base{
* * * * virtual void report() const{std::cout << "Derived A ClassReport" <<
std::endl;};
* * * * virtual ~derivedA(){std ::cout << "Derived A Class Destructor" <<
std::endl;};

};

class derivedB : public base{
* * * * virtual void report() const{std::cout << "Derived B ClassReport" <<
std::endl;};
* * * * virtual ~derivedB(){std ::cout << "Derived B Class Destructor" <<
std::endl;};

};

int main(int argc,char* argv[])
{
* * * * std::vector<boo st::scoped_ptr< base v;
* * * * v.push_back(boo st::scoped_ptr< base>(new base)); // Compile error
* * * * v.push_back(boo st::scoped_ptr< base>(new derivedA)); // Compile error
* * * * v.push_back(boo st::scoped_ptr< base>(new derivedB)); // Compile error

* * * * std::vector<boo st::scoped_ptr< base::iterator i;
* * * * for (i=v.begin();i! =v.end();++i)
* * * * * * * * (*i)->report();

* * * * return 0;

}- Hide quoted text -

- Show quoted text -
The following reworked code works:

///////////////////////////////////////////////////////////////////////////*//////////////
#include <boost/shared_ptr.hpp>
#include <iostream>
#include <vector>
class base{
public:
virtual void report() const { std::cout << "Base Class Report"
<<
std::endl;};
virtual ~base(){std::co ut << "Base Class Destructor" <<
std::endl;};

};
class derivedA : public base{
public:
virtual void report() const{std::cout << "Derived A Class
Report" <<
std::endl;};
virtual ~derivedA(){std ::cout << "Derived A Class Destructor"
<<
std::endl;};
};
class derivedB : public base{
public:
virtual void report() const{std::cout << "Derived B Class
Report" <<
std::endl;};
virtual ~derivedB(){std ::cout << "Derived B Class Destructor"
<<
std::endl;};
};
int main(int argc,char* argv[])
{
std::vector<boo st::shared_ptr< base v;
v.push_back(boo st::shared_ptr< base>(new base)); // Compile
error
v.push_back(boo st::shared_ptr< base>(new derivedA)); // Compile
error
v.push_back(boo st::shared_ptr< base>(new derivedB)); // Compile
error

std::vector<boo st::shared_ptr< base::iterator i;
for (i=v.begin();i! =v.end();++i)
(*i)->report();
return 0;
}
I changed the scoped_ptrs to shared_ptrs. scoped_ptr AFAIK is designed
for simple RAII. I also declared the methods in derivedA and derivedB
public (having private destructors didn't help!!).
Sep 11 '08 #2

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

Similar topics

4
1362
by: Eric | last post by:
See question in main function below...TIA. struct A {}; struct B: public A {}; #include <boost/shared_ptr.hpp> #include <set> typedef boost::shared_ptr<A> AP; typedef std::set<AP> AS;
25
2515
by: Jack | last post by:
Hi, Is there a general solution for the following problem: I have an array of instances of class B. Class B is publicly derived from class A. Then I have a class named Buffer that generally takes care of allocating and deallocating arrays. Class Buffer knows only that the objects contained in its internal array are derived from class A but not that they are exactly of type B, so Buffer has a member variable of type A* that points at...
92
5124
by: Jim Langston | last post by:
Someone made the statement in a newsgroup that most C++ programmers use smart pointers. His actual phrase was "most of us" but I really don't think that most C++ programmers use smart pointers, but I just don't know. I don't like them because I don't trust them. I use new and delete on pure pointers instead. Do you use smart pointers?
33
5083
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the actual counting. Here is the latter's definition: // --- Begin ReferenceCountable.h ---------- class ReferenceCountable
12
5221
by: Henrik Goldman | last post by:
Hi, I have some data which is stored as plain old C structures. Until now I've had arrays of these structs on stack but due to stack overrun I need to move these to heap. I've been trying out some smart pointers to help but so far the implementation I've been dealing with has had problems with operator since it could conflict when using the same class with "unsigned char". Do you have any recommendations for smart pointers able to...
3
2856
by: mati-006 | last post by:
Hi, I think the code will be the best way to explain what I mean: #include "arglib/arg_shared.h" class base { public: base() {} virtual ~base() {} };
3
2015
by: Tim H | last post by:
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...
54
12020
by: Boris | last post by:
I had a 3 hours meeting today with some fellow programmers that are partly not convinced about using smart pointers in C++. Their main concern is a possible performance impact. I've been explaining the advantages of smart pointers endlessly (which are currently used in all our C++ software; we use the Boost smart pointers) as I'm seriously concerned that there is a shift to raw pointers. We are not developing system software but rather...
1
2105
by: mosfet | last post by:
Hi, I am trying to modify existing code to use smart pointers and I get some issues with virtual methods : class Folder : public Object { public: friend class PimItemCollection; friend class ContactCollection;
0
9480
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,...
1
10092
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
8973
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...
0
6740
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
5381
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
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
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
3647
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
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.