You know, I don't know why I didn't just use my own project as an example of polymorphism. I even asked about smart pointers using polymorphism the other day. Why I didn't just pull from that is beyond me. Perhaps it's me recovering from a semester of C boot camp, or maybe my mind is fumbling with finals.
Say you have a base class, Base, and some classes that derive from it.
Polymorphism allows you to do this:
1 2 3 4 5 6 7
|
std::vector<Base *> myObejcts;
DerivedClass1 *derivedPtr1 = new DerivedClass1;
DerivedClass2 *derivedPtr2 = new DerivedClass2;
myObejcts.push_back(derivedPtr1);
myObjects.push_back(derivedPtr2);
|
This is
exactly what I'm doing in my project.
You can push a pointer to any derived class into that vector, as long as that derived class derives from Base. Of course, make sure they actually point to an allocated object and not just garbage in memory.
You
must use pointers to properly achieve this.
See this little stack overflow page about why pointers have to be used:
http://stackoverflow.com/questions/15188894/why-doesnt-polymorphism-work-without-pointers-references
That is all I will give code for, as you
really need to try this yourself. There are many examples out there. A simple Google search will find you many.
See:
http://www.cplusplus.com/doc/tutorial/polymorphism/
http://bfy.tw/3IfH
As I've recently learned, it is best to use smart pointers for this:
shared_ptr
:
http://en.cppreference.com/w/cpp/memory/shared_ptr
unique_ptr
:
http://en.cppreference.com/w/cpp/memory/unique_ptr
MSDN article on smart pointers:
https://msdn.microsoft.com/en-us/library/hh279674.aspx
Smart pointers make memory management much less of a pain.
There are also other smart pointers. Look at them as well.
That should address:
...Or use of a container declared with base pointer. |
What I did was do exactly that. A vector declared with a base class pointer, and I pushed derived class pointers into it. Of course, that is not the only way to take advantage of polymorphism. You can also pass a pointer of a derived class to a function that has a base class pointer as a parameter, for example.
My previous code was more of an example of inheritance than anything. If it showed any polymorphism, it was done poorly.
Despite what looks like stupidity in me, I cannot begin to describe how powerful polymorphism is. I do know what polymorphism is, so please pardon my malfunction. But thank you for also addressing it in a
kind way instead of just ripping me to shreds. You all are much more respectful than my professors, who seem to have no problem insulting my intelligence. I spent time to clear up my mind and clear up concepts.
You will not do casting in polymorphism, as it defeats the purpose. That isn't to say that casting isn't ever useful, but only use it when it makes sense.
why suddenly talk about STL containers already? |
STL containers are incredibly powerful tools, and provide great flexibility, not to mention everything else they do for you. They should be your default choice for containing objects.
Even if you don't fully understand templating, you should still learn how to use STL containers. I learned how to use them long before I learned how to make template classes.
See:
http://www.cplusplus.com/reference/stl/
why talk about pointer too? |
Polymorphism cannot be properly achieved without using pointers.
See my stack overflow page linked above.
Also See 'Obejct Slicing', something you'll want to avoid like the plague:
http://www.geeksforgeeks.org/object-slicing-in-c/
http://stackoverflow.com/questions/274626/what-is-object-slicing
@TheIdeasMan
I can start a new topic about this if anyone is interested. |
Why don't you? OP might not want to see it, but I'd be very interested to see this. I've learned more here in the last two weeks than I did in half a semester in school (at least with C++, that is).
Perhaps this is me trying to salvage any credibility I may still have. Take it as you will. I can partially attribute this to being sent into C boot camp, and becoming somewhat distanced from C++, as well as a lack of thorough instruction from professors - I've since learned to check what they teach me for validity before asserting its truth. I've tried to be meticulous with this post to make sure I got it right, and addresses what was shown was wrong with my last code example. No more quickly typed-out responses from me, as we've seen what happens what I do that. I'll be much more meticulous with my posts and make sure I actually use the right concept.
OP: Read the articles and read the comments posted from others. They explain why pointers and STL containers are used, as well as other concepts you need to know. You can always use Google - it is your friend. How do you think I found the articles that I linked in here?
I had this typed up a little while ago, then clicked out of the box and hit 'Backspace'. Boom, it was gone. This website needs a 'Confirm' when you have stuff typed in the box and you try to leave the page before posting it. Now I'm copying my text every minute in case I do that again. Also, why 'polymorphism' isn't a recognized word baffles me.
Oops. Did it again. Glad I had that copied. No way I would have typed this out a third time.
TL;DR: There is none. Read the post, lazy.