CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 1999
    Location
    CA,USA
    Posts
    22

    Multiple Inheritance and operator new

    I need to define my own operator new in order to perform memory management in a large project.
    Everything is fine, until I try to compile a class which multpily inherits from several bases which inherit from the storag management class, then VC++ doesn't know which "new" to use, although it really comes down to the same one, i.e.

    //--------------------------Define new for Memory Management---------------------------------------
    class Base1
    {
    public:
    void *operator new(unsigned int); // "our" new operator
    void operator delete(void *, unsigned int); // "our" delete operator
    };
    //--------------------------Level 1 Inheritance----------------------------------------------------

    class Level1A : public Base1
    {
    protected:
    int temp1a;
    };

    class Level1B : public Base1
    {
    protected:
    int temp1b;
    };
    class Level1C : public Base1
    {
    protected:
    int temp1c;
    };
    //--------------------------Multiple Inheritance---------------------------------------------------
    class Level2 : public Level1A,
    public Level1B,
    public Level1C
    {
    protected:
    int morejunk;
    };
    //--------------------------Main-------------------------------------------------------------------
    int main(int argc, char **argv)
    {
    Level2 *pcl2 = new Level2;
    return(0);
    }



    Is there a simple way around this problem?

    Bryan.



  2. #2
    Join Date
    May 1999
    Posts
    667

    Re: Multiple Inheritance and operator new

    If Base1 is only used for Operator new/delete funcionality declare it as virtual in each of the derived classes
    ex:
    class Level1A : virtual public Base1
    {
    protected:
    int temp1a;
    };

    class Level1B : virtual public Base1
    {
    protected:
    int temp1b;
    };
    class Level1C : virtual public Base1
    {
    protected:
    int temp1c;
    };
    class Level2 : public Level1A,
    public Level1B,
    public Level1C
    {
    protected:
    int morejunk;
    };

    Now Level12 has only one instance of Base1 and this should resolve your problem


    HTH,
    Chris


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