473,396 Members | 1,996 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.

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 1348
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(SomeClass 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
On Jun 30, 3:23*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 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?
<snip>
// 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
Your example is made somewhat trickier by the fact that one variable
is static, the other isn't, but they both have the same name. Avoiding
making fields public helps too :) I'm also not sure where threading
comes into it particularly...

But yes, your point that the field is a just reference rather than
something which automatically keeps track of a different variable is
correct. I can't remember when I've ever wanted the latter behaviour
though.

Jon
Jun 30 '08 #11
Avoiding
making fields public helps too :)
Bare in mind that was just for sake of example.
>I'm also not sure where threading
comes into it particularly...
It just comes in the sense that you wouldn't like your data collection
being modified while reading. In case of "reference by value" that's not
a problem cos it never gets updated anyway - just remains stuck with an
old collection unless you do something on your own, lol.
But yes, your point that the field is a just reference rather than
something which automatically keeps track of a different variable is
correct. I can't remember when I've ever wanted the latter behaviour
though.
Imagine a class which handles rendering at 15 frames a second. Now,
imagine that you have a collection of objects it renders to screen or
wherever you fancy. Do you really desire to inform the class that some
objects are added/removed from the data collection? I'd rather have a
memory-wise reference that assures the class would always see the
updated collection. What's the point in copying reference values of
let's say 1000 objects 15 times a second (provided collection changes so
dynamically)?

Cheers
Jun 30 '08 #12
On Jun 30, 5:09*pm, A n g l e r <p|k|o|n|i|u|...@h-o-t-m-a-i-l.c_o_m>
wrote:
*Avoiding
making fields public helps too :)

Bare in mind that was just for sake of example.
Unfortunately the duplication of variable names made it hard for me to
see what you actually meant :(
I'm also not sure where threading
comes into it particularly...

It just comes in the sense that you wouldn't like your data collection
being modified while reading. In case of "reference by value" that's not
a problem cos it never gets updated anyway - just remains stuck with an
old collection unless you do something on your own, lol.
No, the collection itself can be changed without changing which
collection the variable refers to.
Imagine a class which handles rendering at 15 frames a second. Now,
imagine that you have a collection of objects it renders to screen or
wherever you fancy. Do you really desire to inform the class that some
objects are added/removed from the data collection? I'd rather have a
memory-wise reference that assures the class would always see the
updated collection. What's the point in copying reference values of
let's say 1000 objects 15 times a second (provided collection changes so
dynamically)?
Um, you will. You can change the contents of the array or another
collection. Here's a short example:

using System;

class Foo
{
string[] names;

public Foo(string[] names)
{
this.names = names;
}

public void ShowNames()
{
Console.WriteLine("Names:");
foreach (string name in names)
{
Console.WriteLine(name);
}
}
}

class Test
{
static void Main()
{
string[] x = {"Jon", "Holly"};
Foo foo = new Foo(x);
foo.ShowNames();
x[0] = "Robin";
x[1] = "William";
foo.ShowNames();
}
}

The changes (made in Test) to the array are visible in Foo, because
they both share a reference to the same array. The same would be true
for other reference types such as List<T>.

Now in the display updating example you'd need to potentially be
careful with threading, yes - but it's not too bad a problem.

Given how the above works, why would I want to use "ref" in the
constructor signature? Bear in mind that the "ref" would only affect
the parameter itself anyway...

Jon
Jun 30 '08 #13
class Test
{
static void Main()
{
string[] x = {"Jon", "Holly"};
Foo foo = new Foo(x);
foo.ShowNames();
x[0] = "Robin";
x[1] = "William";
foo.ShowNames();
}
}
And what now happens if you wanted to add yet another field? You'd have
to write x = {"bla", "bla", "bla"} which results in a new reference by
value that isn't passed to Foo. OK, you may go for some more advanced
container of objects where you can add/delete all stuff by appropriate
methods, though this suddenly becomes more and more elaborate while with
means of memory-wise reference you could replace the whole array easily
if needed.
Jun 30 '08 #14
A n g l e r <p|*************@h-o-t-m-a-i-l.c_o_mwrote:
class Test
{
static void Main()
{
string[] x = {"Jon", "Holly"};
Foo foo = new Foo(x);
foo.ShowNames();
x[0] = "Robin";
x[1] = "William";
foo.ShowNames();
}
}

