Design question (sort of)

This is a discussion on Design question (sort of) within the C++ Programming forums, part of the General Programming Boards category; Hello, Say I want to create a class, that is going to contain a vector of some other objects. My ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    14

    Design question (sort of)

    Hello,

    Say I want to create a class, that is going to contain a vector of some other objects. My question is this: Do you want the vector to contain the actual objects, or do you want the vector to contain pointers to dynamically allocated objects.

    You can assume that there were be setters and getters for this vector, as well as functions to add and remove items.

    In Java this wouldn't be an issue, but in C++ I don't know how to go abou it. If I create a vector of objects, then I can return it using a const reference, so no extra expensive copies are going to happen. But then again having a vector of pointers might come in handy at some other kind of operation, which I can't think of right now.

    All comments are welcome. Thanks...

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,632
    One thing that you might do is store base class pointers in the vector so as to use polymorphism via virtual function calls. But if you are not doing this, chances are you should keep things simple and just store the objects directly.

    Note that you can store smart shared pointers or even use a pointer container.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    14
    So if I want to make my code more extensible I should use pointers I suppose...?

    As for smart shared pointers or pointer containers, doesn't that make things more complicated?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,632
    Quote Originally Posted by crazyinlove
    As for smart shared pointers or pointer containers, doesn't that make things more complicated?
    Yes, but the complication is hidden by the interface. Furthermore, if you really do need to approximate the behaviour they provide, then it does not make things more complicated because things are already inherently complicated. In fact, it makes things simpler by simplifying through an interface and existing implementation.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    14
    OK good. I'll stick to vector of objects for this project, but i'll keep vector of pointers in mind for the future

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting a linked list using merge sort question..
    By kakaroto in forum C Programming
    Replies: 17
    Last Post: 08-07-2009, 12:13 PM
  2. Straight Insertion Sort function problem
    By StaticKyle in forum C++ Programming
    Replies: 6
    Last Post: 05-12-2008, 04:03 AM
  3. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  4. Question - GUIs and Actual Program Design
    By Enoctis in forum C++ Programming
    Replies: 10
    Last Post: 10-03-2005, 08:51 PM
  5. program design question
    By Chaplin27 in forum C++ Programming
    Replies: 0
    Last Post: 06-23-2005, 06:58 PM

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