Discussion:
Question RE Variable Scoping, Etc.
(too old to reply)
William R. Lorenz
2005-05-20 03:54:16 UTC
Permalink
Hi All,

My Lab #4 is all done and submitted, but I do have a quick question.

In one of the functions, where I was defining a new self[hash_table]
object, I was required to use the &= as opposed to the = operator:

[... more Kernel_2_Body.h ...]

object Array_Of_Queue table;
self[hash_table] &= table;

[... more Kernel_2_Body.h ...]

Is this a matter of variable scoping, or what's the deal with this?

Thanks, in advance, for your insights! :)
Alan DeLong
2005-05-20 05:25:19 UTC
Permalink
I'm pretty sure that this is a matter of the = operator not being overloaded
for type Array_Of_Queue. In C++, classes do not have automatic functionality
for the assignment operator. I don't know if it is the same way in Java, or
not.

Somehow, though, the RESOLVE/C++ creators have implemented swap
functionality for all types in the RESOLVE catalog.

Someone else can probably give you a more thorough response, if you'd like.

P.S. Where in Lab 4 did you find that you needed to create a new hash table?
I didn't run into that problem...but perhaps I messed something up. I'd be
interested to hear what your approach was.
Post by William R. Lorenz
Hi All,
My Lab #4 is all done and submitted, but I do have a quick question.
In one of the functions, where I was defining a new self[hash_table]
[... more Kernel_2_Body.h ...]
object Array_Of_Queue table;
self[hash_table] &= table;
[... more Kernel_2_Body.h ...]
Is this a matter of variable scoping, or what's the deal with this?
Thanks, in advance, for your insights! :)
A. Karl Kornel
2005-05-20 14:34:54 UTC
Permalink
Post by Alan DeLong
I'm pretty sure that this is a matter of the = operator not being overloaded
for type Array_Of_Queue. In C++, classes do not have automatic functionality
for the assignment operator. I don't know if it is the same way in Java, or
not.
That's correct for the most part. The only thing I should add is
that the = operator and the copy constructor are declared, but they are
marked private so that copying using those methods is prohibited. The
only way to copy between most Catalog components is to do it yourself or
use the Copy_To operation (if one exists).
Post by Alan DeLong
Somehow, though, the RESOLVE/C++ creators have implemented swap
functionality for all types in the RESOLVE catalog.
It's implemented for you using the magic of the Representation and
standard_concrete_operations().
--
=============================
| Alfred Karl Kornel
| -- ***@cis.ohio-state.edu
| Member- Europa Research Group
| UNIX / RESOLVE Consultant
=============================
William R. Lorenz
2005-05-20 20:49:19 UTC
Permalink
Hi there,

First of all, thanks so much to you and Mr. Kornel for the insights! :)
Post by Alan DeLong
P.S. Where in Lab 4 did you find that you needed to create a new hash table?
I didn't run into that problem...but perhaps I messed something up. I'd be
interested to hear what your approach was.
Unless I missed something, which is entirely possible, I created the
Array_Of_Queue object within the Initialize() function and set the
bounds on the array so that the other functions had an array to work
with of the appropriate size and containing the various hash buckets.

I assumed the Initalize() function was a constructor alias of sorts.

Should I have not created the array within that function? :)
Post by Alan DeLong
Post by William R. Lorenz
[... more Kernel_2_Body.h ...]
object Array_Of_Queue table;
self[hash_table] &= table;
[... more Kernel_2_Body.h ...]
A. Karl Kornel
2005-05-20 21:52:32 UTC
Permalink
<<<snip>>>
Unless I missed something, which is entirely possible, I created the
Array_Of_Queue object within the Initialize() function and set the
bounds on the array so that the other functions had an array to work
with of the appropriate size and containing the various hash buckets.
OK, I see what you did. Yes, you are supposed to set the bounds of
the array during the execution of Initialize. As you said, Initialize
is called when an object of the class first comes into scope*. However,
you seemed to miss one thing. self[hash_table] already had an array in
place. All you had to do was call Set_Bounds on that array, you did not
have to create a completely new one.

In fact, the instructions pretty much said this. Let me quote:

"But here, the representation of an empty Partial_Map object must have
the bounds of the Array field (called hash_table) set to 0 and
hash_table_size-1, respectively; by default they are initially 1 and 0.
Therefore, you need to put (only) the following code into the body of
Initialize:

self[hash_table].Set_Bounds (0, hash_table_size-1);"

As the instructions said, the ONLY line of code you needed in
Initialize was the call to Set_Bounds, because the Representation had
already been set up, and the fields had already been filled in.
Although creating a new Array, setting its bounds, and swapping it with
self[hash_table] has the same effect, it was extra work that did not
need to be done**.
--
=============================
| Alfred Karl Kornel
| -- ***@cis.ohio-state.edu
| Member- Europa Research Group
| UNIX / RESOLVE Consultant
=============================

