473,805 Members | 2,001 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

array with value semantics

I would like to define a structure or a class with an array field that
behaves like a simple value-semantics variable. For example, I want
something like

public structure polynomial
public a() as double ' the polynomial coefficients
...
end structure

that will behave like

public structure complex
public x,y as double ' the real and imaginary parts
...
end structure

If I declare u and v as complex, no operation on u will affect v. In other
words, the complex structure has value semantics.

If I declare u and v as polynomial, initialize v, and then assign v to u (ie
execute the statement u=v), I now have two references to the same array. An
update to u.a is simultaneously an update to v.a because both u and v have
references to the same array. In other words, polynomial does not have value
semantics.

My question is this - Is there a way to achieve value semantics with an array?

Perhaps the proper word to use here is immutability, but I don't want to get
hung up on these definitions. If I declare variables of type double, I get
value semantics. If I make a structure that contains only doubles, I also
get value semantics. But if I make an array of doubles or a class/structure
that contains an array, I do not get value semantics because an array is a
reference type. What I want is value type treatment of an array.

I am fully aware of the performance issues here. Please no performance
digressions. What I want to know is feasiblility - can it be done? I have
searched and it appears that it can't be done.

FYI, the purpose of this question is just what the examples imply, namely
some mathematical functionality that involves complex numbers, polynomials,
vectors and other algebraic elements with operator overloads and other
extensions. I want value semantics throughout - I want to program with
variables of these new types in the same way I program with a double. But I
also need arrays to get the job done.

Nov 4 '07 #1
2 1453
AMercer wrote:
I would like to define a structure or a class with an array field that
behaves like a simple value-semantics variable. For example, I want
something like

public structure polynomial
public a() as double ' the polynomial coefficients
...
end structure

that will behave like

public structure complex
public x,y as double ' the real and imaginary parts
...
end structure

If I declare u and v as complex, no operation on u will affect v. In other
words, the complex structure has value semantics.

If I declare u and v as polynomial, initialize v, and then assign v to u (ie
execute the statement u=v), I now have two references to the same array. An
update to u.a is simultaneously an update to v.a because both u and v have
references to the same array. In other words, polynomial does not have value
semantics.

My question is this - Is there a way to achieve value semantics with an array?
No.
Perhaps the proper word to use here is immutability, but I don't want to get
hung up on these definitions. If I declare variables of type double, I get
value semantics. If I make a structure that contains only doubles, I also
get value semantics. But if I make an array of doubles or a class/structure
that contains an array, I do not get value semantics because an array is a
reference type. What I want is value type treatment of an array.

I am fully aware of the performance issues here. Please no performance
digressions. What I want to know is feasiblility - can it be done? I have
searched and it appears that it can't be done.

FYI, the purpose of this question is just what the examples imply, namely
some mathematical functionality that involves complex numbers, polynomials,
vectors and other algebraic elements with operator overloads and other
extensions. I want value semantics throughout - I want to program with
variables of these new types in the same way I program with a double. But I
also need arrays to get the job done.
As you can't get value semantics, I believe that immutability is your
best bet. By creating a class and making it immutable, you can copy the
reference of the object without risk. As each instance of the object can
never change, copying the reference and creating a copy of the object
gives the same result.

--
Göran Andersson
_____
http://www.guffa.com
Nov 4 '07 #2
As you can't get value semantics, I believe that immutability is your
best bet. By creating a class and making it immutable, you can copy the
reference of the object without risk. As each instance of the object can
never change, copying the reference and creating a copy of the object
gives the same result.
Thanks Göran, that sounds reasonable. I made a() private and switched to a
class:

public class polynomial
private a() as double ' the polynomial coefficients
...
end class

I added a function to return a clone of a() in case the consumer needs it,
and the operators and methods generally return new poly's. From the
consumer's point of view, I guess there isn't much difference between value
semantics and immutablilty.

Nov 4 '07 #3

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

Similar topics

38
5242
by: VK | last post by:
Hello, In my object I have getDirectory() method which returns 2-dimentional array (or an imitation of 2-dimentional array using two JavaScript objects with auto-handled length property - please let's us do not go into an "each dot over i" clarification discussion now - however you want to call - you call it ;-) array contains records of all files in the current dir. array contains records of all subs in the current dir
204
13142
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 = {0,1,2,4,9};
26
7100
by: Brett | last post by:
I have created a structure with five fields. I then create an array of this type of structure and place the structure into an array element. Say index one. I want to assign a value to field3 of the structure inside the array. When I try this, an error about late assignment appears. Is it possible to assign a value to a structure field that is in an array? I'm currently getting around the problem by creating a new structure, assign...
16
16433
by: Gerrit | last post by:
Hello, Is it possible to sort an array with a struct in it? example: I have a struct: public struct mp3file { public int tracknr;
51
23751
by: Pedro Graca | last post by:
I run into a strange warning (for me) today (I was trying to improve the score of the UVA #10018 Programming Challenge). $ gcc -W -Wall -std=c89 -pedantic -O2 10018-clc.c -o 10018-clc 10018-clc.c: In function `main': 10018-clc.c:22: warning: array subscript has type `char' I don't like warnings ... or casts.
30
3362
by: questions? | last post by:
say I have a structure which have an array inside. e.g. struct random_struct{ char name; int month; } if the array is not intialized by me, in a sense after I allocated a
18
4217
by: toton | last post by:
Hi, In C++ when I initialize an array it, also initializes the class that it contains, which calls the default constructor. However, I want to initialize the array only (i.e reserve the space) and use my specific constructor to initialize the class. How to do it without using malloc? Something like Point* pt = new Point; I want it to reserve the space for N points only, and not to call default constructor. I dont have a default...
11
3378
by: abhiM | last post by:
I have a struct that has an array in it. I need to assign space to the array in a function and pass the corresponding struct by reference to another function so that it can store values into the array. When I try it with the following code i get these errors 1. Use of possibly unassigned field 'micData' 2. The out parameter 'rem' must be assigned to before control leaves the current method Could someone point out how to do this and what...
33
7191
by: Adam Chapman | last post by:
Hi, Im trying to migrate from programming in Matlab over to C. Im trying to make a simple function to multiply one matrix by the other. I've realised that C can't determine the size of a 2d array, so im inputting the dimensions of those myself. The problem is that the output array (C=A*B) has as many rows as A and as many columns as B. I would think of initialising C with:
0
9716
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
9596
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10356
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
10361
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
9179
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...
1
7644
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5676
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3839
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.