Click here to Skip to main content
15,886,919 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: C++ style casting Pin
Nemanja Trifunovic17-Jul-06 5:35
Nemanja Trifunovic17-Jul-06 5:35 
GeneralRe: C++ style casting Pin
Rob Caldecott17-Jul-06 6:09
Rob Caldecott17-Jul-06 6:09 
GeneralRe: C++ style casting [modified] Pin
ricecake17-Jul-06 11:15
ricecake17-Jul-06 11:15 
GeneralRe: C++ style casting Pin
David Crow17-Jul-06 6:54
David Crow17-Jul-06 6:54 
GeneralRe: C++ style casting Pin
David Crow18-Jul-06 2:48
David Crow18-Jul-06 2:48 
GeneralRe: C++ style casting Pin
Stephen Hewitt17-Jul-06 14:15
Stephen Hewitt17-Jul-06 14:15 
GeneralRe: C++ style casting Pin
David Crow18-Jul-06 2:46
David Crow18-Jul-06 2:46 
GeneralRe: C++ style casting Pin
Stephen Hewitt18-Jul-06 3:58
Stephen Hewitt18-Jul-06 3:58 
C style casts are anything but clear. While C-style casts look simple they are actually very complicated and subtle. In small programs this often goes unnoticed. But as the program gets larger real problems can and do occur. Try this example program. I know it's on the longish side but it's typical of real applications and may suprise you.

The output with the #include is:
Base2

The output without #include is:
Base1

Not many C++ programmers will be able to spot the problem.

--------------------

// Casts.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include "Functions.h"

// C-style casts suck. The program will compile with or without the line below using C-style casts.
// With it all is OK, without it the program misbehaves at runtime. If 'static_cast' is used the
// program not compile without the #include and you don't get any nasty suprises.
#include "Classes.h"

int main(int argc, char* argv[])
{
Derived *pDerived = MakeDerived();
DoStuff((Base2*)pDerived);
//DoStuff(static_cast<Base2*>(pDerived));
KillDerived(pDerived);

return 0;
}

\/\/\/\/\/\/

// Classes.h

#ifndef __CLASSES_H__
#define __CLASSES_H__

class Base1
{
public:
Base1();
void Print() const;

private:
const char *m_pName;
};

class Base2
{
public:
Base2();
void Print() const;

private:
const char *m_pName;
};

class Derived : public Base1, public Base2
{
};

#endif // !__CLASSES_H__

\/\/\/\/\/\/

// Functions.h

#ifndef __FUNCTIONS_H__
#define __FUNCTIONS_H__

class Base1;
class Base2;
class Derived;

Derived* MakeDerived();
void KillDerived(Derived *pDerived);
void DoStuff(Base2 *pB2);

#endif // !__FUNCTIONS_H__

\/\/\/\/\/\/

// Implementation.cpp

#include "stdafx.h"
#include "Functions.h"
#include "Classes.h"
#include <iostream>

Base1::Base1()
{
m_pName = "Base1";
}

void Base1::Print() const
{
using namespace std;;
cout << m_pName << endl;
}

Base2::Base2()
{
m_pName = "Base2";
}

void Base2::Print() const
{
using namespace std;;
cout << m_pName << endl;
}

Derived* MakeDerived()
{
return new Derived;
}

void KillDerived(Derived *pDerived)
{
delete pDerived;
}

void DoStuff(Base2 *pB2)
{
pB2->Print();
}



Steve
GeneralRe: C++ style casting Pin
Maximilien17-Jul-06 5:36
Maximilien17-Jul-06 5:36 
GeneralRe: C++ style casting Pin
toxcct17-Jul-06 5:37
toxcct17-Jul-06 5:37 
GeneralRe: C++ style casting Pin
Maximilien17-Jul-06 6:31
Maximilien17-Jul-06 6:31 
GeneralRe: C++ style casting Pin
David Crow17-Jul-06 6:51
David Crow17-Jul-06 6:51 
GeneralRe: C++ style casting Pin
Jörgen Sigvardsson17-Jul-06 9:03
Jörgen Sigvardsson17-Jul-06 9:03 
GeneralRe: C++ style casting Pin
Zac Howland17-Jul-06 9:49
Zac Howland17-Jul-06 9:49 
GeneralRe: C++ style casting Pin
Stephen Hewitt17-Jul-06 14:16
Stephen Hewitt17-Jul-06 14:16 
GeneralRe: C++ style casting Pin
Zac Howland17-Jul-06 16:06
Zac Howland17-Jul-06 16:06 
GeneralRe: C++ style casting [modified] Pin
Jörgen Sigvardsson17-Jul-06 15:13
Jörgen Sigvardsson17-Jul-06 15:13 
GeneralRe: C++ style casting Pin
Zac Howland17-Jul-06 16:12
Zac Howland17-Jul-06 16:12 
GeneralRe: C++ style casting Pin
Zac Howland17-Jul-06 9:46
Zac Howland17-Jul-06 9:46 
QuestionBackground color Pin
goodoljosh198017-Jul-06 5:06
goodoljosh198017-Jul-06 5:06 
AnswerRe: Background color Pin
SimonSays17-Jul-06 6:05
SimonSays17-Jul-06 6:05 
GeneralRe: Background color Pin
goodoljosh198017-Jul-06 9:14
goodoljosh198017-Jul-06 9:14 
AnswerRe: Background color Pin
Hamid_RT17-Jul-06 18:14
Hamid_RT17-Jul-06 18:14 
QuestionExporting function objects from DLL Pin
napoleaninlondon17-Jul-06 4:52
napoleaninlondon17-Jul-06 4:52 
AnswerRe: Exporting function objects from DLL Pin
Cedric Moonen17-Jul-06 4:57
Cedric Moonen17-Jul-06 4: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.