ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
You cannot put a derived class object into a container of base class objects.
Trying to do so will make a base class object by copying the base class portion of the derived class object you tried to insert.
Virtual functions would be the answer if you were trying to use a base class reference or pointer that points to a derived class object and you wanted the derived class version of the function.
But containers of polymorphic objects represent a much harder problem in C++.
You can have a container of base class pointer, pointing to polymorphic objects (then you would still need to make the functions virtual to achieve what you want). But a container of pointers introduces a bunch of extra issues involving object ownership (decision of when to delete) as well as the syntactic overhead of the extra level of indirection.
brazilnut, I usually start with simpler examples to simplify debugging. After the smaller pieces work, putting them into bigger pieces either will work, or the problem is in the bigger pieces. I inserted the following lines before the pushback calls:
Code:
n.print();
na.print();
nb.print();
The output then looked like
Quote:
test
a: test
in
zoom: in
test
test
in
So the problem is that the pushback() and print() functions of the vector class don't work as you expect.
No, the main problem was that the OP wasn't considering the importance of virtual functions. posixculprit told him, and brazilnut totally got the message.
And Johnsfine's note about "object pointers" vs. "stack objects", and the whole issue of "copy semantics", was absolutely correct. Another (of the many reasons!) I believe C++ sucks ... at least as a beginners language.
Brazilnut - as a courtesy, you might want to "thank" posixculprit (the blue "thumbs up" icon) and/or mark his thread as "useful" (I did the same). You might also want to mark the thread as "solved" (assuming the problem is indeed resolved).
The compiler never complained about any type conflict when it was asked to use the derived types. It simply used the objects' base classes. That is the type declared for the container.
In the Thread Tools menu near the top of your view of the thread.
Quote:
can't change the title from edit.
Edit your first post in the thread, then click the Go Advanced button. Then it lets you edit the title. But if adding "Solved" was the reason to edit the title, use the Thread Tools menu instead.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.