If I create a class called "tree" and inherit from it to classes called "elm" and "oak", can I, using all three header files, create a vector <tree> and use push_back to add an "elm" and an "oak" to that vector?

Recommended Answers

All 8 Replies

Why don't you write a simple code snippet and see? If the compiler doesn't complain, then it should be allowed.

Venomlash back again.
Wrote a code snippet, compiler didn't like it. The pain, the pain, the pain. Of course, it might just be that I'm not good with inheriting classes, so anyone who can get this to work, good job!:!:
Byyye!

Try making a vector of Tree*, and add elements which are Oak* and Elm*. Of course, this means you'll be working with pointers, but I'm assuming that you'll be fine with that...

>If I create a class called "tree" and inherit from it to classes
>called "elm" and "oak", can I, using all three header files, create a
>vector <tree> and use push_back to add an "elm" and an "oak" to that vector?
Polymorphism only works through references, so you'll want a vector<tree*> rather than vector<tree>. But if that's the case, yes you can do what you want. However, you'll end up with a heterogenous data structure, and you need a way to figure out which tree is which if you plan on using anything but inherited members.

Try making a vector of Tree*, and add elements which are Oak* and Elm*. Of course, this means you'll be working with pointers, but I'm assuming that you'll be fine with that...

Huh? What's a pointer? I can only write basic code? Please tell me what a pointer is? Apparently I can't stop using question marks?
Venomlash?

I'm glad you have a sense of humor? I'm sorry your first post didn't leave me with a clear impression of how familiar you are with C++? Do you need anything else? Maybe a few question marks?

Huh? What's a pointer?

Do you think this will help?
http://www.cprogramming.com/tutorial/lesson6.html

I'm sorry if everyone is starting to be sarcastic with these question marks we don't mean anything, hopefully it doesn't offend you?

I mean... :cheesy:

>I'm sorry if everyone is starting to be sarcastic with these question
>marks we don't mean anything, hopefully it doesn't offend you?
Yes, please don't take offense? Friendly banter is one of the best things about this forum? But our humor can be a little weird sometimes? Eh? Eh? Yes? No? :cheesy:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.