CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2008
    Posts
    3

    Array of template classes.

    I have some classes that are derived from two base class as shown below (I should add I cannot change the Base classes – Base & HeaderBase).

    template<typename T>
    class Base {
    public:
    typedef T header_type;

    Base () : m_addr(), m_header() {};
    header_type& GetHeader() { return m_header; }
    void SetHeader(header_type const& newHeader) { m_header = newHeader; }
    private:
    header_type m_header;
    };

    class HeaderBase {
    public:
    HeaderBase() { m_name = "Header";};
    std::string GetName() {return m_name;}
    void SetName(std::string name) {m_name = name;}

    protected:
    std::string m_name;
    };

    ///////////////////////////////////////////////////////////////////////////////////
    //I can Change all the code below..
    ///////////////////////////////////////////////////////////////////////////////////

    class TwoHeader: public HeaderBase {
    public:
    TwoHeader() { m_name = "TwoSectorHeader";}
    };

    class OneHeader: public HeaderBase {
    public:
    OneHeader() { m_name = "OneSectorHeader";}
    };

    class One: public Base <OneHeader> {
    public:
    One ();
    ~One ();
    };

    class Two: public Base <TwoHeader> {
    public:
    Two ();
    ~Two ();
    };


    What I’d like to do is create an array of Derived objects as follows but store them as an array of Base classes. But since the Base class takes in a template I cannot get this to work.


    int main() {

    Base<HeaderBase> *arrayOfBaseObjects[2];

    arrayOfBaseObjects[0] = One();
    arrayOfBaseObjects[1] = Two();

    return 0;
    };

    Any ideas?
    Thank you.

  2. #2
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    Re: Array of template classes.

    #1 use code tags to make your code readable
    #2 One and Two are two separate types, so storing objects of them in an array of Base results in object slicing (you lose all data except for base class´ data). Storing pointers to Base may fix this issue.
    - Guido

  3. #3
    Join Date
    Feb 2008
    Posts
    3

    Re: Array of template classes.

    Thanks for the reply. Could you possibly expand on what you said for #2 please? I'm not sure I follow.

    tx

  4. #4
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    Re: Array of template classes.

    I´ll construct a simple example:

    Code:
    class Base
    {
       int m_iBaseMember;
    
    public:
       Base();
       virtual ~Base();
    };
    
    class DerivedA : public Base
    {
       double m_dDerivedAMember;
    public:
       DerivedA();
    };
    
    class DerivedB : public Base
    {
       float m_fDerivedBMember;
    public:
       DerivedB();
    };
    
    Base[2] BaseArray;
    
    BaseArray[0] = DerivedA();
    BaseArray[1] = DerivedB();
    Each class has a specific memory layout (and size), your derived classes will need more memory than their base class because they have additional members. When declaring an array of base class objects the compiler reserves only memory for the base class, so only the base class´ member data can be stored in that array. The excess data of the derived objects will be cut off and not be stored in the array. This is called object slicing because you slice off parts of the derived object.
    You can fix this by using pointers to objects because pointers have fixed size no matter what they´re pointing at.
    - Guido

  5. #5
    Join Date
    Aug 2007
    Posts
    9

    Re: Array of template classes.

    I think that you can get what you need if you will define some other class that will be inherited by your Base class, this class won't be a template. Then you will have no problem to create an array of all derived classes even though they are all templates.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured