Copy Constructors

This is a discussion on Copy Constructors within the C++ Programming forums, part of the General Programming Boards category; Do std::list/vector, boost::unorder_map, etc. have good copy constructors? or do I need to return by reference? if so, I guess ...

  1. #1
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034

    Question Copy Constructors

    Do std::list/vector, boost::unorder_map, etc. have good copy constructors? or do I need to return by reference? if so, I guess that means I have to wrap them in shared_ptr's? what about shared_array?

    This is sort of just an example...

    Code:
    typedef boost::unordered_map<std::string, std::string> Row;
    typedef std::vector<Row> RowList;
    
    RowList FetchRow();
    RowList FetchRowList();
    or
    Code:
    typedef boost::unordered_map<std::string, std::string> Row;
    typedef std::vector<Row> RowList;
    
    Row& FetchRow();
    RowList& FetchRowList();
    or
    Code:
    typedef boost::unordered_map<std::string, std::string> Row;
    typedef std::vector<boost::shared_ptr<Row> > RowList;
    
    Row FetchRowList();
    RowList FetchRowList();
    or
    Code:
    typedef boost::unordered_map<std::string, std::string> Row;
    typedef std::vector<boost::shared_ptr<Row> > RowList;
    
    Row& FetchRow();
    RowList& FetchRowList();
    or
    Code:
    typedef boost::unordered_map<std::string, std::string> Row;
    typedef boost::shared_ptr<Row> pRow;
    typedef std::vector<Row> RowList;
    
    pRow FetchRow();
    RowList FetchRowList();
    or
    Code:
    typedef boost::unordered_map<std::string, std::string> Row;
    typedef boost::shared_ptr<Row> pRow;
    typedef std::vector<pRow> RowList;
    
    pRow FetchRow();
    RowList& FetchRowList();
    or
    Code:
    typedef boost::unordered_map<std::string, std::string> Row;
    typedef boost::shared_ptr<Row> pRow;
    typedef std::vector<pRow> RowList;
    typedef boost::shared_array<RowList> pRowList;
    
    pRow FetchRow();
    pRowList FetchRowList();
    
    //pRowList r(new RowList); return r;
    etc.

    Last one?

    Thanks guys!!
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    Do std::list/vector, boost::unorder_map, etc. have good copy constructors? or do I need to return by reference? if so, I guess that means I have to wrap them in shared_ptr's? what about shared_array?
    The efficiency of STL containers is dependant on how they're implemented. At any rate, generally speaking, you want to return references wherever possible, and by value if you don't want the return value altered. It might be a good idea to have the functions take references to a user-supplied variable to cut down on object-creation overhead.

  3. #3
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by Sebastiani View Post
    The efficiency of STL containers is dependant on how they're implemented. At any rate, generally speaking, you want to return references wherever possible, and by value if you don't want the return value altered. It might be a good idea to have the functions take references to a user-supplied variable to cut down on object-creation overhead.
    Is the object creation overhead low with boost::shared_ptr? Would I need to take references to boost::shared_ptr, or just copies? or is it a thing where copies don't hurt, but references would be best (a benefit)?

    Other than that, I got it. I wasn't sure I should go with the smart pointer approach. Actually I have to, since the function is creating the object itself rather than taking an argument. Otherwise references would be correct, allowing the user to control the object lifetime. Either way.

    Thanks
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    Quote Originally Posted by Dae View Post
    Is the object creation overhead low with boost::shared_ptr? Would I need to take references to boost::shared_ptr, or just copies? or is it a thing where copies don't hurt, but references would be best (a benefit)?

    Other than that, I got it. I wasn't sure I should go with the smart pointer approach. Actually I have to, since the function is creating the object itself rather than taking an argument. Otherwise references would be correct, allowing the user to control the object lifetime. Either way.

    Thanks
    Object creation overhead is the same either way. I'd say just return it by value. The shared pointer approach is really something you'd use in place of references, not to save on overhead, really.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    If the container (vector, unordered_map, etc) has a lot of data, then it is unlikely that copying will be efficient, especially when you don't need the source of the copy. Return Value Optimization might solve this problem given the function prototypes you provided, so it might be worth it to simply use the copy and then profile to see if there is any slowdown.

    If you find a slowdown or want to optimize prematurely, then I would consider auto_ptr or something like it rather than shared_ptr. This is the example where auto_ptr makes sense, returning from a function. It has a pretty low overhead, although Idon't think shared_ptr's overhead is too bad either.

    Finally, I think Sebastiani's suggestion of taking a reference parameter and just filling the data in there rather than returning the container might be the best of both worlds.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with copy constructors
    By arya6000 in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2008, 12:34 AM
  2. Copy constructors; Best practices (and private)
    By Mario F. in forum C++ Programming
    Replies: 15
    Last Post: 06-23-2006, 04:42 PM
  3. Copy constructors and operator=()
    By filler_bunny in forum C++ Programming
    Replies: 13
    Last Post: 08-25-2003, 07:43 AM
  4. Copy constructors and private constructors
    By Eibro in forum C++ Programming
    Replies: 5
    Last Post: 11-24-2002, 09:16 AM
  5. Copy constructors........
    By incognito in forum C++ Programming
    Replies: 1
    Last Post: 05-02-2002, 08:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21