Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C++ Questions: need response urgently

Name: Anonymous 2010-04-27 23:23

I'm working on my final project for my Object-Oriented programming class. The language I'm using (and that the course was taught based around) is C++. We have relatively simple projects that we designed in a previous class and now we're implementing them.

 I'm in a situation where I need to have an STL vector that includes items from two different child classes derived from the same parent class. The parent class never exists on its own, and the only difference between the child classes is the value of a boolean return function that returns "true" for one child class and "false" for the other. I realize it's stupid, but we were required to shoehorn class inheritance into our projects somewhere.

What I did was create a vector of the parent class and then push_down() items of the child classes into it. I try to compile after every significant bit of coding to make sure I havent broken anything, and the compiler (Microsoft VS 2008) had no issue with doing that. But when I try to write a for loop to access items of every item of the vector in sequence, it tells me that the function added in the child class doesn't exist.

I know I'm doing this wrong somehow, but I don't know what I should do about it. I'm due to give a presentation tomorrow, and I'm pretty sure my professor's asleep by now, so I can't ask him.

Help me, /prog/! Please!

Name: Anonymous 2010-04-27 23:42

To rephrase a bit, as I may have rambled on a bit too much (I'm working an all-nighter here on very little sleep):

How can I get multiple types of child classes of the same parent into the same STL vector in C++? Is such a thing even possible?

Once again, prompt help would be appreciated. I'm at an impasse here until I get this working, as I can't make sure my program will work as intended until I can get it to compile.

Name: Anonymous 2010-04-27 23:53

C++? Is such a thing even possible?
( ≖‿≖)

Name: Anonymous 2010-04-27 23:55

Sepples
YHBT

Name: Anonymous 2010-04-27 23:58

Name: Anonymous 2010-04-28 0:00

Watch me troll classical Pragusticians by HELPING HIM

>>1
If the function wasn't originally defined in the parent class and overridden in the child classes, then the compiler won't know it exists. Learn to use virtual.

Name: Anonymous 2010-04-28 0:03

>>6
Watch me troll classical Pragusticians by HELPING HIM
You have a lot to learn about classical ``Praguesticians''

Name: Anonymous 2010-04-28 0:06

Use vector<Parent*>, not vector<Parent>. Then use dynamic_cast<>

Name: Anonymous 2010-04-28 0:34

You waited to fix this until the day before you gave a presentation on it? 
Sucks to be you.

Name: Anonymous 2010-12-09 5:17

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List