Skip to main content
GameDev.net gamedev.net
🔒 Locked

dynamic memory allocation class and derived classes

Started by markdezonia Aug 5, 2002 at 11:18 AM 5 replies 400+ views
Original Post
markdezonia
markdezonia
Is there an easy way to dynamic allocation a base class and derived class all in one. What if MyClass is the base class but you want to include the derived clases too. MyClass *NewClass = new MyClass[80]; how can you do that? is there an easy way of doing that? Becuase my derived classes all ready are dynamic allocation. So I will run in to problems dynamic allocationing my base class. but if I could dynamic allocation my base class and all my dirived classes it would be a cake walk. but I don''t think it''s possible. base class[80] - dirived[3] kind of a mult-arry classes?
Oluseyi
Oluseyi
quote:
Original post by markdezonia
Is there an easy way to dynamic allocation a base class and derived class all in one?

No. You must allocate them separately.
markdezonia
markdezonia
well now I don''t have to spend the next 5 hours on the web trying to find something that does not exist.
Thank you I appreciated your help.
ShawnO
ShawnO
Maybe I have misunderstood your question, but if you dynamically allocate an instance of your derived class your base class will be taken care of automatically.


  
BaseClass {
...
};

DerivedClass : public BaseClass {
...
};

DerivedClass *pArray1 = new DerivedClass[80];
BaseClass *pArray2 = new DerivedClass[80];


As you can see, you can refer to instances of your derived class with a pointer to the base class.

Let me know if this is not the question you were asking.
Shawn

When using the Windows calculator program, always remember to clear any values from memory before exiting to prevent burn-in.

[edited by - ShawnO on August 5, 2002 12:42:21 PM]
When using the Windows calculator program, always remember to clear any values from memory before exiting to prevent burn-in.
Oluseyi
Oluseyi
quote:
Original post by ShawnO
Maybe I have misunderstood your question, but if you dynamically allocate an instance of your derived class your base class will be taken care of automatically.

My understanding of his question was that he was asking if it was possible to allocate a series of objects dynamically, some of base type and some of derived type. Something like this:
Base * arr = new {Base[77], Derived[3]}; 

That would have resulted in a total of 80 dynamic allocations, the first 77 being of Base type. C++ provides no such syntax and supports no such functionality.
Oluseyi
Oluseyi
quote:
Original post by Anonymous Poster
STL

Not a solution. Interesting demonstration of the "panacea reflex", though.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.