Calling method of child class on a vector of parent class?

You are encountering the slicing problem vector. Push_back() copies its argument, so it invokes Parent::Parent(const Parent &) (i.e. The copy constructor) This is really no different to: Parent x10; x0 = Parent(); x1 = Child() Or it might be Parent::operator=(const Parent &) i.e.

Copy-assignment. I'm tired, and I can't remember which it needs.

You are encountering the slicing problem. Vector. Push_back() copies its argument, so it invokes Parent::Parent(const Parent &) (i.e.

The copy constructor)*. This is really no different to: Parent x10; x0 = Parent(); x1 = Child(); * Or it might be Parent::operator=(const Parent &), i.e. Copy-assignment.

I'm tired, and I can't remember which it needs.

Slicing: a classic. – Seth Carnegie Sep 5 at 23:43 @Oli: I don't think that it's specified, actually. Placement new would be required for copy construction; initial default construction would be required for copy assignment.So I'd say a sane implementation will usually do the former.

– Tomalak Geret'kal Sep 5 at 23:49 n3290: 23.2. 1/13 describes CopyInsertableness, which is the only requirement. And it says that the default allocator will use placement new, but that others may not. – Tomalak Geret'kal Sep 5 at 23:55.

The vector stores Parents, not Childrens. If you want to take advantage of polymorphism, store pointers instead... but be damned sure that it's what you want.

1 This isn't really going to address the issue yet as method isn't virtual. – pmr Sep 5 at 23:53 @pmr: Indeed, there is more work to be done. However, I did not write my answer intending for it to be a 25-page book chapter on polymorphism.

The OP can look up that chapter in his own book for the full story. – Tomalak Geret'kal Sep 5 at 23:57 @Gerardo That's what I have been saying. Your member functions need to be virtual.

Tomalak is of course right: You need to read a book. – pmr Sep 6 at 0:01 Thanks Tomalak and @pmr , this helped a lot. – Gerardo Marset Sep 6 at 0:05.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions