473,492 Members | 4,279 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Casting to base class

Hey guys, quick question about how C++ designers chose to not implement
this, I'm sure there are some people here who can take a better guess at it
than I.

So, in C++, the only way to prevent typecasting to a base class is by having
private inheritance (correct?). However, this prevents one from doing 2
very important things:

Example #1: Allow conversion from child -> grandparent, but NOT child ->
parent.

Say we have a class structure such as this:
class AbstractionLayer
{};
class FunctionalityLayer : protectected AbstractionLayer
{};
template <typename _T>
class TypesafeLayer : private FunctionalityLayer
{};

We want to be able to cast to AbstractionLayer, but not to
FunctionalityLayer (this is an internal class which has funcitonality, but
we don't want usage of this class directly). However, we do want to
typecast to an AbstractionLayer.

Take, for example, a memory allocator. AbstractionLayer provides simple
allocation of memory, returning simply allocated memory and freeing
allocated memory. FunctionalityLayer provides the actual allocation of
memory: we could have, say, 2 types, a static allocator (allocates one big
chunk and asserts if we try to insert memory when it's full), and a growing
allocator (creates a linked list of memory chunks). Lastly, TypesafeLayer
would have 2 functions which are typesafe, and would do the appropriate
new() and ~ calls after allocating/deallocating memory. However, in the
example above, we are not allowed to typecast to AbstractionLayer.

Example #2: Limit typecasting but allow function usage

Say we have something similar to the above, i.e. a class with functionality.
We want these functions to be externally accessible to others, HOWEVER, we
do NOT want it to be typecasted to the base class. Sure we can do "using"
for all the functions, but this becomes a hassle if we add functionality, as
we have to modify both classes. Lastly, in a case such as this, we are left
with no options:

class FunctionalityLayer1 : protectected AbstractionLayer
{};
class FunctionalityLayer2 : protectected AbstractionLayer
{};
template <typename _FuncLayer>
class TypesafeLayer : private _FuncLayer
{};

Anyone know if they're planning to add support to overriding the "conversion
to a reference to a base class operator", or why it hasn't been done yet?
The above examples are shown below with this operator, if C++ were to
support this (it does not):

Example #1:
class AbstractionLayer
{};
class FunctionalityLayer : protectected AbstractionLayer
{};
template <typename _T>
class TypesafeLayer : private FunctionalityLayer
{
public:
operator AbstractionLayer& () { return *this; }
};

Example #2:
class FunctionalityLayer1 : protectected AbstractionLayer
{};
class FunctionalityLayer2 : protectected AbstractionLayer
{};
template <typename _FuncLayer>
class TypesafeLayer : private _FuncLayer
{
private:
operator _FuncLayer& () { return *this; }
};

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #1
1 2258
Marcos Boyington wrote:
Hey guys, quick question about how C++ designers chose to not implement
this, I'm sure there are some people here who can take a better guess at it
than I.

So, in C++, the only way to prevent typecasting to a base class is by having
private
and protected
inheritance (correct?). However, this prevents one from doing 2
very important things:

Example #1: Allow conversion from child -> grandparent, but NOT child ->
parent.

Say we have a class structure such as this:
class AbstractionLayer
{};
class FunctionalityLayer : protectected AbstractionLayer
{};
template <typename _T>
class TypesafeLayer : private FunctionalityLayer
{};

We want to be able to cast to AbstractionLayer, but not to
FunctionalityLayer (this is an internal class which has funcitonality, but
we don't want usage of this class directly). However, we do want to
typecast to an AbstractionLayer.
template <class T>
class TypesafeLayer : public AbstractionLayer,
private FunctionalityLayer
{
};
Example #2: Limit typecasting but allow function usage

Say we have something similar to the above, i.e. a class with functionality.
We want these functions to be externally accessible to others, HOWEVER, we
do NOT want it to be typecasted to the base class. Sure we can do "using"
for all the functions, but this becomes a hassle if we add functionality, as
we have to modify both classes.
Either A is a B or A is not a B. You cannot have both so you're stuck
with using declarations or function forwarding. Your design seems to be
flawed somehow.
Lastly, in a case such as this, we are left
with no options:

class FunctionalityLayer1 : protectected AbstractionLayer
{};
class FunctionalityLayer2 : protectected AbstractionLayer
{};
template <typename _FuncLayer>
class TypesafeLayer : private _FuncLayer
{};
That's too bad, and that's another indication that your design is
impraticable.
Anyone know if they're planning to add support to overriding the "conversion
to a reference to a base class operator", or why it hasn't been done yet?
The above examples are shown below with this operator, if C++ were to
support this (it does not):

Example #1:
class AbstractionLayer
{};
class FunctionalityLayer : protectected AbstractionLayer
{};
template <typename _T>
class TypesafeLayer : private FunctionalityLayer
{
public:
operator AbstractionLayer& () { return *this; }
};

Example #2:
class FunctionalityLayer1 : protectected AbstractionLayer
{};
class FunctionalityLayer2 : protectected AbstractionLayer
{};
template <typename _FuncLayer>
class TypesafeLayer : private _FuncLayer
{
private:
operator _FuncLayer& () { return *this; }
};


These should be illegal: TypesafeLayer is neither a FunctionalityLayer
nor a _FuncLayer, it is implemented in terms of these. It is a *good*
thing such construct is disallowed.

The whole thing seems to be caused my a misunderstanding/misuse of
public inheritance.
Jonathan

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #2

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

Similar topics

5
2263
by: Vinodh Kumar | last post by:
I see that casting changes the value of a pointer in case of multiple inheritance.In single inheritance also it is the same know?Isn't it? Vinodh Kumar P
3
1668
by: Kurt | last post by:
i just can't figure out why something im doing is not working correctly.... public interface IInterface { int someProperty { get; set; }
0
1299
by: Kurt Lange | last post by:
no... the array is created dynamically. and no... that defeats the purpose of what im trying todo.. encapsulate all initializing of variables in base class... derive from it... by deriving...
2
2547
by: Zac | last post by:
Alright anyone who has 2c throw it in... I am working through a custom xml serializer and have come upon a conundrum, given our class design. The interface implemented on the base class (base...
23
3474
by: René Nordby | last post by:
Hi there, Is there anyone that knows how to do the following? I have a class A and a class B, that 100% inherits from class A (this means that I don't have other code in class B, than...
10
1723
by: Brett Romero | last post by:
Say I have a class inheriting some base class: BaseClass { void Foo() { Update(); } }
5
2987
by: mijobee | last post by:
Hello Everyone, I just wanted to check that I'm using dynamic_cast correctly. I have a hierarchy of objects using pure virtual classes and virtual inheritance to implement interfaces. I ran...
9
2443
by: Jess | last post by:
Hello, It seems both static_cast and dynamic_cast can cast a base class pointer/reference to a derived class pointer/reference. If so, is there any difference between them? In addition, if I...
9
2399
by: Naomi | last post by:
I need to make software engineering decision to do with using a derived data type in a container class. So for example, if I have an Edge class, and I want to make a Edge object which contains two...
9
3450
by: Taras_96 | last post by:
Hi everyone, I was experimenting with static_cast and reinterpret cast #include <iostream> struct A1 { int a; }; struct A2 { double d; }; struct B : public A1, A2
0
7118
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
6980
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
7192
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...
1
6862
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...
1
4886
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...
0
4579
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3087
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
637
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
282
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.