Skip to main content
gamedev.net
🔒 Locked

Class Question

Started by NOMADMAN 3 replies 700+ views
Original Post
NOMADMAN
NOMADMAN
Say I have a templated class called array. All this class does is hold the template info in an array. Is it possible to put different types into the same templated class... I don''t think it is. So what if the things I want to put in are inherited from the same basic object. Anyone know of a clever way around this? Thanks! NOMAD
Oluseyi
Oluseyi
Is it possible to put different types into the same templated class?
No.

So what if the things I want to put in are inherited from the same basic object?
Make it an array of pointers to the base class. Oh, and use vectors (std::vector, found in <vector>) instead of arrays.
NOMADMAN
NOMADMAN
I was gonna ask about pointers, but I''m still kinda new to C++. If I make a pointer to the base type how would I know which of the inherited classes would have the spot? Would I need another pointer in my basic class?

Thanks!
Oluseyi
Oluseyi
quote:
Original post by NOMADMAN
If I make a pointer to the base type how would I know which of the inherited classes would have the spot?

You shouldn''t need to. The principle here is polymorphism - abstracting method interfaces such that they can be generically applied without care which type the object actually is. So if you have an hierarchy of DrawableObject classes, they should all provide the Draw method with the exact same parameter list.

More importantly, though, if you don''t understand C++ and its basic constructs well enough, perhaps you shouldn''t be attempting to solve problems that require them just yet.
NOMADMAN
NOMADMAN
ahh, But if I''m never "attempting to solve problems" how will I ever "understand C++ and its basics constructs"?

But you are right, I do not know about polymorphism. I will go learn about it, then come back with a more informed question.

Thank you Oluseyi!

NOMAD

Topic Locked

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

Sign in to reply to this topic.