Thread: How to hold pointers/refernces to an abstract class?

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    228

    How to hold pointers/refernces to an abstract class?

    Hi.
    I need some advice regarding the following:
    I have an abstract class named Terrain, and a class named RoadMap, which supposed to hold an N*N array of Terrains.
    But I'm not sure what type should the RoadMap class hold:


    Code:
    #ifndef TERRAIN_H
    #define TERRAIN_H
    
    
    class Terrain {
    public:
        virtual ~Terrain(){}
        virtual unsigned getCost()=0;
    };
    
    
    #endif /* TERRAIN_H */
    and:

    Code:
    #ifndef ROADMAP_H
    #define ROADMAP_H
    #include"Terrain.h"
    
    class RoadMap {
    public:
        RoadMap(int size); // to be determined at runtime
        virtual ~RoadMap();
    private:
        /* Two dimentional array of size*size Terrains */
        int terrain_size;
    };
    
    
    
    #endif /* ROADMAP_H */
    I can't use an array of refernces here, so I tried this:

    Code:
    Terrain** terrain;
    and then I thought this was the way to go:

    Code:
    Terrain (*terrain)[];
    But now I'm not sure.

    The N*N matrix size supposed to be determined according to a given input...
    What type should I use there?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    There are a few options, all with different trade-offs.

    "Terrain **terrain" requires you to manage all the memory allocations and deallocations explicitly. It would emulate a 1D array of pointers. A 2D array of pointers might be done using "Terrain ***terrain" - but bear in mind that a larger set of allocations and deallocations to be explicitly managed.

    "std::vector<Terrain *>" will manage an array of pointers dynamically, but require you to explicitly manage the lifetimes of the objects the pointers point at. The vector would need to be sized to hold N*N elements. If you want to access particular terrain objects using two integers you will need to map those two integers into a single value (i.e. terrain[i][j]->getCost() won't work, but a mapping like terrain[i + N*j]->getCost() would).

    "std::vector<std::vector<Terrain *> > will allow you to emulate a 2D array of pointers (vector of vectors of pointers). The difficulty is that you need to initialize lots of vectors (i.e. more code to construct your object, and more code if you ever need to resize) but the advantage is that you can treat it like a 2D array of pointers. (i.e. terrain[i][j]->getCost() will work if you have correctly set up all the vectors and contained pointers)

    If you are using C++11 (i.e. a recent compiler/library) you will be able to use "std::vector<std::unique_pointer<Terrain> >" (for a 1D array) or "std::vector<std::vector<std::unique_pointer<Terra in> > >" (for a 2D array). The advantage of unique_pointer is that it manages lifetimes of the contained Terrain objects - assuming they are allocated dynamically (i.e. with operator new).


    Where I've used "std::vector<>" above, any standard container will do (std::list, std::array, etc). The choice of container depends on what you're doing with the container, but vector is often a good default choice. All standard containers keep track of their own size, so you wouldn't necessarily need a separate variable to track the size.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    May 2013
    Posts
    228
    Great answer, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Setting up a Class to hold Objects with multiple attributes
    By IPthereforIam in forum C++ Programming
    Replies: 4
    Last Post: 04-11-2012, 10:50 AM
  2. Replies: 4
    Last Post: 03-22-2012, 01:11 PM
  3. Replies: 4
    Last Post: 04-04-2011, 12:40 AM
  4. Issue with pointers to an abstract class and inheritance
    By bainevii in forum C++ Programming
    Replies: 2
    Last Post: 03-31-2009, 07:51 AM
  5. abstract vs pure abstract class
    By Kohatian 3279 in forum C++ Programming
    Replies: 2
    Last Post: 05-10-2002, 11:12 AM