|
It's as safe is it can reasonably be without runtime overhead. There is a relationship between the two classes (some BaseClass es are DerivedClass es) so the cast will succeed; if there is no relationship (no BaseClass es are DerivedClass es) then it fails. In the general case there is no way a compiler can be expected to detect if a particular base class instance is related to a particular derived class (they simple aren't smart enough and separate compilation complicates this even further). If you want complete safety you’ll have to pay a price at runtime and use dynamic_cast .
No one's suggesting that using static_cast will solve all your problems, just that it’s safer than a C-style cast.
Your point is taken however.
Steve
|
|
|
|
|
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
|
|
|
|
|
Use static_cast ; reinterpret_cast is the wrong choice in this context.
Steve
|
|
|
|
|
I'm having the following problem.
When I try to run the release version of a VC project, instead of running the program it opens up another instance of visual studio. And that's it. Doesn't run the program.
Debug works fine.
I'm not sure how this occured, but I must have accidentally done something in the project options? or configuration?
Any thoughts on what would cause this? Or how to fix it?
Thanks.
|
|
|
|
|
Create another project, see if the same thing happens. If it does, it's VS. If it doesn't, it's the project.
|
|
|
|
|
I checked out your suggestion, and it works as it should for another project.
Still not sure what the setting or problem could be though. Not sure how to word my google searches to find more info and I haven't seen anything in the project setting that sheds any light on it.
One strange thing is that this is a team project, and my coworkers are not experiencing the same issue, even though we have synced project files.
|
|
|
|
|
CodeGoose wrote: instead of running the program it opens up another instance of visual studio. And that's it. Doesn't run the program.
This means, program is crashing in release version. And there could be various resons for that.
There is an article on site ,"surviving release version". Refer it, to deal the problem.
Prasad
MS MVP - VC++
|
|
|
|
|
What happens when you run the release application from Windows Explorer?
Software Zen: delete this;
|
|
|
|
|
Hello,
sorry for my bad english, is polymorphic function called overloaded? are these terms the same?
and let's say we have code like this:
class A{
int a;
};
class B:public A{
int b;
};
class C:public B{
int c;
};
i am beginner in c++. i want to write an example of polymorphic function in this classes inherit case. Could you advice how should it look like? what is the main point of polymorphic function in classes inherit?
-- modified at 14:32 Saturday 12th May, 2007
Dj_Lord
|
|
|
|
|
i think polymerphic having many form
lets example of function overloading
fun() is a function which can have diffrent signature
fun(int );
fun(int , int );
fun(float);
signature means only no of argument and type of argument not return type;
here name mangling comes to feature which is a good feature in c++;
class a{
public:
a(){
}
a(int var){
}
a(a obj){
}
};
a is a constructor which take diffrent no of arg which is an good example of function overloading.
but constructor can't be inherited
if class a has a function name fun() and another class b derived from base a and derived also has fun() function there is ambigity occur so there is problem which can be overcome by virtual function which is a pure object oriented and from which object oriented start in c++.
biswajit nayak
|
|
|
|
|
Basically you are dealing with inheritance and you need the base class to call a member function in a derived class. What that means: if you convert a pointer to a derived class to that of the base class and call a virtual function that is defined in both classes, the one in the derived class will be called.
class A { virtual const void printName() { cout << "class A" << endl; }
class B: public A { virtual void char printName() { cout << "class B" << endl; }
class C: public B { virtual void char printName() { cout << "class C" << endl; }
C c;
A* pa = dynamic_cast<A*>&c;
pa->printName();
INTP
"Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra
|
|
|
|
|
<emp>
<name>white</name>
<adress>amsterdam</adress>
<company>avaya tenovis</company>
</emp>
coding in vc++ but looks like above
biswajit nayak
trainee engineer
|
|
|
|
|
OK. This is a good representation for an XML document. BTW the question is?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
I dont know whats your purpose of these messages but do you need to TinyXML[^]
|
|
|
|
|
hi all
sorry i think no body geting my question
my question is there is a root tag called employee
sub tags are his name , adreess , id or some more features
which looks like xml format but i can't write it here as it will be not displaying this format so that's why i think no body geting my question
coding should be in vc++
plz don't mind it as i am a new to this code project
biswajit nayak
|
|
|
|
|
Ok, Let's suppose that you have a XML document. What do you want to do using C++?
(and please note that you can use HTML escape codes to post your document here).
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
emp
name
adress
emp is the root tag
name and adress are the sub tag
coding in vc++ but looks like xml format
biswajit nayak
|
|
|
|
|
<emp>
<name>biswa
<adress>abc
code in vc++ looks like above
biswajit nayak
|
|
|
|
|
Do you have a question to ask ? If not, spam in the forum is not welcome.
|
|
|
|
|
sorry,
i think u didn't see my question
my question is coding in vc++ but looks like this
<emp>
<name>abc
<adress>marathahali
biswajit nayak
|
|
|
|
|
That doesn't like coding in any lanquage that I recognize and I still don't see a question.
WHAT IS YOUR QUESTION ?
|
|
|
|
|
Ah.. I see you haven't received the new Code Project mind reading unit yet. Don't worry, you'll have it soon!
--
Raaaaaaaaaaaaaaaaaaaaa!
|
|
|
|
|
Joergen Sigvardsson wrote: Ah.. I see you haven't received the new Code Project mind reading unit yet.
ummm...is this something one has to order, or should I receive mine automatically?
Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
|
|
|
|
|
Think and you shall receive!
--
Raaaaaaaaaaaaaaaaaaaaa!
|
|
|
|
|
If I had my Code Project mind reading unit I would have known that!
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
|
|
|
|