And what now happens if you wanted to add yet another field?
You can't with an array. You could with other collections such as
List<T>.
You'd have to write x = {"bla", "bla", "bla"} which results in a new
reference by value that isn't passed to Foo. OK, you may go for some
more advanced container of objects where you can add/delete all stuff
by appropriate methods, though this suddenly becomes more and more
elaborate while with means of memory-wise reference you could replace
the whole array easily if needed.
Using List<Tis hardly elaborate, and it results in a nice simple
model. The complexity is hidden in the container, instead of having to
keep track of where a reference originally came from (and making sure
that's not inappropriately on a stack somewhere, ready to pop out of
existence...)

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
Jun 30 '08 #15
You can't with an array. You could with other collections such as
List<T>.
Yeah, I know, am just being fussy cos I like fiddling with all these
properties and above all worship C++ syntax, also I do mix very often
unmanaged code with managed one and all this repacking of more complex
structures is just a waste of a processor time. Tough ...
Jun 30 '08 #16
A n g l e r 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:
>public ClNew(string a) : base(a, CreateArray())
{
// Other stuff
}
Brilliant, I thought so. Blooming drawbacks of the garbage collector and
the whole automated memory management by ms, right? :/
The syntax is almost the same in C++. So no relation to GC.

Arne
Jul 1 '08 #17
Arne, hi.

Thanks for the comment. I've just realised that you're actually right.
I've got a little bit hazy on details and now am rather sure that the
sort of the construction I'm referring to was available in Delphi
(perhaps C++ builder as well - don't remember now).

Cheers,
Pete
The syntax is almost the same in C++. So no relation to GC.

Arne
Jul 1 '08 #18
A n g l e r 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?
I'll admit to being quite confused by your examples, so this may well
be completely unrelated to what you're looking for, but I thought I'd
mention it in the off-chance that it's relevant.

I have sometimes encountered the problem of wanting to do some sort of
calculation in a sub-class constructor before calling the base-class.
As Jon Skeet mentions, sometimes a static method will help you out
here, but this is not a lot of help if you have to pass several
arguments to the base class constructor that are interdependent.
Here's a way you can do that:

public class Base
{
public Base(int a, int b, int c) { /* ... */ }
}
public class Derived : Base
{
public Derived(int x, int y)
:this(new BaseArgs(x,y))
{ }
private Derived(BaseArgs args)
:base(args.a, args.b, args.c)
{ /* ... */ }
private class BaseArgs
{
public int a;
public int b;
public int c;
public BaseArgs(int x, int y)
{
int temp = ComplicatedCalculation(x,y);
a = x + temp;
b = y + temp;
c = temp * temp;
}
}
}

The idea is that you have two constructors for your base class: a
public one and a private one. The public one constructs a special
object and passes it to the private one. The private one extracts data
from that special object and passes it up to the base class. In the
constructor for the special object, you can do whatever it is that you
wanted to do before the base constructor gets called.
Jul 1 '08 #19
A n g l e r wrote:
Arne, hi.

Thanks for the comment. I've just realised that you're actually right.
I've got a little bit hazy on details and now am rather sure that the
sort of the construction I'm referring to was available in Delphi
(perhaps C++ builder as well - don't remember now).
In Java, the call to the base constructor doesn't have to be first.

In C++ it definitely does, I don't see how Borland or Microsoft compiler
would make any difference.
Jul 2 '08 #20
Ben Voigt [C++ MVP] wrote:
A n g l e r wrote:
>Arne, hi.

Thanks for the comment. I've just realised that you're actually right.
I've got a little bit hazy on details and now am rather sure that the
sort of the construction I'm referring to was available in Delphi
(perhaps C++ builder as well - don't remember now).

In Java, the call to the base constructor doesn't have to be first.
Yes - it does.

It is a method call inside the { }, but it need to be the first
statement, so the difference if purely syntactical.

Arne
Jul 2 '08 #21
In C++ it definitely does, I don't see how Borland or Microsoft compiler
would make any difference.
As to Delphi, like it or not, you aren't obliged at all to call any
inherited constructor until you're done with any initialisation bits you
are in need of in first place.

Cheers.
Jul 16 '08 #22

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

Similar topics

0
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
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
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...
4
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...
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...
10
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,...
2
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,...
7
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...
3
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...
2
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...
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
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?
1
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...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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.