Click here to Skip to main content
15,893,594 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: casting stl::vector confusion... Pin
yeti1115-Jan-07 12:16
yeti1115-Jan-07 12:16 
GeneralRe: casting stl::vector confusion... Pin
Stephen Hewitt15-Jan-07 16:00
Stephen Hewitt15-Jan-07 16:00 
GeneralRe: casting stl::vector confusion... [modified] Pin
yeti1115-Jan-07 18:21
yeti1115-Jan-07 18:21 
GeneralRe: casting stl::vector confusion... Pin
Stephen Hewitt15-Jan-07 18:27
Stephen Hewitt15-Jan-07 18:27 
GeneralRe: casting stl::vector confusion... Pin
yeti1115-Jan-07 18:39
yeti1115-Jan-07 18:39 
GeneralRe: casting stl::vector confusion... Pin
Stephen Hewitt15-Jan-07 18:43
Stephen Hewitt15-Jan-07 18:43 
GeneralRe: casting stl::vector confusion... Pin
yeti1115-Jan-07 18:44
yeti1115-Jan-07 18:44 
AnswerRe: casting stl::vector confusion... Pin
John R. Shaw15-Jan-07 4:18
John R. Shaw15-Jan-07 4:18 
I have read your post and the answers to it, before posting mine:

1) Do not do that!
2) Do not even think about doing that!
3) If you have to, then redesign it so that it is not required.

Why don’t you just use a virtual base class and store a pointer to that in the vector?
class A
{
public:
	A() {}
	virtual ~A() { std::cout << "destroying class A" << std::endl; }
	virtual print() const { std::cout << "class A" << std::endl; }
};
class B : public A
{
public:
	B() {}
	virtual ~B() { std::cout << "destroying class B" << std::endl; }
	virtual print() const { std::cout << "class B" << std::endl; }
};

int main(/*int argc, char* argv[]*/)
{
	std::vector<A*> a_vec;
	int i;
	for( i=0; i<3; ++i )
		a_vec.push_back(new B);

	for( i=0; i<3; ++i )
		a_vec[i]->print();

	for( i=0; i<3; ++i )
		delete a_vec[i];

	return 0;
}

Then if you need to access a method that is in a derived class that is not defined in the base class, use dynamic_cast. You can check the pointer, returned by the cast, to insure that it is not null, which would indicate that the cast would be invalid.

I hope that helps.


INTP
"Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

GeneralRe: casting stl::vector confusion... Pin
yeti1115-Jan-07 12:09
yeti1115-Jan-07 12:09 
GeneralRe: casting stl::vector confusion... Pin
John R. Shaw19-Jan-07 16:23
John R. Shaw19-Jan-07 16:23 
QuestionA Qestion on thread Pin
sunshine jeffrey14-Jan-07 15:35
sunshine jeffrey14-Jan-07 15:35 
AnswerRe: A Qestion on thread Pin
Stephen Hewitt14-Jan-07 15:43
Stephen Hewitt14-Jan-07 15:43 
GeneralRe: A Qestion on thread [modified] Pin
sunshine jeffrey14-Jan-07 16:29
sunshine jeffrey14-Jan-07 16:29 
GeneralRe: A Qestion on thread Pin
Stephen Hewitt14-Jan-07 16:49
Stephen Hewitt14-Jan-07 16:49 
GeneralRe: A Qestion on thread Pin
sunshine jeffrey14-Jan-07 16:52
sunshine jeffrey14-Jan-07 16:52 
GeneralRe: A Qestion on thread Pin
Stephen Hewitt14-Jan-07 16:56
Stephen Hewitt14-Jan-07 16:56 
AnswerRe: A Qestion on thread Pin
Roger Stoltz14-Jan-07 20:30
Roger Stoltz14-Jan-07 20:30 
AnswerRe: A Qestion on thread Pin
Cristian Amarie14-Jan-07 23:35
Cristian Amarie14-Jan-07 23:35 
AnswerRe: A Qestion on thread Pin
David Crow15-Jan-07 3:44
David Crow15-Jan-07 3:44 
QuestionI don't know How to build Pin
rockfanskid14-Jan-07 15:34
rockfanskid14-Jan-07 15:34 
AnswerRe: I don't know How to build Pin
Nibu babu thomas14-Jan-07 17:42
Nibu babu thomas14-Jan-07 17:42 
QuestionRe: I don't know How to build Pin
David Crow15-Jan-07 3:46
David Crow15-Jan-07 3:46 
QuestionTimer + Network Programming = problem Pin
Sean Cleary14-Jan-07 13:07
Sean Cleary14-Jan-07 13:07 
AnswerRe: Timer + Network Programming = problem [modified] Pin
bob1697214-Jan-07 13:50
bob1697214-Jan-07 13:50 
AnswerRe: Timer + Network Programming = problem Pin
Mark Salsbery14-Jan-07 13:57
Mark Salsbery14-Jan-07 13:57 

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.