473,395 Members | 2,436 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,395 software developers and data experts.

Problem with inheritance / compossition

Hi I have this C# code
---------------------------------

class BaseUnit;
....
class ResizeBorder : Shape
....

class Shape : BaseUnit
{
ResizeBorder a = new ResizeBorder();
}
---------------------------------
(The classes offcourse has implementations)

When i do this i get a stackOverFlowException.

If ResizeBorder dosent inherit from shape it works perfeckt...Am I doing
somethíng wrong? Or is the StackOverFlow maybe caused ba something else wich
first shows when i use the above form?

Anders

Nov 16 '05 #1
3 1163
Hi, Flare

You have perfect infinite loop of constructors here. In Shape constructor
you create ResizeBorder, which calls Shape constructor, which once again
creates ResizeBorder and so on. That's why you have stack overflow.

HTH
Alex

"Flare" <no****@sorry.dk> wrote in message
news:ef**************@tk2msftngp13.phx.gbl...
Hi I have this C# code
---------------------------------

class BaseUnit;
...
class ResizeBorder : Shape
...

class Shape : BaseUnit
{
ResizeBorder a = new ResizeBorder();
}
---------------------------------
(The classes offcourse has implementations)

When i do this i get a stackOverFlowException.

If ResizeBorder dosent inherit from shape it works perfeckt...Am I doing
somethíng wrong? Or is the StackOverFlow maybe caused ba something else wich first shows when i use the above form?

Anders


Nov 16 '05 #2
Flare,

The reason this is happening is because when you create a new
ResizeBorder instance, the constructor for the Shape class needs to be
called. When that happens, you create another ResizeBorder instance, which
then calls the constructor of the Shape class again, and so on, and so on,
ad infinitum (or at least, until you get the StackOverflowException).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Flare" <no****@sorry.dk> wrote in message
news:ef**************@tk2msftngp13.phx.gbl...
Hi I have this C# code
---------------------------------

class BaseUnit;
...
class ResizeBorder : Shape
...

class Shape : BaseUnit
{
ResizeBorder a = new ResizeBorder();
}
---------------------------------
(The classes offcourse has implementations)

When i do this i get a stackOverFlowException.

If ResizeBorder dosent inherit from shape it works perfeckt...Am I doing
somethíng wrong? Or is the StackOverFlow maybe caused ba something else wich first shows when i use the above form?

Anders


Nov 16 '05 #3
This is because you have a cyclic reference in the classes. the
dependency of the classes causes the constructor to be called infinitely

Flare wrote:
Hi I have this C# code
---------------------------------

class BaseUnit;
...
class ResizeBorder : Shape
...

class Shape : BaseUnit
{
ResizeBorder a = new ResizeBorder();
}
---------------------------------
(The classes offcourse has implementations)

When i do this i get a stackOverFlowException.

If ResizeBorder dosent inherit from shape it works perfeckt...Am I doing
somethíng wrong? Or is the StackOverFlow maybe caused ba something else wich
first shows when i use the above form?

Anders


--
Regards,
Dilip Krishnan
MCAD, MCSD.net
dilipdotnet at apdiya dot com
Nov 16 '05 #4

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

Similar topics

18
by: George Sakkis | last post by:
I'm looking for a design to a problem I came across, which goes like this (no, it's not homework): 1. There is a (single inheritance) hierarchy of domain classes, say A<-B<-..<-Z (arrows point...
2
by: Graham Banks | last post by:
Does using multiple inheritance introduce any more performance overhead than single inheritance?
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...
3
by: Morten Aune Lyrstad | last post by:
Hi again! I'm having problems with inheritance. I have a base interface class called IObject. Next I have two other interfaces classes, IControl and ICommandMaster, which derives from IObject. ...
0
by: Bita-kookoo | last post by:
I am having a problem with inheritance for my web solution. Here are the steps I took for a project within C#: -I first created a new class, descended from System.Web.UI.Page. For this...
0
by: Mariano | last post by:
Hi, I have posted a bug of the forms designer that overwrites always the Icon, thus preventing Icon inheritance when you derive from a form. I am trying to overcome this by adding an ImageList in...
9
by: surendran.d | last post by:
hi, can diamond inhertance problem be solved using virtual functions,, or can only be done with scope resolution operators.. is there any other way to solve this problem... Thanks, suri
14
by: dl | last post by:
I have two classes, say A and B, both having a data member 'int n'; private in A, public in B. When I derive class C from both public A and public B, B::n should be visible to C while A::n...
9
by: weird0 | last post by:
How does C++ and C# solve the Diamond problem? With the help of interfaces that is. Can anyone elaborate ....... Regards
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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...
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.