By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,719 Members | 1,046 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,719 IT Pros & Developers. It's quick & easy.

struct reference?

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
* 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

P: n/a


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

P: n/a
>> * 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 discussion thread is closed

Replies have been disabled for this discussion.