Jump to content
  • Advertisement
Sign in to follow this  
TheSkywise

Copy Constructor with STL Containers

This topic is 5446 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Quote:
Original post by Xai
but then what would you do if you had to sort or rearange your list a lot ...


Thats not a problem, if you sort list it will change node links it wont cause any reallocation or copying of the actual elements it holds.

EDIT: looking at your example, list doesn't have a resize method nor does it need to.

At the end of the day except for certain exceptions which are:

the user-defined type has an expensive copy & assigement operations,

the user-defined type has no meaning to copy/assign its copy and assigmenet operations are private,

To be more efficent by sharing instances from another container that holds the actual type in question,

you need heterogeneous containers,

you need polymorphic behaviours in STL containers.

Storing pointers to the type in question can be more harmful than good, for one pointers are small objects and the default allocator types for STL containers use general C++ heap memory operations which are not very efficent for small objects.

Secondly you can cause heap fragmentation calling global standard new for individual instances each time you go to add a new element to the container of pointers, also the default allocator will copy construct the address on the heap aswell so except for the above cases its better to store the actual type.

[Edited by - snk_kid on September 23, 2004 5:36:44 PM]

Share this post


Link to post
Share on other sites
Advertisement
Here two ways to handle it:

version 1:

#include <list>


struct map {};
struct stellarObject {};

class sector_map {

std::list<map> maps;
std::list<stellarObject> stellars;

public:

typedef std::list<map> map_list;
typedef std::list<stellarObject> stellar_list;

typedef map_list::iterator map_itr;
typedef map_list::const_iterator const_map_itr;

typedef stellar_list::iterator stellar_itr;
typedef stellar_list::const_iterator const_stellar_itr;

sector_map(): maps(), stellars() {}

void insert_map(const map& new_map) {
maps.push_back(new_map);
}

void insert_stellar(const stellarObject& stellar) {
stellars.push_back(stellar);
}

map_list::size_type GetNumMaps() const { return maps.size(); };
stellar_list::size_type GetNumSOs() const { return stellars.size(); };

/* ... omitting code ... */
};



This stores the actual types, so the default copy constructor & assigmenet operator implicitly defined will handle proper copying/assigning for you, list handles memory management aswells.

version 2:

#include <boost\pool\pool_alloc.hpp>
#include <boost\pool\object_pool.hpp>
#include <boost\smart_ptr.hpp>
#include <list>

struct map {};
struct stellarObject {};

class sector_map {
public:

typedef boost::shared_ptr<map> map_ptr;
typedef boost::shared_ptr<stellarObject> stellar_ptr;
typedef boost::pool_allocator<map_ptr> map_allocator;
typedef boost::pool_allocator<stellar_ptr> stellar_allocator;

typedef std::list< map_ptr, map_allocator > map_list;
typedef map_list::iterator map_itr;
typedef map_list::const_iterator const_map_itr;

typedef std::list< stellar_ptr, stellar_allocator > stellar_list;
typedef stellar_list::iterator stellar_itr;
typedef stellar_list::const_iterator const_stellar_itr;

private:

stellar_list stellars;
map_list maps;

public:

sector_map(): stellars(), maps() {}

void insert_map(map_ptr new_map) {
maps.push_back(new_map);
}

void insert_stellar(stellar_ptr stellar) {
stellars.push_back(stellar);
}

map_list::size_type GetNumMaps() const { return maps.size(); };
stellar_list::size_type GetNumSOs() const { return stellars.size(); };

/* ... omitting code ... */
};



You'll need to download boost library for this, this version stores smart pointers and uses a custom allocator boost's pool allocator for the container, this is better for small & fixed sized objects such as pointers/smart pointer.

As you can see i haven't explicitly defined a copy constructor & assigmenet operator because the default implicitly defined will do a shallow copy by copying smart pointers, this should be okay because smart pointers know how & when to delete the instances they refer to for you.

In this context I don't see any point in providing proper copy schematics for this problem because you'll lose the benefit of storing pointers in a container in the first place i.e. to avoid the over-head of the copy/assignement operations when doing operations on containers such as adding a new element to the container.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!