473,734 Members | 2,647 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inheritance of constructors.

Hi all.

I'm facing the following construction:

class ClOld
{
public ClOld(string a) { ... }
}

class ClNew: ClOld
{
SomeData[] arr;
public ClNew(string a, SomeObject b): base(a)
{
arr = new SomeData[] {b.data1, b.data2, b.data3 };
}
}

Now, I wonder how to pass initialized arr to a base constructor. Why
can't I code something like below:

class ClOld
{
public ClOld(string a, ref SomeData[] arr) { ... }
}

class ClNew: ClOld
{
SomeData[] arr;
public ClNew(string a, SomeObject b):
{
arr = new SomeData[] {b.data1, b.data2, b.data3 };
base(a, ref arr);
}
}
Thanks in advance,
Pete
Jun 30 '08 #1
21 1398
On Jun 30, 12:17*pm, A n g l e r <p|k|o|n|i|u|.. .@h-o-t-m-a-i-l.c_o_m>
wrote:
I'm facing the following construction:
<snip>
Now, I wonder how to pass initialized arr to a base constructor. Why
can't I code something like below:
<snip>

Basically you can't write any actual code in the constructor before
the base constructor call. You *can* make a static method call, so you
can do:

public ClNew(string a) : base(a, CreateArray())
{
// Other stuff
}

That won't help you for a ref parameter, however. Are you sure you
need a ref parameter in the constructor call? That's pretty unusual.

Jon
Jun 30 '08 #2
Basically you can't write any actual code in the constructor before
the base constructor call. You *can* make a static method call, so you
can do:
Brilliant, I thought so. Blooming drawbacks of the garbage collector and
the whole automated memory management by ms, right? :/
public ClNew(string a) : base(a, CreateArray())
{
// Other stuff
}
That will help for now :)
That won't help you for a ref parameter, however. Are you sure you
need a ref parameter in the constructor call? That's pretty unusual.
No, I guess I don't cos I pass just an array of objects which anyway
means (I guess so) it's handled in a similar way to reference. Isn't it?

DataGridView[] arr = new DataGridView[] { grid1, grid2, grid3 };

Cheers,
P.

Jun 30 '08 #3
On Jun 30, 1:43*pm, A n g l e r <p|k|o|n|i|u|.. .@h-o-t-m-a-i-l.c_o_m>
wrote:
Basically you can't write any actual code in the constructor before
the base constructor call. You *can* make a static method call, so you
can do:

Brilliant, I thought so. Blooming drawbacks of the garbage collector and
the whole automated memory management by ms, right? :/
Well, it's more that doing things within an object before it's been
initialized in the parent is somewhat questionable.

<snip>
That won't help you for a ref parameter, however. Are you sure you
need a ref parameter in the constructor call? That's pretty unusual.

No, I guess I don't cos I pass just an array of objects which anyway
means (I guess so) it's handled in a similar way to reference. Isn't it?

DataGridView[] arr = new DataGridView[] { grid1, grid2, grid3 };
There's a big difference between "pass by reference" and "pass
reference by value". It confuses quite a lot of people, and it's well
worth being aware of the difference. It's mostly down to confusingly
named terminology, IMO.

See http://pobox.com/~skeet/csharp/parameters.html

