473,327 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Copy constructor for class with vector of pointers

Hi, I have developed a little class to help implement dynamic menus in
our
application which has a UI. This allows our engine to send a MenuItem
object to the UI which is an observer of the engine, then the UI can
construct
its menu based on the info in the object. The MenuItem is based on the
composite
design pattern so it contains a vector of child MenuItems, this allows
sub menus
to passed in aswell.

I currently don't know how to implement a copy constructor for this,
any ideas?
The vector won't contain any base class pointers to derived objects if
that
makes things easier. Second i read in Effective C++ not return handles
to
object internals which i seem to be doing in the :

const std::vector<MenuItem*>& SubMenuItems() const; method

I can't see how to get round this. Any other comments welcome.
Thanks
#include <string>
#include <vector>
#include <memory>
class MenuItem
{
public:
MenuItem();
MenuItem(std::wstring name, int id);
virtual ~MenuItem();

int Id() const;
std::wstring Name() const;
const std::vector<MenuItem*>& SubMenuItems() const;
void AddSubMenu(std::wstring name, int id);
bool SubMenuPresent() const;
typedef std::vector<MenuItem*SubMenuItem;

private:
MenuItem(const MenuItem& item);
MenuItem& operator=(const MenuItem&);

private:
std::wstring m_itemName;
int m_id;
SubMenuItem m_subMenuItems;
};

MenuItem::MenuItem():m_itemName(L""),m_id(0)
{

}

MenuItem::MenuItem(std::wstring name, int
id):m_itemName(name),m_id(id)
{
}

MenuItem::~MenuItem()
{
// Iterate through all submenuitems

SubMenuItem::iterator it = m_subMenuItems.begin();
while ( it != m_subMenuItems.end() )
{
delete *it;
it = m_subMenuItems.erase(it);
}
}

int MenuItem::Id() const
{
return m_id;
}

std::wstring MenuItem::Name() const
{
return m_itemName;
}

const std::vector<MenuItem*>& MenuItem::SubMenuItems() const
{
return m_subMenuItems;
}

bool MenuItem::SubMenuPresent() const
{
return !m_subMenuItems.empty();
}

void MenuItem::AddSubMenu(std::wstring name, int id)
{
std::auto_ptr<MenuItemitem(new MenuItem(name, id));
m_subMenuItems.push_back(item.get());
item.release();
}
int main()
{
// Engine code would do this and pass the object to the UI
MenuItem item(L"File", 0);
item.AddSubMenu(L"New", 1);
item.AddSubMenu(L"Edit", 2);

// UI code would read the item object and construct menu
std::vector<MenuItem*>::const_iterator it =
item.SubMenuItems().begin();
while( it != item.SubMenuItems().end())
{
// Create submenu etc
++it;
}
return 0;
}
Jul 15 '08 #1
0 1512

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

Similar topics

39
by: scooter | last post by:
Given this class heirarchy class Base{}; class A : public Base {}; class B : public Base {};
6
by: Charles Herman | last post by:
I am using push_back to place a class object into a vector. This class contains data members that are pointers to a char string. When I use push_back, I believe a copy of the object is first made,...
1
by: Simon | last post by:
Hi, I have a class that does not seem to work. I cannot see the problem, and the "fix" I have found does not help me understand what the problem was I know I don't need a copy constructor but...
11
by: PengYu.UT | last post by:
The following program calls the normal constructor and the copy constructor. By calling the copy constuctor is redundandant, all I want is only a vector of a trial object. Is there any way to...
3
by: pocmatos | last post by:
Hi all, Imagine I have: class X { public: // Some methods... private: vector<MyObject*> vec;
17
by: qazmlp1209 | last post by:
Will the following program cause any problem, because of the missing explicit copy constructor for the 'test' class? #include <vector> #include <iostream> class test { char* cPtr ;
13
by: Jeroen | last post by:
Hi all, I'm trying to implement a certain class but I have problems regarding the copy ctor. I'll try to explain this as good as possible and show what I tried thusfar. Because it's not about a...
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
10
by: Alex Snast | last post by:
hello I'm trying to implement a copy constructor for a vector of pointers to a base class (which is abstract) My question is how would i know what kind of new type should i allocate since the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.