473,385 Members | 1,429 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,385 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 8272
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
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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
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...

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.