Hi,
Consider two classes from different namespaces each of which is derived
from a given class in the third namespace:
1. MyNamespace1.FirstClass : ThirdNamespace.MyBaseClass
2. MyNamespace2.SecondClass : ThirdNamespace.MyBaseClass
and consider the following code snippet:
MyNamespace1.FirstClass c1;
MyNamespace2.SecondClass c2;
Why the following assignment doesn't simply work?
c1 = (ThirdNamespace.MyBaseClass)c2;
How am I supposed to do the above mentioned assignment?
Any help would be highly appreciated,
TIA,
Mehdi 6 1219
mehdi_mousavi wrote:
Consider two classes from different namespaces each of which is derived
from a given class in the third namespace:
1. MyNamespace1.FirstClass : ThirdNamespace.MyBaseClass
2. MyNamespace2.SecondClass : ThirdNamespace.MyBaseClass
and consider the following code snippet:
MyNamespace1.FirstClass c1;
MyNamespace2.SecondClass c2;
Why the following assignment doesn't simply work?
c1 = (ThirdNamespace.MyBaseClass)c2;
How am I supposed to do the above mentioned assignment?
You can't. Firstly, namespaces are irrelevant here. Secondly, let's
consider a "real-world" example - a base class of Animal, with Dog and
Cat derived classes. Your code is trying to do:
Dog dog;
Cat cat;
....
dog = (Animal) cat;
Can you see why that shouldn't work? What would you expect to happen if
you tried to ask "dog" to bark after that assignment?
Leaving aside the fact that it was a cat, you can't do:
myDerivedClassVariable = (MyBaseClass) something;
because not every instance of the base class is an instance of the
specific derived class. For example, you can't do:
MemoryStream stream = new MemoryStream();
string x = (object) stream;
Jon
hi,
mehdi_mousavi wrote:
1. MyNamespace1.FirstClass : ThirdNamespace.MyBaseClass
2. MyNamespace2.SecondClass : ThirdNamespace.MyBaseClass
and consider the following code snippet:
MyNamespace1.FirstClass c1;
MyNamespace2.SecondClass c2;
Why the following assignment doesn't simply work?
c1 = (ThirdNamespace.MyBaseClass)c2;
How am I supposed to do the above mentioned assignment?
Don't think that this will work in any other OO language.
Maybe this will work:
ThirdNamespace.MyBaseClass polymorph1 = null, polymorph2 = null;
polymorph1 = (ThirdNamespace.MyBaseClass)c1;
polymorph2 = polymorph1;
((MyNamespace1.FirstClass) polymorph2).DoSomething();
mfG
--stefan <--
Hi,
Consider two classes from different namespaces each of which is derived
from a given class in the third namespace:
1. MyNamespace1.FirstClass : ThirdNamespace.MyBaseClass
2. MyNamespace2.SecondClass : ThirdNamespace.MyBaseClass
and consider the following code snippet:
MyNamespace1.FirstClass c1;
MyNamespace2.SecondClass c2;
Why the following assignment doesn't simply work?
c1 = (ThirdNamespace.MyBaseClass)c2;
How am I supposed to do the above mentioned assignment?
Any help would be highly appreciated,
TIA,
Mehdi
The fact that those classes are defined in separate namespaces doesn't
matter. It's the fact that both FirstClass and SecondClass are "more"
than a MyBaseClass (at least as far as the compiler is concerned).
The cast from c2 to MyBaseClass works, because you tell the compiler to
"ignore" the extras (=what SecondClass has more than MyBaseClass). But
the assignment of that MyBaseClass to c1 fails, because the compiler
can't "add" the other extra's (=what FirstClass has more than
MyBaseClass).
Maybe (that would depend on how those derived classes really differ)
you can write a cast-operator (or a conversion method) from SecondClass
to FirstClass (if you still want to be able to do this assignment).
Hans Kesting
Wow, I thought it works under C++. However, it also gives the C2679
error. Anyway, let's back to C#. How am I supposed to write a cast
operator?
TIA,
Mehdi
Hans Kesting wrote:
Hi,
Consider two classes from different namespaces each of which is derived
from a given class in the third namespace:
1. MyNamespace1.FirstClass : ThirdNamespace.MyBaseClass
2. MyNamespace2.SecondClass : ThirdNamespace.MyBaseClass
and consider the following code snippet:
MyNamespace1.FirstClass c1;
MyNamespace2.SecondClass c2;
Why the following assignment doesn't simply work?
c1 = (ThirdNamespace.MyBaseClass)c2;
How am I supposed to do the above mentioned assignment?
Any help would be highly appreciated,
TIA,
Mehdi
The fact that those classes are defined in separate namespaces doesn't
matter. It's the fact that both FirstClass and SecondClass are "more"
than a MyBaseClass (at least as far as the compiler is concerned).
The cast from c2 to MyBaseClass works, because you tell the compiler to
"ignore" the extras (=what SecondClass has more than MyBaseClass). But
the assignment of that MyBaseClass to c1 fails, because the compiler
can't "add" the other extra's (=what FirstClass has more than
MyBaseClass).
Maybe (that would depend on how those derived classes really differ)
you can write a cast-operator (or a conversion method) from SecondClass
to FirstClass (if you still want to be able to do this assignment).
Hans Kesting
mehdi_mousavi wrote:
Wow, I thought it works under C++. However, it also gives the C2679
error. Anyway, let's back to C#. How am I supposed to write a cast
operator?
They're actually called 'conversion operators', and that's the name of
the help topic, or just follow links from the help for the 'implicit'
and 'explicit' keywords.
--
Larry Lard la*******@googlemail.com
The address is real, but unread - please reply to the group
For VB and C# questions - tell us which version
Wow, I thought it works under C++. However, it also gives the C2679
error. Anyway, let's back to C#. How am I supposed to write a cast
operator?
TIA,
Mehdi
http://msdn2.microsoft.com/en-us/library/ms173105.aspx
should help you in the right direction.
Hans Kesting
>
Hans Kesting wrote:
>>Hi, Consider two classes from different namespaces each of which is derived from a given class in the third namespace:
1. MyNamespace1.FirstClass : ThirdNamespace.MyBaseClass 2. MyNamespace2.SecondClass : ThirdNamespace.MyBaseClass
and consider the following code snippet:
MyNamespace1.FirstClass c1; MyNamespace2.SecondClass c2;
Why the following assignment doesn't simply work?
c1 = (ThirdNamespace.MyBaseClass)c2;
How am I supposed to do the above mentioned assignment?
Any help would be highly appreciated,
TIA, Mehdi
The fact that those classes are defined in separate namespaces doesn't matter. It's the fact that both FirstClass and SecondClass are "more" than a MyBaseClass (at least as far as the compiler is concerned).
The cast from c2 to MyBaseClass works, because you tell the compiler to "ignore" the extras (=what SecondClass has more than MyBaseClass). But the assignment of that MyBaseClass to c1 fails, because the compiler can't "add" the other extra's (=what FirstClass has more than MyBaseClass).
Maybe (that would depend on how those derived classes really differ) you can write a cast-operator (or a conversion method) from SecondClass to FirstClass (if you still want to be able to do this assignment).
Hans Kesting
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Nitin Bhardwaj |
last post by:
Thanx in advance for the response...
I wanna enquire ( as it is asked many a times in Interviews that i
face as an Engg PostGraduate ) about the overloading capability of the
C++ Language.
...
|
by: CoolPint |
last post by:
It seems to me that I cannot assign objects of a class which has a constant data
member since the data member cannot be changed once the constructor calls are
completed. Is this the way it is meant...
|
by: Edward Diener |
last post by:
Is there a way to override the default processing of the assignment operator
for one's own __value types ? I realize I can program my own Assign method,
and provide that for end-users of my class,...
|
by: Rick N. Backer |
last post by:
I have an abstract base class that has char* members. Is an
assignment operator necessary for this abstract base class? Why or
why not?
Thanks in advance.
Ken Wilson
Amer. Dlx. Tele,...
|
by: Matthew Polder |
last post by:
Hi,
When a class Apple is written and the assignment operator is not
explicitly declared, the operator
Apple& operator=(const Apple&)
is created by the compiler. Is there any difference...
|
by: Jon Slaughter |
last post by:
I have a chain of classes(i.e., a series of classes each containing an array
of the next class). Each class has array like access.
struct Myclass1
{
vector(Myclass2) _Myclass2;
Myclass2&...
|
by: Neil Zanella |
last post by:
Hello,
I would like to know whether the following C fragment is legal in
standard C and behaves as intended under conforming implementations...
union foo {
char c;
double d;
};
|
by: anongroupaccount |
last post by:
What measures should be taken to avoid this sort of thing?
class Base
{
};
class Derived1 : public Base
{
private:
int i, j, k;
|
by: raylopez99 |
last post by:
I need an example of a managed overloaded assignment operator for a
reference class, so I can equate two classes A1 and A2, say called
ARefClass, in this manner: A1=A2;. For some strange reason...
|
by: George2 |
last post by:
Hello everyone,
I am wondering the default implementation of assignment operator (e.g.
when we do not implement assignment operator in user defined class,
what will be returned? temporary...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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: 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,...
|
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...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |