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

Recast a member variable in derived class?

Hello,

Is there a way to automatically have a member variable in a derived class
recast within that class. In other words, something like this:

class ABase
{
protected:
BBase *m_something;
public:
ABase(BBase *something);

..etc..
};

class ABaseDerived : public ABase
{
public:
ABaseDerived(BBaseDerived *something)

..etc..
};

I'd like m_something in ABaseDerived to be automatically cast to
BBaseDerived everyplace used in ABaseDerived.

Something like this:

class ABaseDerived : public ABase
{
protected:
reinterpret_cast<BBaseDerived *>(m_something); // this would be cool

public:
ABaseDerived(BBaseDerived *something)

..etc..
};
Is there a way to do that?

Thanks!
Jan 17 '06 #1
1 2469
David wrote:
Hello,

Is there a way to automatically have a member variable in a derived class
recast within that class. In other words, something like this:

class ABase
{
protected:
BBase *m_something;
public:
ABase(BBase *something);

..etc..
};

class ABaseDerived : public ABase
{
public:
ABaseDerived(BBaseDerived *something)

..etc..
};

I'd like m_something in ABaseDerived to be automatically cast to
BBaseDerived everyplace used in ABaseDerived.

Something like this:

class ABaseDerived : public ABase
{
protected:
reinterpret_cast<BBaseDerived *>(m_something); // this would be cool

public:
ABaseDerived(BBaseDerived *something)

..etc..
};
Is there a way to do that?


No. Assuming BBase and BBaseDerived are related by inheritance as the
names imply, you could use dynamic_cast (not reinterpret_cast!), though
it would probably be poor design to do so in such a situation. The best
way to accomplish what you want is likely just to store a pointer to
BBaseDerived in ABaseDerived. Casting from BBase to BBaseDerived is
automatic, but casting the other direction should be avoided if
possible.

Cheers! --M

Jan 17 '06 #2

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

Similar topics

8
by: Ernst Murnleitner | last post by:
Hello Readers, Is there a way that only one class can construct a class A and its inherited classes A2, A3 etc.? I want to construct a class A (and the inherited classes A2, A3 etc.) from a...
3
by: Mark Turney | last post by:
Problem: I have a vector full of two different derived class objects (class B and class C) that are derived from the same base class A. I want to loop through vector and invoke a member function...
1
by: none | last post by:
Hi, I have a base class with a pointer-to-member function variable. Then I have a derived class that needs to use that variable to call a member function (with the same arguments and return value...
5
by: Bob | last post by:
I want to find a way to detect the existance of the private member of a particular type in the derived class from inside the base class itself and call its Dispose() method. Reflection GetFields()...
2
by: Mark Sisson | last post by:
Hi all. SITUATION ================ 1. I have a base class with a member variable that's an object 2. I have several classes that inherit from the base class. 3. There are several methods in...
7
by: Valeriu Catina | last post by:
Hi, consider the Shape class from the FAQ: class Shape{ public: Shape(); virtual ~Shape(); virtual void draw() = 0;
15
by: Bob Johnson | last post by:
I have a base class that must have a member variable populated by, and only by, derived classes. It appears that if I declare the variable as "internal protected" then the base class *can*...
4
by: RSH | last post by:
Hi, I have a situation where I have a class which manages different data connections. In trying to enforce encapsulation I have a member field called connection which could be a type of...
3
by: Goran | last post by:
Hi @ all! Is it possible to overload a member variable? Example: class ClassA_t {
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
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...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.