* Well, this isn't entirely true, but for this discussion we can assume that it is.
** It wasn't as much work as before, but again we can ignore that here.
William R. Lorenz
2005-05-22 01:22:39 UTC
Permalink
Hi Mr. Kornel,

I wish I saw those Initialize() instructions earlier *lol* ... I guess
that will teach me to read instructions more thoroughly from now on. ;)

In any case, (a) how is self[hash_table] already instantiated as an
object, and (b) where is the Initalize() function called as it's a
constructor? With regards to the self[hash_table] instantiation, I see
where the representation/Rep is setup with the rep_field_name([...]) in
the Kernel_2.h source. Does the act of defining the representation with
rep_field_name() also instantiatiate an object of the provided type?
Post by A. Karl Kornel
OK, I see what you did. Yes, you are supposed to set the bounds of
the array during the execution of Initialize. As you said, Initialize
is called when an object of the class first comes into scope*. However,
you seemed to miss one thing. self[hash_table] already had an array in
place. All you had to do was call Set_Bounds on that array, you did not
have to create a completely new one.
Richard P. Sharp Jr.
2005-05-22 14:00:25 UTC
Permalink
Post by William R. Lorenz
Hi Mr. Kornel,
Sorry, I'm not Karl, but I do have some extra time this morning. :)
Post by William R. Lorenz
In any case, (a) how is self[hash_table] already instantiated as an
self[hash_table] exists inside the representation record which has
been instatiated outside in Partial_Map_Kernel_2a.h. Take a look at
Partial_Map_Kernel_2a_Rep inside this file:

http://tinyurl.com/bbz4s

See the "Partial_Map_Kernel_2a_Array_Of_Queue"?
Post by William R. Lorenz
object, and (b) where is the Initalize() function called as it's a
Initalize() isn't actually a constructor, it's called by the
constructor if it exists. How's that done? Well it's some trickery
with the "standard_concrete_operations (Partial_Map_Kernel_2);" line
you see floating in the Kernel...
Post by William R. Lorenz
constructor? With regards to the self[hash_table] instantiation, I
see where the representation/Rep is setup with the
rep_field_name([...]) in the Kernel_2.h source. Does the act of
defining the representation with rep_field_name() also instantiatiate
an object of the provided type?
No, rep_field_name() only defines an enumerated type so that you can
access the field in the record. The queue itself is instatiated in
the link above as "Partial_Map_Kernel_2a_Queue_Of_D_R_Pair".

Rich
Bruce W. Weide
2005-05-23 13:00:01 UTC
Permalink
Post by Richard P. Sharp Jr.
Post by William R. Lorenz
In any case, (a) how is self[hash_table] already instantiated as an
self[hash_table] exists inside the representation record which has
been instatiated outside in Partial_Map_Kernel_2a.h. Take a look at
http://tinyurl.com/bbz4s
See the "Partial_Map_Kernel_2a_Array_Of_Queue"?
I think there is probably some confusion here between "instantiated" as we
use the term (to mean "instantiating a template", i.e., creating a concrete
instance class from a class template), and as it's used by some
object-oriented programmers in languages that don't have templates (to mean
"creating a new object", i.e., in C++ declaring an object whose constructor
is automatically called there). I'm pretty sure William meant the latter in
his question, and Rich the former in his answer.

The creation of each object in a Representation record occurs (logically)
when the client declares the object it represents. For example, when a
client declares a Partial_Map object, the encapsulated Representation record
for that object is created automatically AND all the objects in the
Representation record's fields are created automatically. If and only if
the default initial values of any of the objects in these fields NEED TO BE
CHANGED to give you the desired representation of an initial value for a
Partial_Map, then you must write an Initialize operation.
Post by Richard P. Sharp Jr.
Post by William R. Lorenz
object, and (b) where is the Initalize() function called as it's a
Initalize() isn't actually a constructor, it's called by the
constructor if it exists.
That is, if an Initialize body exists. The constructor always exists in
RESOLVE/C++ components. It is the default code that is invoked
automatically when you declare an object. You never need to write a body
for the constructor in R/C++; the code you might have written there in other
disciplines goes in Initialize instead.
Post by Richard P. Sharp Jr.
How's that done? Well it's some trickery
with the "standard_concrete_operations (Partial_Map_Kernel_2);" line
you see floating in the Kernel...
We don't want to seem too mysterious here :-). So, maybe during the last
week of classes -- when we'll try to answer as many open questions as
students ask and discuss what students want -- you can ask your instructor
why the code we write in Initialize isn't placed in the constructor as it
would be in most object-oriented languages. (It has to do with the
weasel-word "logically" in the previous paragraph...)
--
Cheers,
Bruce
Loading...