472,354 Members | 2,025 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,354 software developers and data experts.

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 8127
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**********@discussions.microsoft.com> wrote in message
news:0A**********************************@microsof t.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
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
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...
3
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
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...
23
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
by: Brett Romero | last post by:
Say I have a class inheriting some base class: BaseClass { void Foo() { Update(); } }
24
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....
9
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
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
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
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made but the http to https rule only works for...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...

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.