Class inheritance question
-
Hi! Is it possible to have two instances of a classes which both instances share the same base class instance? For example:
class Program { static void Main(string\[\] args) { Bar bar1 = new Bar(); Bar bar2 = new Bar(); } } class Foe { } class Bar : Foe { }
Is this possible to get the instanees bar1 and bar2 to share the same base instance? /Mc_Topaz!
huh sorry i dont understand
-
Hi! Is it possible to have two instances of a classes which both instances share the same base class instance? For example:
class Program { static void Main(string\[\] args) { Bar bar1 = new Bar(); Bar bar2 = new Bar(); } } class Foe { } class Bar : Foe { }
Is this possible to get the instanees bar1 and bar2 to share the same base instance? /Mc_Topaz!
-
Hi! Is it possible to have two instances of a classes which both instances share the same base class instance? For example:
class Program { static void Main(string\[\] args) { Bar bar1 = new Bar(); Bar bar2 = new Bar(); } } class Foe { } class Bar : Foe { }
Is this possible to get the instanees bar1 and bar2 to share the same base instance? /Mc_Topaz!
I'm a bit unsure that you are asking here. Are you asking whether you can have two classes which are the same instance, i.e. using a Singleton pattern?
Deja View - the feeling that you've seen this post before.
-
Hi! Is it possible to have two instances of a classes which both instances share the same base class instance? For example:
class Program { static void Main(string\[\] args) { Bar bar1 = new Bar(); Bar bar2 = new Bar(); } } class Foe { } class Bar : Foe { }
Is this possible to get the instanees bar1 and bar2 to share the same base instance? /Mc_Topaz!
Well, I shall try to explain what I mean by replying to my own post. When I create a instance for the Bar class, the code will automatically create an instance in the Foe class. So I have one Bar instance and also one Foe instance. Let's call the Bar class instance: bar1. Later I would like one more instance of the Bar class: bar2. But in this case, I don't want a brand new instance of the Foe class. Instead I want bar2 to have the same foe instance as bar1 have. Is this possible? I hope I explained it better this time.
-
Well, I shall try to explain what I mean by replying to my own post. When I create a instance for the Bar class, the code will automatically create an instance in the Foe class. So I have one Bar instance and also one Foe instance. Let's call the Bar class instance: bar1. Later I would like one more instance of the Bar class: bar2. But in this case, I don't want a brand new instance of the Foe class. Instead I want bar2 to have the same foe instance as bar1 have. Is this possible? I hope I explained it better this time.
Bar bar 1 = new Bar(); Bar bar2 = bar1;
-
Well, I shall try to explain what I mean by replying to my own post. When I create a instance for the Bar class, the code will automatically create an instance in the Foe class. So I have one Bar instance and also one Foe instance. Let's call the Bar class instance: bar1. Later I would like one more instance of the Bar class: bar2. But in this case, I don't want a brand new instance of the Foe class. Instead I want bar2 to have the same foe instance as bar1 have. Is this possible? I hope I explained it better this time.
No, that is not possible. Inheritance is the wrong technique for that. What you want to do is set up an association. Create a field in Bar that references the Foo object. You can then share the same Foo object between two Bar objects.
Recent blog posts: *SQL Server / Visual Studio install order *Installing SQL Server 2005 on Vista *Crazy Extension Methods Redux * Mixins My Blog
-
Well, I shall try to explain what I mean by replying to my own post. When I create a instance for the Bar class, the code will automatically create an instance in the Foe class. So I have one Bar instance and also one Foe instance. Let's call the Bar class instance: bar1. Later I would like one more instance of the Bar class: bar2. But in this case, I don't want a brand new instance of the Foe class. Instead I want bar2 to have the same foe instance as bar1 have. Is this possible? I hope I explained it better this time.
To extend what Colin said, take a look at using the Singleton pattern for the Foo class.
Deja View - the feeling that you've seen this post before.
-
Hi! Is it possible to have two instances of a classes which both instances share the same base class instance? For example:
class Program { static void Main(string\[\] args) { Bar bar1 = new Bar(); Bar bar2 = new Bar(); } } class Foe { } class Bar : Foe { }
Is this possible to get the instanees bar1 and bar2 to share the same base instance? /Mc_Topaz!
You spelled "Foo" wrong.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 -
You spelled "Foo" wrong.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001I think he actually means it's Bars enemy.
Deja View - the feeling that you've seen this post before.
-
Bar bar 1 = new Bar(); Bar bar2 = bar1;