Click here to Skip to main content
15,892,927 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: casting stl::vector confusion... [modified] Pin
yeti1114-Jan-07 21:05
yeti1114-Jan-07 21:05 
AnswerRe: casting stl::vector confusion... Pin
Michael Dunn14-Jan-07 22:17
sitebuilderMichael Dunn14-Jan-07 22:17 
GeneralRe: casting stl::vector confusion... [modified] Pin
yeti1114-Jan-07 22:59
yeti1114-Jan-07 22:59 
GeneralRe: casting stl::vector confusion... Pin
El Corazon15-Jan-07 4:59
El Corazon15-Jan-07 4:59 
GeneralRe: casting stl::vector confusion... Pin
yeti1115-Jan-07 11:57
yeti1115-Jan-07 11:57 
GeneralRe: casting stl::vector confusion... Pin
El Corazon15-Jan-07 15:00
El Corazon15-Jan-07 15:00 
GeneralRe: casting stl::vector confusion... Pin
Stephen Hewitt15-Jan-07 12:37
Stephen Hewitt15-Jan-07 12:37 
GeneralRe: casting stl::vector confusion... Pin
Stephen Hewitt15-Jan-07 12:02
Stephen Hewitt15-Jan-07 12:02 
It's not that simple. The following program while perverse compiles and runs just fine and works as expected:

#include <iostream>
#include <vector>
 
class CBase
{
public:
	virtual void Print() { std::cout << "CBase" << std::endl; }
};
 
class CDerived : public CBase
{
public:
	virtual void Print() { std::cout << "CDerived" << std::endl; }
};
 
int main(int argc, char* argv[])
{
	typedef std::vector<CBase*> BVec_t;
	typedef std::vector<CDerived*> DVec_t;
 
	DVec_t dvec;
	dvec.push_back(new CDerived);
	BVec_t &bvec = *reinterpret_cast<BVec_t*>(&dvec); // Don't do this at home...
 
	CBase *pBase = bvec.front();
	pBase->Print();
	delete pBase;
 	
	return 0;
}


Like I said in my previous post; it's possible in some instances but perverse and not necessary. Also it only works because the memory layouts of both classes happen to be the same, an explicit specialisation could change this (like std::vector<bool> ).


Steve

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 
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 

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.