Jon
Jun 30 '08 #4
Well, it's more that doing things within an object before it's been
initialized in the parent is somewhat questionable.
Consider an example where a new class (for instance it's constructor) is
in charge of the data preparation which is fed to a parent class
constructor. OK, you can pass it via static variable as suggested,
though what happens if you want to have different data in it for each
instance of a new class? Unless static in C# means it exists over the
whole cycle of a code execution but doesn't limit it to one global data
copy shared amongst all instances like in C++. Is this the case?
There's a big difference between "pass by reference" and "pass
reference by value". It confuses quite a lot of people, and it's well
worth being aware of the difference. It's mostly down to confusingly
named terminology, IMO.

See http://pobox.com/~skeet/csharp/parameters.html
Sure, it's just all happens on different levels. The actual reference is
all about where it is in the memory. The value reference is more like a
container that is being copied while passing.

Thanks,
P.
Jun 30 '08 #5
Consider an example where a new class (for instance it's constructor) is
in charge of the data preparation which is fed to a parent class
constructor. OK, you can pass it via static variable as suggested,
though what happens if you want to have different data in it for each
instance of a new class? Unless static in C# means it exists over the
whole cycle of a code execution but doesn't limit it to one global data
copy shared amongst all instances like in C++. Is this the case?
Erm, OK, I see what happens in this case. It's passed via static method,
the value reference is stored locally in a base class. No harm done
unless I would prefer it would be capable of following every change of
the value reference in outer classes ... what then?
Jun 30 '08 #6
On Jun 30, 2:35*pm, A n g l e r <p|k|o|n|i|u|.. .@h-o-t-m-a-i-l.c_o_m>
wrote:
Well, it's more that doing things within an object before it's been
initialized in the parent is somewhat questionable.

Consider an example where a new class (for instance it's constructor) is
in charge of the data preparation which is fed to a parent class
constructor. OK, you can pass it via static variable as suggested,
though what happens if you want to have different data in it for each
instance of a new class? Unless static in C# means it exists over the
whole cycle of a code execution but doesn't limit it to one global data
copy shared amongst all instances like in C++. Is this the case?
I didn't suggest a static *variable*. I suggested a static *method*.
The method should be able to construct the information required for
the base class's constructor which no information about the current
(uninitialized) instance.
There's a big difference between "pass by reference" and "pass
reference by value". It confuses quite a lot of people, and it's well
worth being aware of the difference. It's mostly down to confusingly
named terminology, IMO.
Seehttp://pobox.com/~skeet/csharp/parameters.html

Sure, it's just all happens on different levels. The actual reference is
all about where it is in the memory. The value reference is more like a
container that is being copied while passing.
Not sure what you mean by "value reference" but the point is that
there's a big difference between
void Foo(ref object[] x)
and
void Foo(object[] x)
even though object[] is a reference type. I rarely see a genuine need
for the former signature.

Jon
Jun 30 '08 #7
On Jun 30, 2:48*pm, A n g l e r <p|k|o|n|i|u|.. .@h-o-t-m-a-i-l.c_o_m>
wrote:
Consider an example where a new class (for instance it's constructor) is
in charge of the data preparation which is fed to a parent class
constructor. OK, you can pass it via static variable as suggested,
though what happens if you want to have different data in it for each
instance of a new class? Unless static in C# means it exists over the
whole cycle of a code execution but doesn't limit it to one global data
copy shared amongst all instances like in C++. Is this the case?

Erm, OK, I see what happens in this case. It's passed via static method,
the value reference is stored locally in a base class. No harm done
unless I would prefer it would be capable of following every change of
the value reference in outer classes ... what then?
I don't really understand what you mean by the last sentence. Could
you give a concrete example in code of what you'd like to be able to
do?

Jon
Jun 30 '08 #8
I don't really understand what you mean by the last sentence. Could
you give a concrete example in code of what you'd like to be able to
do?
Ok, here it goes:

class ClOld
{
public SomeData[] arr;
public ClOld(string a, SomeData[] b) { arr=b; }
}

class ClNew: ClOld
{
static SomeData[] arr;
static SomeData[] StaticPassing(S omeClass src)
{
arr = new SomeData[] { src.d1, src.d2, src.d3 };
return arr;
}

public void UpdateArr(src)
{
arr = new SomeData[] { src.d1, src.d2, src.d3 };
// the arr is now assigned a new reference by value
// though, this doesn't mean that base.arr is also updated
// with a new reference value unless
// I'll update it on myself, for instance base.arr=arr; or more
//likely by set, get that act on arr while it remains the
// private member of ClOld

//also, if I made the arr public in thr ClNew and modified its
//reference by value from outside of the ClNew class, I'd have
//to handle some further updating actions which is inconvenient
}

public ClNew(string a, SomeObject b): base(a, StaticCopier())
{
}
}

ClNew aa= new ClNew(src);
aa.arr=new SomeData[] { data }

// aa.base.arr stays unchanged unless I'll update it. In case of memory
// references it wouldn't be any concern as long as all this happens in
// one thread
Jun 30 '08 #9
Not sure what you mean by "value reference" but the point is that
Sorry, I mean reference by value which indicates that the reference is
copied and isn't maintained automatically by another instances of the
actual "reference by value" container :d

class1 aa=new class1("d1");
bb=aa;
aa= new class1("d2");

bb still contains d1
aa contains d2

while in case of memory-wise C++ reference
bb would contain d2
aa would contain also d2

Therefore I said that reference by value is like a container of
reference. It helps you avoid copying of the whole objects, but you have
to maintain on your own the actual copies of the references ...
Jun 30 '08 #10

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

Similar topics

0
4229
by: Alexander Stippler | last post by:
I've got an inheritance structure with two coupled "dreaded diamonds" like shown below: A / \ / \ B C \ / \ \ / \ D E \ /
4
5872
by: Jimmy Johns | last post by:
Hi, I have some classes as follows: #include <iostream> using namespace std; class A { public: virtual A* clone() const = 0; };
1
6781
by: Russ Ford | last post by:
Hi all, I'm trying to get inheritance and constructors clear in my head (and in my code). I have the following inheritance situation (all derivations public): A is the base class B is inherited from A C is inherited from B
4
2281
by: Busin | last post by:
When a child class inherits from a base class, will the child class inherits everything of the base class, including all member variables and functions? Or is such inheritance "selective", like not inheriting all constructors, assignment operators, destructor, etc.? Thanks!
45
6353
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 parameters, I get CS1501 (no method with X arguments). Here's a simplified example which mimics the circumstances: namespace InheritError { // Random base class. public class A { protected int i;
10
1219
by: Kevin Buchan | last post by:
I searched the news group and could not find an answer to this question, so I'll go ahead and post it. Let's say I have a class A with a couple different constructors... nothin' special. Now, let's say that I have class B that inherits from A. B automagically gets all of the properties and methods of A, but I cannot leverage any of A's contructors without redefining them on B and delegating. I'm sure that there is a very good reason...
2
1674
by: shuisheng | last post by:
Dear All, I have a question on inheritance. Assume I have a base class Vector and its derived class DecoratedVector as follows: class Vector { // Constructors. Vector(); Vector(size_t num, double initValue);
7
3739
by: Adam Nielsen | last post by:
Hi everyone, I'm having some trouble getting the correct chain of constructors to be called when creating an object at the bottom of a hierarchy. Have a look at the code below - the inheritance goes like this: Shape | +-- Ellipse | +-- Circle
3
2554
by: Jess | last post by:
Hello, I've been reading Effective C++ about multiple inheritance, but I still have a few questions. Can someone give me some help please? First, it is said that if virtual inheritance is used, then "the responsibility for initializing a virtual base is borne by the most derived class in the hierarchy". What does it mean? Initializing base class is usually done automatically by the compiler, but a derived class can invoke the base...
2
2000
by: Tim Van Wassenhove | last post by:
Hello, When i read the CLI spec, 8.10.2 Method inheritance i read the following: "A derived object type inherits all of the instance and virtual methods of its base object type. It does not inherit constructors or static methods...." In the C# spec, 17.2.1 Inheritance i read the following:
0
8946
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9449
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9310
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9236
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8186
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3261
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2724
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.