473,396 Members | 1,872 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,396 software developers and data experts.

C# inheritance question from a C++ coder

Hi folks,

I'm trying to do the C# equivilent of the following and it's driving
me up the wall.

// assign base-class portion of an object to a given reference
class base
{
public:
int a;
};

class derived : public base
{
public:
int b;
};

void main(void)
{
derived d;

d.a = 10;
d.b = 20;

base b;
b.a = 30;

((base&) d) = b; d.a becomes 30
}

This comes up b/c I need access to the Client property of a TcpClient
and (unchangable) code I'm interfacing with passes me an already
connected TcpClient. I simply want to do something like this:

class TcpClientEx:TcpClient
{
TcpClientEx(TcpClient alreadyConnectedClient)
{
((TcpClient)this) = alreadyConnectedClient; // won't
compile
}

Socket getClient() { return Client; }
}

I feel like I'm missing something obvious -- can somebody help me out?

Thanks!!

larry
Nov 15 '05 #1
3 1124
Hi,
((TcpClient)this) = alreadyConnectedClient; // won't
compile
The assignment operator assigns references (that is, pointers) in C#. To the
best of my knowledge, it is impossible to overload the "=" operator to clone
a class instance. The .NET Framework has a special way to do that - the
Clone method. Please refer to MSDN on Clone() and ICloneable().

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

<da*********@hotmail.com> wrote in message
news:ff**************************@posting.google.c om... Hi folks,

I'm trying to do the C# equivilent of the following and it's driving
me up the wall.

// assign base-class portion of an object to a given reference
class base
class TcpClientEx:TcpClient
{
TcpClientEx(TcpClient alreadyConnectedClient)
{
((TcpClient)this) = alreadyConnectedClient; // won't
compile
}

Socket getClient() { return Client; }
}

I feel like I'm missing something obvious -- can somebody help me out?

Thanks!!

larry


Nov 15 '05 #2
Hi Larry,

<da*********@hotmail.com> wrote in message
news:ff**************************@posting.google.c om...
Hi folks,

I'm trying to do the C# equivilent of the following and it's driving
me up the wall. <snip> This comes up b/c I need access to the Client property of a TcpClient
and (unchangable) code I'm interfacing with passes me an already
connected TcpClient. I simply want to do something like this:

class TcpClientEx:TcpClient
{
TcpClientEx(TcpClient alreadyConnectedClient)
{
((TcpClient)this) = alreadyConnectedClient; // won't
compile
}

Socket getClient() { return Client; }
}

I feel like I'm missing something obvious -- can somebody help me out?


The thing you're missing is that C# is more type-safe than C++.
Actually, you can't do this for two reasons.

1. "this" is not assignable, ever.
2. You cannot assign an instance of a super-type to a variable of a
sub-type.

Regards,
Dan
Nov 15 '05 #3
The only way you can get the Client out of the TcpClient object is either to
control the instantiation of the TcpClient yourself, and return an object of
your class with something public (which is ruled out by the conditions in
your post, I guess you are probably using TcpListener), or use reflection to
pull it out.

Niall

<da*********@hotmail.com> wrote in message
news:ff**************************@posting.google.c om...
Hi folks,

I'm trying to do the C# equivilent of the following and it's driving
me up the wall.

// assign base-class portion of an object to a given reference
class base
{
public:
int a;
};

class derived : public base
{
public:
int b;
};

void main(void)
{
derived d;

d.a = 10;
d.b = 20;

base b;
b.a = 30;

((base&) d) = b; d.a becomes 30
}

This comes up b/c I need access to the Client property of a TcpClient
and (unchangable) code I'm interfacing with passes me an already
connected TcpClient. I simply want to do something like this:

class TcpClientEx:TcpClient
{
TcpClientEx(TcpClient alreadyConnectedClient)
{
((TcpClient)this) = alreadyConnectedClient; // won't
compile
}

Socket getClient() { return Client; }
}

I feel like I'm missing something obvious -- can somebody help me out?

Thanks!!

larry

Nov 15 '05 #4

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

Similar topics

11
by: Ricky Romaya | last post by:
Hi, Are there any ways to get multiple inheritace in PHP4? For example, I have 3 parent class, class A, B, and C. I want class X to inherit all those 3 classes. Consider merging those 3 classes...
4
by: Dan Bullok | last post by:
I have a couple of classes: class Base: ... class Sub(Base): ... I want to subclass Base and Sub, i.e. define classes: class MyBase(Base):
2
by: AIM | last post by:
Error in msvc in building inheritance.obj to build hello.pyd Hello, I am trying to build the boost 1.31.0 sample extension hello.cpp. I can not compile the file inheritance.cpp because the two...
11
by: Josh Lessard | last post by:
Hi all. I'm maintaining a C++ program and I've come across a nasty piece of code that works, but I just don't understand why. I'm not actually this part of the program, but I really want to know...
4
by: JKop | last post by:
I'm starting to think that whenever you derive one class from another, that you should use virtual inheritance *all* the time, unless you have an explicit reason not to. I'm even thinking that...
14
by: Steve Jorgensen | last post by:
Recently, I tried and did a poor job explaining an idea I've had for handling a particular case of implementation inheritance that would be easy and obvious in a fully OOP language, but is not at...
22
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
47
by: Mark | last post by:
why doesn't .NET support multiple inheritance? I think it's so silly! Cheers, Mark
3
by: Leo Seccia | last post by:
Hello everyone, I have a c# project with a sql server database. I have a number of lookup tables in my database which I successfully managed to import into my LINQ dataclasses. eg. Table:...
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: 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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
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
Oralloy
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,...
0
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...
0
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 project—planning, coding, testing,...

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.