473,394 Members | 1,893 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,394 software developers and data experts.

Nice trick or illegal construct?


As you can see in this example the coder did't think it is necessary
to have an instance of a Derived to be able to set Base member prot.
Is it legal?

class Base
{
protected:

int prot;
};

class Derived : public Base
{
public:

using Base::prot;
};

int main()
{
Base base;
//instance of derived not needed to access protected member?
static_cast<Derived&>(base).prot = 1;
return 0;
}

Robert Milharcic
Jul 23 '05 #1
4 1116
"rmilh" <rm***@mailaddr.com> wrote in message
news:uq********************************@4ax.com...
As you can see in this example the coder did't think it is necessary
to have an instance of a Derived to be able to set Base member prot.
Is it legal? .... Base base;
//instance of derived not needed to access protected member?
static_cast<Derived&>(base).prot = 1;


Not a good idea.
Formally, casting a base class instance as a derived type triggers
undefined behavior (even though the above code is likely to work
"as expected" on many platforms).
Cheers, Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com
Jul 23 '05 #2
rmilh wrote:
static_cast<Derived&>(base).prot = 1;


dynamic_cast is more appropriate for classes
Jul 23 '05 #3
Hello!
Is it legal?

class Base *snip*
class Derived : public Base *snip*
int main()
{
Base base;
//instance of derived not needed to access protected member?
static_cast<Derived&>(base).prot = 1;
return 0;
}


This only works because Base starts at the "begining" of Derived. If you
had:
class Derived: public A, public Base, public C
{
...
}

Then I think it would not work.

Andre Caldas.
Jul 23 '05 #4
On 2005-04-09 02:22:37 -0400, rmilh <rm***@mailaddr.com> said:

As you can see in this example the coder did't think it is necessary
to have an instance of a Derived to be able to set Base member prot.
Is it legal?
No.
class Base;
class Derived : public Base; Base base;
static_cast<Derived&>(base)


Your cast is undefined behavior.

--
Clark S. Cox, III
cl*******@gmail.com

Jul 23 '05 #5

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

Similar topics

6
by: aurora | last post by:
I find that I use some list unpacking construct very often: name, value = s.split('=',1) So that 'a=1' unpack as name='a' and value='1' and 'a=b=c' unpack as name='a' and value='b=c'. The...
10
by: Mac | last post by:
Is there a way to mimic the behaviour of C/C++'s preprocessor for macros? The problem: a lot of code like this: def foo(): # .... do some stuff if debug: emit_dbg_obj(DbgObjFoo(a,b,c)) #...
134
by: Joseph Garvin | last post by:
As someone who learned C first, when I came to Python everytime I read about a new feature it was like, "Whoa! I can do that?!" Slicing, dir(), getattr/setattr, the % operator, all of this was very...
17
by: Jacek Dziedzic | last post by:
Hello! I have a templated class that serves as a simple vector of elements. template <typename T> class simple_vector : public math_object { // ... lots of simple_vector operations // the...
14
by: deko | last post by:
Is there a way to check user input for illegal characters? For example, a user enters something into a text box and clicks OK. At that point I'd like to run code such as this: illegal =...
14
by: BGreene | last post by:
I was not going to post this but my someone thought it was funny, Write the shortest c statement that will "exchange" two unsigned ints. in place.
9
by: John A Grandy | last post by:
In VB6 you could get away with the following code: Dim Index As Integer Dim ItemsCount As Integer Dim StringArray() As String Dim StringValue As String '....
18
by: Dave Booker | last post by:
I have a Thread making a callback to a Form function that alters Form data. Of course, the debugger is telling me, "Cross-thread operation not valid: Control 'FormTest' accessed from a thread other...
1
by: Manish | last post by:
The code is as ... $folderlistfile = $path."xmlfile.xml"; /* <list> <details id="2"> <name>Books</name>
2
by: Kamaria | last post by:
I wrote this program to calculate the income tax of a user depending on whether or not they were married: import javax.swing.JOptionPane; public class Income_Tax { public static void main...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.