Click here to Skip to main content
15,868,016 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: stl list<>::iterator for derived classes ?!? Pin
Christian Graus5-Jan-06 11:58
protectorChristian Graus5-Jan-06 11:58 
GeneralRe: stl list::iterator for derived classes ?!? Pin
tbrake5-Jan-06 12:08
tbrake5-Jan-06 12:08 
GeneralRe: stl list::iterator for derived classes ?!? Pin
Christian Graus5-Jan-06 12:11
protectorChristian Graus5-Jan-06 12:11 
QuestionRe: stl list::iterator for derived classes ?!? Pin
tbrake5-Jan-06 12:29
tbrake5-Jan-06 12:29 
AnswerRe: stl list::iterator for derived classes ?!? Pin
Christian Graus5-Jan-06 12:32
protectorChristian Graus5-Jan-06 12:32 
GeneralRe: stl list::iterator for derived classes ?!? Pin
tbrake5-Jan-06 12:43
tbrake5-Jan-06 12:43 
GeneralRe: stl list::iterator for derived classes ?!? Pin
Christian Graus5-Jan-06 12:46
protectorChristian Graus5-Jan-06 12:46 
AnswerRe: stl list<>::iterator for derived classes ?!? Pin
Christian Graus5-Jan-06 12:55
protectorChristian Graus5-Jan-06 12:55 
OK, I've done some testing:

this works:


#include <string>
#include <iostream>
#include <list>

class CAR
{
public:
virtual char * GetInfo() { return "BASE"; };
};

class BMW: public CAR
{
char * GetInfo() { return "BMW"; }
};

class Gemini :public CAR
{
char * GetInfo() { return "Gemini"; }
};

class Benz :public CAR
{
char * GetInfo() { return "Benz"; }
};


int _tmain(int argc, _TCHAR* argv[])
{
std::list<CAR*> cars;
cars.push_back(new BMW());
cars.push_back(new BMW());
cars.push_back(new Gemini());
cars.push_back(new Benz());

std::list<CAR*>::iterator it = cars.begin();

while(it != cars.end())
{
std::cout << (*it)->GetInfo() << std::endl;
++it;
}

int i;
std::cin >> i;
return 0;
}

but if you don't store pointers in the list, then it does indeed downcast to the base class.

Christian Graus - Microsoft MVP - C++
QuestionGet Status of Comm Port Pin
tj_cts5-Jan-06 9:00
tj_cts5-Jan-06 9:00 
AnswerRe: Get Status of Comm Port Pin
KellyR5-Jan-06 9:12
KellyR5-Jan-06 9:12 
GeneralRe: Get Status of Comm Port Pin
tj_cts5-Jan-06 10:12
tj_cts5-Jan-06 10:12 
GeneralRe: Get Status of Comm Port Pin
Blake Miller5-Jan-06 12:31
Blake Miller5-Jan-06 12:31 
QuestionHow to submit information from an application? Pin
kevincwong5-Jan-06 8:47
kevincwong5-Jan-06 8:47 
AnswerRe: How to submit information from an application? Pin
Office Lineman5-Jan-06 9:19
Office Lineman5-Jan-06 9:19 
GeneralRe: How to submit information from an application? Pin
kevincwong6-Jan-06 11:50
kevincwong6-Jan-06 11:50 
GeneralRe: How to submit information from an application? Pin
Office Lineman6-Jan-06 12:19
Office Lineman6-Jan-06 12:19 
GeneralRe: How to submit information from an application? Pin
kevincwong6-Jan-06 19:15
kevincwong6-Jan-06 19:15 
QuestionDate Time Control Pin
LCI5-Jan-06 8:30
LCI5-Jan-06 8:30 
AnswerRe: Date Time Control Pin
Office Lineman5-Jan-06 8:54
Office Lineman5-Jan-06 8:54 
QuestionCFtpConnection trouble Pin
Anacreon5-Jan-06 7:44
Anacreon5-Jan-06 7:44 
AnswerRe: CFtpConnection trouble Pin
David Crow5-Jan-06 7:55
David Crow5-Jan-06 7:55 
GeneralRe: CFtpConnection trouble Pin
Anacreon5-Jan-06 8:01
Anacreon5-Jan-06 8:01 
GeneralRe: CFtpConnection trouble Pin
David Crow5-Jan-06 8:10
David Crow5-Jan-06 8:10 
AnswerRe: CFtpConnection trouble Pin
Office Lineman5-Jan-06 8:14
Office Lineman5-Jan-06 8:14 
GeneralRe: CFtpConnection trouble Pin
Anacreon5-Jan-06 8:27
Anacreon5-Jan-06 8:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.