473,890 Members | 1,408 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Casting a base class to a derived class

This question kind of follows on from Mike Spass’ posting 10/11/2004; I don’t
understand why you can’t declare an implicit operator to convert a base class
to a derived class.

The text books say “neither the source nor the target types of a conversion
can be a base type of the other, since a conversion would then already
exist”. But this is not really true, whilst automatic (implicit) conversions
do occur from the derived class to the base class, they do not go the other
way because there is no way of knowing what items are in the derived class
that are not inherited from the base.

Each time I look at this I keep coming back to the same question: why can’t
I write an operator that states what to do with the other items?

Nov 16 '05 #1
1 8364
If you have a module that uses a type, we say that the module is "coupled"
to that type. It knows of the existence of the type, and any changes in the
type can potentially affect it.

We want to reduce coupling. That is one of the reasons for using base types
in the first place. In my mind, I can think of no better reason.

So, if a module 'has' an object of base type, and 'needs' an object of
derived type, it is already coupled to the derived type. Otherwise, it
wouldn't 'need' it.

Defining a construct that converts from the base type to the derived type,
in the base class, means that your base class has to either create a
conversion strategy that all derived classes will follow, or it has to be
coupled to each derived type. The latter is not feasable. The former is
possible. In fact, you could implement it in code if you want to.

I would suggest that it would be better to consider alternatives before you
go down that road. You aren't addressing the problem of coupling... you are
working around a 'limitation' that is more like a guard rail. Some
limitations are good for us all.

So, let's look at the problem, and consider alternatives.

Let's say that you have a SQL Server connection, and you pass it to a method
that understands generic database connections, and that method calls another
object that needs to use one of the SQL-specific properties...

I'd say that you need to use strategy patterns in combination with factory
methods. If the generic class gets its caller as a parameter, and the
caller provides the custom methods that the generic class requires, then the
generic method never needs to know the to specific type. Similarly, your
calling code could create a concrete class that performs specific activities
on the "connection " object that the generic code needs performed. The
calling class could pass in one of the "adapter" objects for the generic
code to use. (This pattern is used in the .Net framework).

Alternatively, your calling class and the "one step removed" dependent class
can be tied together using an abstract factory and a singleton. In this
case, your generic class doesn't need to cast anything or even be aware of
anything, because the generic object would turn to the factory to generate
the object to call, and that object would already EXPECT and GET a SQL
Server connection.

In other words, if you examine where you are creating coupling between your
classes, you can nearly always clear up these ambiguities by controlling
object creation more carefully.

I hope this helps,

--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Mark McDonald" <Ma**********@d iscussions.micr osoft.com> wrote in message
news:0A******** *************** ***********@mic rosoft.com...
This question kind of follows on from Mike Spass' posting 10/11/2004; I don't understand why you can't declare an implicit operator to convert a base class to a derived class.

The text books say "neither the source nor the target types of a conversion can be a base type of the other, since a conversion would then already
exist". But this is not really true, whilst automatic (implicit) conversions do occur from the derived class to the base class, they do not go the other way because there is no way of knowing what items are in the derived class
that are not inherited from the base.

Each time I look at this I keep coming back to the same question: why can 't I write an operator that states what to do with the other items?

Nov 16 '05 #2

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

Similar topics

5
2291
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
7
1463
by: Jakob Bieling | last post by:
Hi, I have a question about casting and using the casted pointer: Suppose I have a 'base' class and a 'derived' class (which is derived from 'base'). Now I have this scenario: base* p1 = new base (); base* p2 = new derived (); derived* d1 = (derived*) p1;
3
1690
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
1321
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 from base class, and casting, derived classes would already have their variables initialiezed(cause they have already been initialized in the base class) ....
23
3551
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 the Inherit statement).
10
1754
by: Brett Romero | last post by:
Say I have a class inheriting some base class: BaseClass { void Foo() { Update(); } }
24
39176
by: AtariPete | last post by:
Hey All, I have a C# question for you regarding up casting (base to derived). I was wondering about the most elegant way (readable, less code) to cast from a base type to its derived type. Please consider the following two classes: class Base { private int m_valA;
9
2469
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 have a derived class object and then upcast it to its base class, which cast operator should I use? Is it static_cast, or, can I simply cast it implicitly without any operator? Does this upcasting remove the derived class portion of the object?...
9
2427
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 NumberVertices instead of Vertices, I can either just use a non- templated Edge class which stores pointers to my NumberVertices and leave it up to the programmer to cast back again to NumberVertex (this type of casting only works with...
9
3472
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
9819
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10810
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10918
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10460
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9625
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8015
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 presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6041
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4674
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3275
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.