CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Inheritance Heirarchy Question.

  1. #1
    Join Date
    Dec 2003
    Posts
    112

    Inheritance Heirarchy Question.

    Okay, so suppose I have a set of classes something like this.

    Code:
    class MapCell
    {
    }
    
    class MapCellCollection
    {
    public:
    std::vector<MapCell*> cells;
    void CreateCells();
    void DeleteCells();
    }
    
    class MapCellDerived : public MapCell
    {
    }
    
    class MapCellCollectionDerived : public MapCellCollection
    {
    }
    Now, for my MapCellCollectionDerived, I want it to contain instances of MapCellDerived, not MapCell. You can see that the creation of these cells is done in the MapCellCollection.

    The actual process of creating these cells is fairly complex, and I don't want to re-write it all. For that reason, I don't want to simple override the CreateCells function. What I want is a way to re-use the CreateCells function, but have it create a different type.

    The CreateCells function does a bunch of stuff like Allocating Memory, computing sizes, and initializing the cells. This is the kind of thing I don't want to have to re-write.

    I could define a separate method called CreateSingleCell, and call it in CreateCells. Then I could override CreateSingleCell. CreateSingleCell would just be something like "return new MapCellDerived()", however I want my CreateCells to allocate memory in a single big block rather than having a bunch of calls to new.

    Does that make sense?

    Can anyone offer a suggestion, or am I looking at this the wrong way?
    Eggman
    Using: VS 2008 w. Net 3.5

  2. #2
    Join Date
    Aug 2007
    Location
    Farnborough, Hants, UK
    Posts
    45

    Re: Inheritance Heirarchy Question.

    I believe using "placement new" for your allocations would solve your problem - your CreateSingleCell() method could be passed the place in the memory block you want the object created, and placement new puts it there.

  3. #3
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Inheritance Heirarchy Question.

    however I want my CreateCells to allocate memory in a single big block rather than having a bunch of calls to new.
    And is that what you have right now? I rather doubt (because of the std::vector you are using). Why is that so important?
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  4. #4
    Join Date
    Dec 2003
    Posts
    112

    Re: Inheritance Heirarchy Question.

    The std::vector contains pointers to the Cells. The Cells themselves are allocated as a single chunk.

    It is important first to keep the number of allocations down, and second to avoid memory fragmentation etc.

    Quote Originally Posted by JTeagle
    I believe using "placement new" for your allocations would solve your problem - your CreateSingleCell() method could be passed the place in the memory block you want the object created, and placement new puts it there.
    I am not entirely sure how this solves the issue. Still need to allocate a large enough block of memory to pass to the CreateSingleCell method. Because of this, I still need to know ahead of time what type of object is being created.
    Eggman
    Using: VS 2008 w. Net 3.5

  5. #5
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Inheritance Heirarchy Question.

    The Cells themselves are allocated as a single chunk.
    Can you show us how? Can't you extract that piece of code and put it in a virtual function which you can override in he derived class(es)?
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  6. #6
    Join Date
    Dec 2003
    Posts
    112

    Re: Inheritance Heirarchy Question.

    Quote Originally Posted by cilu
    Can't you extract that piece of code and put it in a virtual function which you can override in he derived class(es)?
    That is sort of the direction I am considering right now. Basically there would be some sort of function "CreateCellBuffer(int cellCount)". This would create a block of cells of the required type. It would be virtual so I could override it in child classes.

    Then my CreateCells function would do some processing to figure out how many cells are needed, call the CreateCellBuffer function, and then use the resulting buffer to populate the list and do any other initializing.

    That way in my child classes, I could just create a cell buffer which contains different types as needed.
    Eggman
    Using: VS 2008 w. Net 3.5

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)