473,624 Members | 2,564 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

References, AddressOf and Object management

Am I correct in think that you can't re-assign a reference to a different
object? If so, what should happen here:

struct A
{
DoSomeThing();
};

A& GetNextA();

while (1)
{
A& a = GetNextA();

a.DoSomeThing() ;
}

Does reference 'a' only get assigned first time around the loop and,
thereafter, object 'a' gets assigned to the next 'A'... or is 'a' connected
to a different 'A' every time around the loop? What about this:

while (1)
{
{
A& a = GetNextA();

a.DoSomeThing() ;
}
}

Surely, 'a' would now be different every time around the loop (because the
scope of 'a' ends before looping?

On a related note, is it possible to prevent a reference to a class
instance? Similarly, is it possible to prevent the address of an instance
from being taken?

What I'm looking for is a way to implement a 'Reference<T>' class. In other
words, some kind of class like a smart-pointer except that the 'Reference'
is the only way to access the object to which it refers. I want to prevent
'address-of' and references and use the 'Reference' as a means to manage
object life-time.

Any insight or links to good articles will be very much appreciated.

Regards
Tim
Jul 22 '05 #1
29 1899

"Tim Clacy" <no*******@nosp amphaseone.nosp amdk> wrote in message news:40******** *************@d read11.news.tel e.dk...
while (1)
{
A& a = GetNextA();

a.DoSomeThing() ;
}

Since "a" is not declared static, it gets initalized everytime code passes through
the block.
while (1)
{
{
A& a = GetNextA();

a.DoSomeThing() ;
}
}
The extra { } have no additional effect here.
On a related note, is it possible to prevent a reference to a class
instance? Similarly, is it possible to prevent the address of an instance
from being taken?
You can't do anything to prevent a value from being bound to a reference.
You can make taking the address of it difficult, by overloading the unary
& operator for the class, but I'm not certain that would be a fool proof
way of disabling it entirely.

What I'm looking for is a way to implement a 'Reference<T>' class. In other
words, some kind of class like a smart-pointer except that the 'Reference'
is the only way to access the object to which it refers. I want to prevent
'address-of' and references and use the 'Reference' as a means to manage
object life-time.


I'm unclear of what you're really trying to accomplish, however provided you don't
specifically provide a way to do with it, a Reference<T> wouldn't be bindable to T&.
However such a class would have other problems. Specifically, you can't overload
operator (period). .

Jul 22 '05 #2
Tim Clacy wrote:
Am I correct in think that you can't re-assign a reference to a different
object? If so, what should happen here:
Yes.

[snip] {
A& a = GetNextA();

a.DoSomeThing() ;
}
The object "a" only exists within this scope. Once the scope is left it
should be destroyed. When the loop re-enters the scope a new object,
also called "a", will be created.

I suppose compiler optimisations might change this behaviour in some
unnoticeable way.

Note that I'm not certain about any of this. You could always try it and
see what your compiler does.
Does reference 'a' only get assigned first time around the loop and,
thereafter, object 'a' gets assigned to the next 'A'... or is 'a' connected
to a different 'A' every time around the loop? What about this:

while (1)
{
{
A& a = GetNextA();

a.DoSomeThing() ;
}
}
Call me crazy, but isn't that identical to the last bit of code? You're
just adding an extra (redundant) scope.
Surely, 'a' would now be different every time around the loop (because the
scope of 'a' ends before looping?
Yes, I'm pretty sure they are the same.
On a related note, is it possible to prevent a reference to a class
instance? Similarly, is it possible to prevent the address of an instance
from being taken?

What I'm looking for is a way to implement a 'Reference<T>' class. In other
words, some kind of class like a smart-pointer except that the 'Reference'
is the only way to access the object to which it refers. I want to prevent
'address-of' and references and use the 'Reference' as a means to manage
object life-time.


I do this by writing public static "Create" methods in each class that
return a smart pointer to the new object. All constructors are
private/protected so there is no way of creating the object outside of
the static methods. Consequently there is no direct access to the object
itself (only through the smart pointer), so a reference or pointer to it
cannot be obtained (unless the smart pointer allows it).

Like this:

class SafeObject
{
public:
static MySmartPtr< SafeObject > Create();

private:
SafeObject() {}

// Probably don't need private copy-ctors or copy-assignment
// operators, because you can't access the object directly
// to use them with anyway.
};

Where "MySmartPtr " is your "Reference" class template. You will be able
to take a reference or pointer of MySmartPtr, but not of SafeObject
directly (to the best of my knowledge).

Hope that helps.

-- Pete
Jul 22 '05 #3
Pete Vidler wrote:
Tim Clacy wrote:
Am I correct in think that you can't re-assign a reference to a
different object? If so, what should happen here:
Yes.

[snip]
{
A& a = GetNextA();

a.DoSomeThing() ;
}


The object "a" only exists within this scope. Once the scope is left
it should be destroyed. When the loop re-enters the scope a new
object,
also called "a", will be created.


Pete,

Hi. So when you loop, this is like leaving a scope then re-entering?
I suppose compiler optimisations might change this behaviour in some
unnoticeable way.

Note that I'm not certain about any of this. You could always try it
and
see what your compiler does.
Does reference 'a' only get assigned first time around the loop and,
thereafter, object 'a' gets assigned to the next 'A'... or is 'a'
connected to a different 'A' every time around the loop? What about
this:

while (1)
{
{
A& a = GetNextA();

a.DoSomeThing() ;
}
}
Call me crazy, but isn't that identical to the last bit of code?
You're
just adding an extra (redundant) scope.
Surely, 'a' would now be different every time around the loop
(because the scope of 'a' ends before looping?


Yes, I'm pretty sure they are the same.


Yep, it sounds like they are the same. I wasn't (still not) entirely clear
about life-time of variables/references/objects inside a loop scope. If
variables/references/objects are destroyed and re-constructed every loop
cycle, then my second case is the same as the first.
On a related note, is it possible to prevent a reference to a class
instance? Similarly, is it possible to prevent the address of an
instance from being taken?

What I'm looking for is a way to implement a 'Reference<T>' class.
In other words, some kind of class like a smart-pointer except that
the 'Reference' is the only way to access the object to which it
refers. I want to prevent 'address-of' and references and use the
'Reference' as a means to manage object life-time.


I do this by writing public static "Create" methods in each class that
return a smart pointer to the new object. All constructors are
private/protected so there is no way of creating the object outside of
the static methods. Consequently there is no direct access to the
object itself (only through the smart pointer), so a reference or
pointer to it cannot be obtained (unless the smart pointer allows it).

Like this:

class SafeObject
{
public:
static MySmartPtr< SafeObject > Create();

private:
SafeObject() {}

// Probably don't need private copy-ctors or copy-assignment
// operators, because you can't access the object directly
// to use them with anyway.
};

Where "MySmartPtr " is your "Reference" class template. You will be
able
to take a reference or pointer of MySmartPtr, but not of SafeObject
directly (to the best of my knowledge).

Hope that helps.


Hmm, that's about where I am right now... but all we've done is replaced the
problem of controlling the object life-time with a problem of controlling
the smart-pointer life-time.

-- Pete

Jul 22 '05 #4
Ron Natalie wrote:
"Tim Clacy" <no*******@nosp amphaseone.nosp amdk> wrote in message
news:40******** *************@d read11.news.tel e.dk...
while (1)
{
A& a = GetNextA();

a.DoSomeThing() ;
}

Since "a" is not declared static, it gets initalized everytime code
passes through
the block.
while (1)
{
{
A& a = GetNextA();

a.DoSomeThing() ;
}
}


The extra { } have no additional effect here.
On a related note, is it possible to prevent a reference to a class
instance? Similarly, is it possible to prevent the address of an
instance
from being taken?


You can't do anything to prevent a value from being bound to a
reference.


Ron,

Hi. Doesn't this mean that there is no automatic way to manage object
life-time (because anyone can just connect a C++ reference to any object)?
You can make taking the address of it difficult, by overloading the
unary & operator for the class, but I'm not certain that would be a
fool proof
way of disabling it entirely.

What I'm looking for is a way to implement a 'Reference<T>' class.
In other
words, some kind of class like a smart-pointer except that the
'Reference'
is the only way to access the object to which it refers. I want to
prevent 'address-of' and references and use the 'Reference' as a
means to manage
object life-time.


I'm unclear of what you're really trying to accomplish, however
provided you don't
specifically provide a way to do with it, a Reference<T> wouldn't be
bindable to T&.
However such a class would have other problems. Specifically, you
can't overload
operator (period). .


....hmm, so the long and the short of it is that if you don't mind using '->'
to access objects you can almost acheive automatic object life-time, but if
you want to use '.' you have to use C++ references, which are an
object-life-time management nightmare?

Regards
Tim
Jul 22 '05 #5
Tim Clacy wrote:
[snip]
Hi. So when you loop, this is like leaving a scope then re-entering?
Effectively, yes.

[snip]
Yes, I'm pretty sure they are the same.


Yep, it sounds like they are the same. I wasn't (still not) entirely clear
about life-time of variables/references/objects inside a loop scope. If
variables/references/objects are destroyed and re-constructed every loop
cycle, then my second case is the same as the first.


Of course, I'm not exactly certain about this. Consider the fact that,
in a "for( int i =... )" loop, the i variable is considered to be inside
the loop's scope. It doesn't get deleted and reconstructed each time,
though.

I'm just confusing myself now. In reality all this is rarely an issue.
If you need a variable in every loop cycle, declare it outside the loop.

[snip] Hmm, that's about where I am right now... but all we've done is replaced the
problem of controlling the object life-time with a problem of controlling
the smart-pointer life-time.

[snip]

Smart pointers are typically reference counted. If all you really want
is a smart pointer, check out boost::shared_p tr from www.boost.org. I
can highly recommend it (and the rest of that library).

As an aside about shifting the problem onto a separate class, this is a
common technique in C++. The answer to many problems seems to be adding
an extra layer of indirection (so I've heard).

-- Pete
Jul 22 '05 #6
Pete Vidler wrote:
Tim Clacy wrote:

[snip]
Hi. So when you loop, this is like leaving a scope then re-entering?
Effectively, yes.

[snip]
Yes, I'm pretty sure they are the same.


Yep, it sounds like they are the same. I wasn't (still not) entirely
clear
about life-time of variables/references/objects inside a loop scope. If
variables/references/objects are destroyed and re-constructed every loop
cycle, then my second case is the same as the first.


Of course, I'm not exactly certain about this. Consider the fact that,
in a "for( int i =... )" loop, the i variable is considered to be inside
the loop's scope. It doesn't get deleted and reconstructed each time,
though.


There are two nested scopes associated with for loops: an outer scope
for any declaration-definition in the 'for' initializer, and an inner
scope for the loop body. The inner scope may or may not be marked
explicitly with braces but it's always there. The outer scope is entered
once at the beginning of the construct and left once after all
iterations are complete. The inner scope is entered and left on every
iteration.

For while loops, the scope of a declaration in the 'while' condition
contains the scope of the loop body and is entered and left once for
every iteration. It's less important here to make the 'two scopes'
distinction. The same is true of declarations in 'if' conditions.
('switch' too?)

You can depend on this absolutely. Optimization won't change the meaning
of your code. The one small snag is that there are compilers that use
obsolete rules for the scope of declarations in for, while and if
statements. Hopefully it's no longer necessary to think about that.
[...]


Regards,
Buster.
Jul 22 '05 #7
Pete Vidler wrote:
Tim Clacy wrote:
[snip]
Hi. So when you loop, this is like leaving a scope then re-entering?
Effectively, yes.

[snip]
Yes, I'm pretty sure they are the same.


Yep, it sounds like they are the same. I wasn't (still not) entirely
clear about life-time of variables/references/objects inside a loop
scope. If variables/references/objects are destroyed and
re-constructed every loop cycle, then my second case is the same as
the first.


Of course, I'm not exactly certain about this. Consider the fact that,
in a "for( int i =... )" loop, the i variable is considered to be
inside
the loop's scope. It doesn't get deleted and reconstructed each time,
though.

I'm just confusing myself now. In reality all this is rarely an issue.
If you need a variable in every loop cycle, declare it outside the
loop.

[snip]
Hmm, that's about where I am right now... but all we've done is
replaced the problem of controlling the object life-time with a
problem of controlling the smart-pointer life-time.

[snip]

Smart pointers are typically reference counted. If all you really want
is a smart pointer, check out boost::shared_p tr from www.boost.org. I
can highly recommend it (and the rest of that library).


I've have perused Boost (some of the 15MB) and there's some awfully clever
stuff in there. However, what's to stop someone taking a C++ reference to a
smart-pointer (i.e. defeating the purpose)?
As an aside about shifting the problem onto a separate class, this is
a common technique in C++. The answer to many problems seems to be
adding
an extra layer of indirection (so I've heard).

-- Pete

Jul 22 '05 #8
Buster wrote:
Pete Vidler wrote:
Tim Clacy wrote:
>
[snip]
Hi. So when you loop, this is like leaving a scope then re-entering?


Effectively, yes.
>
[snip]
Yes, I'm pretty sure they are the same.

Yep, it sounds like they are the same. I wasn't (still not) entirely
clear
about life-time of variables/references/objects inside a loop
scope. If variables/references/objects are destroyed and
re-constructed every loop cycle, then my second case is the same as
the first.


Of course, I'm not exactly certain about this. Consider the fact
that, in a "for( int i =... )" loop, the i variable is considered to
be inside the loop's scope. It doesn't get deleted and reconstructed
each time, though.


There are two nested scopes associated with for loops: an outer scope
for any declaration-definition in the 'for' initializer, and an inner
scope for the loop body. The inner scope may or may not be marked
explicitly with braces but it's always there. The outer scope is
entered once at the beginning of the construct and left once after all
iterations are complete. The inner scope is entered and left on every
iteration.

For while loops, the scope of a declaration in the 'while' condition
contains the scope of the loop body and is entered and left once for
every iteration. It's less important here to make the 'two scopes'
distinction. The same is true of declarations in 'if' conditions.
('switch' too?)

You can depend on this absolutely. Optimization won't change the
meaning of your code. The one small snag is that there are compilers
that use obsolete rules for the scope of declarations in for, while
and if statements. Hopefully it's no longer necessary to think about
that.
> [...]


Regards,
Buster.


Cheers.
Jul 22 '05 #9
* "Tim Clacy" <no*******@nosp amphaseone.nosp amdk> schriebt:
Hi. Doesn't this mean that there is no automatic way to manage object
life-time (because anyone can just connect a C++ reference to any object)?


In theory yes. In practice no. Both because code that does that breaks
the contract, and because you _can_ (although no one does) make it
utterly impractical to obtain a direct reference.

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #10

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

Similar topics

28
365
by: Tim Clacy | last post by:
Am I correct in think that you can't re-assign a reference to a different object? If so, what should happen here: struct A { DoSomeThing(); }; A& GetNextA();
0
8231
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
8168
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
8672
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8614
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...
0
8471
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7153
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
6107
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
4075
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
4167
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.