meaw | Utility for Unicode East Asian Width cat2 | User Interface library
kandi X-RAY | meaw Summary
kandi X-RAY | meaw Summary
Utilities for Unicode East Asian Width (EAW).
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of meaw
meaw Key Features
meaw Examples and Code Snippets
Community Discussions
Trending Discussions on meaw
QUESTION
You have 2 classes Cats and Dogs and need to create a vector of shared pointers which stores the data from those 2 classes. Hints: polymorphism and keep in mind that classes can have similar fields.
So this is what I've done until now. I want to insert in that shared_ptr
vector
all the info Cats
and Dogs
classes have, but I don't know how. I only managed to insert in that vector
the data from the base class.
ANSWER
Answered 2020-Mar-25 at 20:30There are a few problems with your code:
Animal
lacks anyvirtual
methods. At the very least, it needs avirtual
destructor, so that the destructors ofCats
andDog
are called correctly whenshared_ptr
callsdelete
on its heldAnimal*
pointer.add()
is creating an instance ofAnimal
specifically, regardless ofT
. So yourvector
contains only realAnimal
objects.add()
needs to create an instance ofT
instead. Astd::shared_ptr
can be assigned to astd::shared_ptr
whenT
derives fromAnimal
. Of course,add()
is redundant,main()
can just create and add the new objects directly to itsvector
without usingadd()
at all.When
main()
callsoperator<<
on anAnimal
, it will not call theoperator<<
defined byCats
orDog
, only theoperator<<
defined byAnimal
. This can be fixed by havingoperator<<
inAnimal
call avirtual
method thatCats
andDog
override. There is no need to defineoperator<<
in derived classes when the base class also has anoperator<<
.
Try this instead:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install meaw
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page