473,322 Members | 1,480 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,322 software developers and data experts.

struct reference?

Can I have a struct reference (i.e. a reference to a struct)?. I tried
it and it compiles, but are there any gotchas I need to be aware of ?

Apr 2 '06 #1
4 1570
In article <P6********************@bt.com>, Bit byte <fl**@flop.com>
wrote:
Can I have a struct reference (i.e. a reference to a struct)?. I tried
it and it compiles, but are there any gotchas I need to be aware of ?


Yes and no. You can think of a struct as a class with different access
restrictions.
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Apr 2 '06 #2
* Bit byte:
Can I have a struct reference (i.e. a reference to a struct)?
Yes, in the sense that you can have a reference to a struct /instance/,
which we usually just call an 'object'.

I tried
it and it compiles, but are there any gotchas I need to be aware of ?


Here are some:

* Aliasing:
That two references refer to the same object, when that is not
expected. Changes via one can then affect what you see via the
other.

* Dangling reference:
A reference kept around after the object it refers to, is
destroyed.

* Assignment prevention:
Having a reference (or a const pointer, or any const member) /in/
an object prevents assignment.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Apr 2 '06 #3


Alf P. Steinbach wrote:
* Bit byte:
Can I have a struct reference (i.e. a reference to a struct)?

Yes, in the sense that you can have a reference to a struct /instance/,
which we usually just call an 'object'.

I tried it and it compiles, but are there any gotchas I need to be
aware of ?

Here are some:

* Aliasing:
That two references refer to the same object, when that is not
expected. Changes via one can then affect what you see via the
other.

* Dangling reference:
A reference kept around after the object it refers to, is
destroyed.

* Assignment prevention:
Having a reference (or a const pointer, or any const member) /in/
an object prevents assignment.

Wow, I did not know about the last one (Assignment prevention) - must
read that one up. Any references will be appreciated (I will ofcourse do
the usual Google searches)

Apr 2 '06 #4
>> * Bit byte:
Can I have a struct reference (i.e. a reference to a struct)?
Alf P. Steinbach wrote:
Yes, in the sense that you can have a reference to a struct /instance/,
which we usually just call an 'object'. I tried it and it compiles, but are there any gotchas I need to be
aware of ?
Here are some:

* Aliasing:
That two references refer to the same object, when that is not
expected. Changes via one can then affect what you see via the
other.

* Dangling reference:
A reference kept around after the object it refers to, is
destroyed.

* Assignment prevention:
Having a reference (or a const pointer, or any const member) /in/
an object prevents assignment.


Yes, though keep in mind that these issues (at least the first two) are
present with any use of references, not just references to structs.

Bit byte <fl**@flop.com> wrote: Wow, I did not know about the last one (Assignment prevention) - must
read that one up. Any references will be appreciated (I will ofcourse do
the usual Google searches)


When a reference is created, it must be bound (at that time) to what it
refers to. The reference cannot be "reseated". So, if you were to try
to assign to a struct with a reference member, then in effect it would
try to reseat the reference. This is not allowed. However, don't
confuse this situation with the situation of copy-construction, which
can have a similar syntax but the above doesn't apply, since we are
creating a new object, instead of assigning a new value to an existing
object.

--
Marcus Kwok
Apr 3 '06 #5

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

Similar topics

26
by: phoenix | last post by:
Hello, I've got a design question. I need to keep track of some variables and I am planning to put them inside a class or struct. Basically I'm talking about 10 bools, 20 ints and 2 arrays of...
9
by: thomson | last post by:
Hi all, Would you please explain me where will be the heap stored if it is declared inside the Class, As class is a reference type, so it gets stored on the heap, but struct is a value...
16
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;
12
by: Mike | last post by:
Consider the following code: """ struct person { char *name; int age; }; typedef struct person* StructType;
7
by: Kenneth Siewers Møller | last post by:
Hi there I know this probably have been addressed before, but I have an object that should contain the following Guid id String fName String lName Could I use a struct for that? I have read...
37
by: JohnGoogle | last post by:
Hi, Newbie question... After a recent article in VSJ I had a go at implementing a Fraction class to aid my understanding of operator overloading. After a previous message someone suggested...
11
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...
45
by: Zytan | last post by:
This returns the following error: "Cannot modify the return value of 'System.Collections.Generic.List<MyStruct>.this' because it is not a variable" and I have no idea why! Do lists return copies...
5
by: karthikbalaguru | last post by:
Hi, struct sky { int stars; struct ocean *oceanptr; }; struct ocean { int waves; struct sky *skyptr;
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.