Click here to Skip to main content
15,887,683 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Correct usage of reinterpret_cast and static_cast Pin
John R. Shaw14-May-07 15:12
John R. Shaw14-May-07 15:12 
GeneralRe: Correct usage of reinterpret_cast and static_cast Pin
ComplexLifeForm12-May-07 21:11
ComplexLifeForm12-May-07 21:11 
GeneralRe: Correct usage of reinterpret_cast and static_cast Pin
bob1697213-May-07 2:07
bob1697213-May-07 2:07 
AnswerRe: Correct usage of reinterpret_cast and static_cast Pin
Michael Dunn13-May-07 6:24
sitebuilderMichael Dunn13-May-07 6:24 
GeneralRe: Correct usage of reinterpret_cast and static_cast Pin
Stephen Hewitt13-May-07 14:31
Stephen Hewitt13-May-07 14:31 
GeneralRe: Correct usage of reinterpret_cast and static_cast Pin
bob1697213-May-07 19:41
bob1697213-May-07 19:41 
GeneralRe: Correct usage of reinterpret_cast and static_cast Pin
Stephen Hewitt13-May-07 19:48
Stephen Hewitt13-May-07 19:48 
GeneralRe: Correct usage of reinterpret_cast and static_cast Pin
Stephen Hewitt13-May-07 16:46
Stephen Hewitt13-May-07 16:46 
Here’s a more subtle variation on the same theme as the previous post. Pay particular attention to the "Actions.cpp" file and the comments inside it. I have seen this in real life. Although it seems contrived in a small demo program like this, in the real word it’s not esoteric at all. Real programmers do indeed try to speed up compile times by removing a header and seeing if the program still compiles: in this program, if C-style casts are used, removing a header results in a program that still compiles but behaves incorrectly at runtime. C-style casts, "just say no".

=============================================================
CastProblems.cpp
=============================================================
// CastProblems.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "Classes.h"
#include "Actions.h"

int main(int argc, char* argv[])
{
Derived d;
GetBase2(&d)->Who();

return 0;
}

=============================================================
Actions.cpp
=============================================================
// Actions.cpp
//

#include "StdAfx.h"
#include "Actions.h"

#include "Classes.h"
// If C-style cast is used below:
// - If this header is included the output is "Base2" (correct).
// - If this header is not included the program compiles but the output is "Base1" (incorrect)!!!
// If a 'static_cast' is used below:
// - If this header is included the output is "Base2" (correct).
// - If this header is not included the program doesn't compile.

const Base2* GetBase2(const Derived *pDerived)
{
// return (const Base2*)pDerived;
return static_cast<const Base2*>(pDerived);
}

=============================================================
Actions.h
=============================================================
// Actions.h
//

#ifndef __ACTIONS_H__
#define __ACTIONS_H__

class Base1;
class Base2;
class Derived;

const Base2* GetBase2(const Derived *pDerived);

#endif // !__ACTIONS_H__

=============================================================
Classes.h
=============================================================
// Classes.h
//

#ifndef __CLASSES_H__
#define __CLASSES_H__

#include <iostream>

class RootBase
{
public:
void Who() const
{
using namespace std;
cout << "Base" << m_Num << endl;
}

protected:
RootBase(int num) : m_Num(num) {}

int m_Num;
};

class Base1 : public RootBase
{
public:
Base1() : RootBase(1) {}
};

class Base2 : public RootBase
{
public:
Base2() : RootBase(2) {}
};

class Derived : public Base1, public Base2
{
public:
};

#endif // !__CLASSES_H__



Steve
AnswerRe: Correct usage of reinterpret_cast and static_cast Pin
Stephen Hewitt13-May-07 15:04
Stephen Hewitt13-May-07 15:04 
QuestionVS build/run problem. [modified] Pin
CodeGoose12-May-07 9:01
CodeGoose12-May-07 9:01 
AnswerRe: VS build/run problem. Pin
Hans Dietrich12-May-07 10:55
mentorHans Dietrich12-May-07 10:55 
GeneralRe: VS build/run problem. Pin
CodeGoose12-May-07 13:31
CodeGoose12-May-07 13:31 
AnswerRe: VS build/run problem. Pin
prasad_som12-May-07 19:33
prasad_som12-May-07 19:33 
AnswerRe: VS build/run problem. Pin
Gary R. Wheeler13-May-07 2:37
Gary R. Wheeler13-May-07 2:37 
Questionsimple question Pin
Dj_Lordas12-May-07 8:26
Dj_Lordas12-May-07 8:26 
AnswerRe: simple question Pin
biswajit nayak12-May-07 8:50
biswajit nayak12-May-07 8:50 
AnswerRe: simple question Pin
John R. Shaw12-May-07 11:27
John R. Shaw12-May-07 11:27 
Questionrelated to vc++ and xml Pin
biswajit nayak12-May-07 8:13
biswajit nayak12-May-07 8:13 
AnswerRe: related to vc++ and xml Pin
CPallini12-May-07 8:21
mveCPallini12-May-07 8:21 
AnswerRe: related to vc++ and xml Pin
Hamid_RT12-May-07 8:26
Hamid_RT12-May-07 8:26 
Questionvc++ related question Pin
biswajit nayak12-May-07 7:46
biswajit nayak12-May-07 7:46 
AnswerRe: vc++ related question Pin
CPallini12-May-07 8:10
mveCPallini12-May-07 8:10 
Questionvc++ related question Pin
biswajit nayak12-May-07 7:34
biswajit nayak12-May-07 7:34 
Questionvc++ related question Pin
biswajit nayak12-May-07 7:10
biswajit nayak12-May-07 7:10 
AnswerRe: vc++ related question Pin
Rick York12-May-07 7:20
mveRick York12-May-07 7:20 

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.