Click here to Skip to main content
15,915,324 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
Questionscrollbar and slider color Pin
locoone19-Jun-06 17:17
locoone19-Jun-06 17:17 
AnswerRe: scrollbar and slider color Pin
_AnsHUMAN_ 19-Jun-06 17:46
_AnsHUMAN_ 19-Jun-06 17:46 
GeneralRe: scrollbar and slider color [modified] Pin
locoone20-Jun-06 11:57
locoone20-Jun-06 11:57 
AnswerRe: scrollbar and slider color Pin
FarPointer19-Jun-06 17:48
FarPointer19-Jun-06 17:48 
AnswerRe: scrollbar and slider color Pin
Laxman Auti19-Jun-06 18:04
Laxman Auti19-Jun-06 18:04 
QuestionCoding Convention Pin
pjmvn19-Jun-06 17:03
pjmvn19-Jun-06 17:03 
AnswerRe: Coding Convention [modified] Pin
Stephen Hewitt19-Jun-06 17:17
Stephen Hewitt19-Jun-06 17:17 
GeneralRe: Coding Convention [modified] Pin
Stephen Hewitt19-Jun-06 19:27
Stephen Hewitt19-Jun-06 19:27 
Here's some code I developed recently which justifies my previous post and shows one of the ways C-style casts can go wrong. See if you can spot the problem. Hopefully this will educate the people who voted good advice down - the new style casting operators were not invented for fun but to solve real problems. As I said, this is just one example - the C-style cast, while it looks simple, is actually very subtle.
--------------------------------
// 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.h
//

#ifndef __ACTIONS_H__
#define __ACTIONS_H__

class Base1;
class Base2;
class Derived;

const Base2* GetBase2(const Derived *pDerived);

#endif // !__ACTIONS_H__


// Actions.cpp
//

#include "StdAfx.h"
#include "Actions.h"
//#include "Classes.h"

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


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

--------------------------------
As it stands this program will output "Base1" but we wanted "Base2". Needless to say if you use the following version of "Action.cpp" which uses "static_cast" the runtime error becomes a compiler error and your compiler complains instead of your customers.
--------------------------------

// Actions.cpp
//

#include "StdAfx.h"
#include "Actions.h"
//#include "Classes.h"

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



Steve
AnswerRe: Coding Convention [modified] Pin
capricious_00119-Jun-06 17:27
capricious_00119-Jun-06 17:27 
GeneralRe: Coding Convention [modified] Pin
Stephen Hewitt19-Jun-06 17:54
Stephen Hewitt19-Jun-06 17:54 
GeneralRe: Coding Convention Pin
capricious_00119-Jun-06 18:21
capricious_00119-Jun-06 18:21 
GeneralRe: Coding Convention Pin
FarPointer19-Jun-06 18:56
FarPointer19-Jun-06 18:56 
GeneralRe: Coding Convention Pin
Blake Miller20-Jun-06 8:33
Blake Miller20-Jun-06 8:33 
QuestionC++/Non MFC [modified] Pin
Bram van Kampen19-Jun-06 14:20
Bram van Kampen19-Jun-06 14:20 
AnswerRe: C++/Non MFC Pin
Stephen Hewitt19-Jun-06 16:19
Stephen Hewitt19-Jun-06 16:19 
QuestionQueueUserAPC Pin
neilsolent19-Jun-06 9:41
neilsolent19-Jun-06 9:41 
QuestionRe: QueueUserAPC Pin
David Crow19-Jun-06 9:55
David Crow19-Jun-06 9:55 
AnswerRe: QueueUserAPC Pin
neilsolent19-Jun-06 10:32
neilsolent19-Jun-06 10:32 
GeneralRe: QueueUserAPC Pin
David Crow19-Jun-06 10:38
David Crow19-Jun-06 10:38 
GeneralRe: QueueUserAPC Pin
neilsolent19-Jun-06 11:03
neilsolent19-Jun-06 11:03 
GeneralRe: QueueUserAPC Pin
neilsolent19-Jun-06 11:05
neilsolent19-Jun-06 11:05 
QuestionQuestion about Com in Visual C++... Pin
Yanshof19-Jun-06 9:18
Yanshof19-Jun-06 9:18 
AnswerNo one can help me with this question ... ? Pin
Yanshof19-Jun-06 17:36
Yanshof19-Jun-06 17:36 
GeneralRe: No one can help me with this question ... ? Pin
Milton Karimbekallil19-Jun-06 21:27
Milton Karimbekallil19-Jun-06 21:27 
GeneralRe: No one can help me with this question ... ? Pin
Steve S19-Jun-06 21:30
Steve S19-Jun-06 21: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.