473,385 Members | 1,531 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Wrong usage of static_cast: is output undefined or predicted?


Here is some program in which static_cast is (wrongly) used instead of dynamic_cast.

Is output of the program undefined or predicted?

=========== C++ code : foo.cpp : BEGIN ===========
#include <iostream>
using namespace std;

struct B
{
virtual void foo1 () { cout << "B::foo1" << endl; }
virtual void foo2 () { cout << "B::foo2" << endl; }
};
struct D1 : public B
{
void foo1 () { cout << "D1::foo1" << endl; }
};
struct D2 : public B
{
void foo2 () { cout << "D2::foo2" << endl; }
};

int main ()
{
B* pB = new D1;
// --------------------------
// Of course, here we should use dynamic_cast
D2* pD2 = static_cast<D2*> (pB);
// --------------------------

pD2->foo1();
pD2->foo2();

return 0;
}

=========== C++ code : foo.cpp : END =============

=========== Compilation & Run : BEGIN =============

$ g++ --version
g++ (GCC) 3.3.3 (cygwin special)
[---omitted---]

$ g++ -W -Wall foo.cpp
// No errors, no warnings

$ a
D1::foo1
B::foo2
=========== Compilation & Run : END ===============


Jul 22 '05 #1
1 1333
* Alex Vinokur:

Here is some program in which static_cast is (wrongly) used instead of dynamic_cast.

Is output of the program undefined or predicted?
UB.

The code will probably work on most compilers since the derived classes
do not add anything to or otherwise change the object memory layout.

// --------------------------
// Of course, here we should use dynamic_cast


Just say no to casting.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Gary Labowitz | last post by:
Am I doing this correctly? It is a sample program for my class. #include <iostream> using namespace std; int main( ) { int x=3, y=4;
26
by: Steven T. Hatton | last post by:
The code shown below is an example from the Coin3D documentation. I believe the use of the C-style cast is safe under the circumstances, but from what I've been exposed to (TC++PL(SE)), I would...
11
by: ma740988 | last post by:
I'm perusing a slide with roughly 12 bullets spread across 3 pages. Each bullet reflects 'advice'. I'm ok with all but 1 bullet, more specifically the bullet that states: " Avoid the STL unless...
28
by: wwj | last post by:
void main() { char* p="Hello"; printf("%s",p); *p='w'; printf("%s",p); }
6
by: Dave Rahardja | last post by:
Is it safe to use static_cast to downcast from a virtual base class? For example, class V {}; class A: public virtual V {}; class B: public virtual V {}; class C: public A, public B; void...
5
by: Jim Langston | last post by:
Expected output of this program is: 1 -1 -1 -1 Using Microsoft Visual C++ .net 2003 actual output is: 1 4294967295 4294967295
83
by: Anonymous | last post by:
Came across some code summarized as follows: char const* MyClass::errToText(int err) const { switch (err) { case 0: return "No error"; case 1: return "Not enough"; case 2: return "Too...
9
by: arundelo | last post by:
Is there a way to tell whether accessing a variable will result in an "Undefined variable" E_NOTICE? isset() almost does this, but it also returns false if a variable is set to null: ...
3
by: Rahul | last post by:
Hi, Everywhere I read that static_cast<only work fine for the conversion which are implicitly allowed by the compiler hence the following does not work int *i; double *d; d = i; ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.