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

Can someone explain this code?

Wondering how this code worked

I have a base class called eg MyBase. It has a private member called
myprivatemember which is of course not accessible from users of the
class.

But I suddenly need to update the variable myprivate and I am not
allowed to edit the original source code so I create a new class,
MyNewToAllowChange which implements a public function called
SetMyPrivate which allows me to change the variable.

I realise this is bad design etc but just bear with me.

I don't implement operator= (or assign) and in my code I have an
instance of the base object - MyBase.

I need to update the myprivatemember and so the following functions
have been provided which make it possible.

inline MyNewToAllowChange* Mt(MyBase* thebase)
{
return static_cast<MyNewToAllowChange*>(thebase);
}

I can then do this sort of thing:

(if my base object is called mybaseobj (a pointer))
Mt(mybaseobj)->SetMyPrivate(whatever);

MyBase
MyNewToAllowChange

How is this working? Is it a fairly standard technique?

I can see that it is useful in certain circumstances, just not see
before.
Jun 27 '08 #1
3 1138
Angus wrote:
How is this working? Is it a fairly standard technique?
As far as I can tell...

You've not provided enough information to answer that question. It's a
mystery.
Jun 27 '08 #2
On 2008-06-23 17:47, Angus wrote:
Wondering how this code worked

I have a base class called eg MyBase. It has a private member called
myprivatemember which is of course not accessible from users of the
class.

But I suddenly need to update the variable myprivate and I am not
allowed to edit the original source code so I create a new class,
MyNewToAllowChange which implements a public function called
SetMyPrivate which allows me to change the variable.

I realise this is bad design etc but just bear with me.

I don't implement operator= (or assign) and in my code I have an
instance of the base object - MyBase.

I need to update the myprivatemember and so the following functions
have been provided which make it possible.

inline MyNewToAllowChange* Mt(MyBase* thebase)
{
return static_cast<MyNewToAllowChange*>(thebase);
}

I can then do this sort of thing:

(if my base object is called mybaseobj (a pointer))
Mt(mybaseobj)->SetMyPrivate(whatever);

MyBase
MyNewToAllowChange

How is this working?
By praying to the gods, sacrificing virgins at full moon, and a few
other tricks.

At the very least you should used reinterpret_cast since static_cast is
not allowed to do that conversion, and even if you did the result of the
conversion is implementation defined. In short, while it might work for
you now, if you try it on another platform and/or with another compiler
you might be in for a surprise.

--
Erik Wikström
Jun 27 '08 #3
Angus wrote:
...
I have a base class called eg MyBase. It has a private member called
myprivatemember which is of course not accessible from users of the
class.

But I suddenly need to update the variable myprivate and I am not
allowed to edit the original source code so I create a new class,
MyNewToAllowChange which implements a public function called
SetMyPrivate which allows me to change the variable.
Er... How??? If the member variable is indeed private and the class has
no "friends", there's no way to gain access to it, regardless of how
many "new" classes you introduce.
How is this working? Is it a fairly standard technique?
Sorry, so far you haven't demonstrated any technique at all. Explain
more clearly what "technique" exactly you are talking about.

--
Best regards,
Andrey Tarasevich
Jun 27 '08 #4

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

Similar topics

11
by: Maciej Nadolski | last post by:
Hi! I can`t understand what php wants from me:( So: Cannot send session cache limiter - headers already sent (output started at /home/krecik/public_html/silnik.php:208) in...
21
by: Gactimus | last post by:
Can anyone explain what the lines with the '*' by them do? ----------- #ifndef _COUNTER_H #define _COUNTER_H #include <iostream> using namespace std; class Counter
3
by: MarcJessome | last post by:
Hi, I was wondering if someone could help me through learning C++. I've tried learning before, but I find I would work better if I had someone that could help explain a few things to me. Im using...
5
by: J Allen Horner | last post by:
I'm just starting to learn C, and the tutorial I'm reading uses this code as an example: ----------- #include <stdio.h> #define MAX 10 int a; int rand_seed=10;
1
by: Tran Hong Quang | last post by:
Hi, In header file, I see this declaration: #define DECLARE_VTBL(iname) iname vt##iname; then, in C file, there are below codes: typedef struct _Abc { .......... } abc;
5
markmcgookin
by: markmcgookin | last post by:
Hi Folks, this may seem very lazy, but can someone exaplin this bit of code for me? regID int IDENTITY(0,1) It is part of an SQL CE command in a VB app, but I just don't get what the regID...
1
by: td0g03 | last post by:
Hello, I am new to C and I am new to English. I not sure what palindromes mean. I don't know exactly what my teacher wants me to do. If someone could explain it to me in a different way that would be...
1
by: mansa | last post by:
a) A company receives hundreds of orders every week through the mail. Explain carefully how BATCH- PROCESSING might be used. Describe the role of the BATCH TOTAL in this process. Explain how batch...
6
by: Dave Young | last post by:
I'm looking at some code that i've inherited and I'm not really familar with what's going on here and was hoping somone could explain it to me. For reference: f1 is a long f2 is a long ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.