473,549 Members | 2,751 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reference vs object handle

I don't understand the difference between ojbect handle and reference.
For example,

class Stack
{
private:
CD cd_obj; // cd object
DVD & dvd_ref;// dvd reference

Why is it that dvd_ref can ONLY be initialized in the Stack's
constructor whereas cd_obj can be initialized during the Stack
object's lifetime?

Also, why is not allowed to return by reference a locally created
object?
For example,
Vector Vector::operato r+(const Vector & b) const
{
return Vector(x+b.x, y+b.y); //Why is this allowed?

}

Vector & Vector::operato r+(const Vector & b) const
{
return Vector(x+b.x, y+b.y); //Why is this NOT allowed?

}
Jul 19 '05 #1
1 6340
<te************ ***@hotmail.com > wrote in message
news:41******** *************** ***@posting.goo gle.com...
I don't understand the difference between ojbect handle and reference.
For example,

class Stack
{
private:
CD cd_obj; // cd object
DVD & dvd_ref;// dvd reference

Why is it that dvd_ref can ONLY be initialized in the Stack's
constructor whereas cd_obj can be initialized during the Stack
object's lifetime?
A reference must be initialized. For instance you can't write:

int &foo;

You would have a reference to nothing in particular which is not allowed.
Actually this is a big advantage of references over pointers which can be
left uninitialized.

If dvd_ref isn't initialized in Stack's constructor it would violate this
rule.

Also, why is not allowed to return by reference a locally created
object?
For example,
Vector Vector::operato r+(const Vector & b) const
{
return Vector(x+b.x, y+b.y); //Why is this allowed?
Consider the calling sequence:

Vector v = v1 + v2;

Effectively this is the same as

Vector v = Vector(v1.x + v2.x, v1.y + v2.y);

No problem here. The values of the temporary on the right hand side are
copied to the values in v. (Conceptually at least -- the compiler may
optimize that out.)

}

Vector & Vector::operato r+(const Vector & b) const
{
return Vector(x+b.x, y+b.y); //Why is this NOT allowed?
Again, consider the calling sequence:

Vector &v = v1 + v2;

which is now the same as the nonsense statement

Vector &v = Vector(v1.x + v2.x, v1.y + v2.y);

Since the right hand side is a temporary object it can hardly be used to
create a valid reference.

}


--
Cy
http://home.rochester.rr.com/cyhome/
Jul 19 '05 #2

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

Similar topics

3
2079
by: Christian Lammel | last post by:
i'm looking for a possibility in C# to get a unique Int32 handle for an object reference. This handle must later (within the same process of course) be convertible back to an object reference (call it handle or pointer or whatever...). at the moment i'm working with a Hashtable Int32 to Object, but i want to optimize this. I know it is...
10
2253
by: Tony Johansson | last post by:
Hello Experts!! This class template and main works perfectly fine. I have this class template called Handle that has a pointer declared as T* body; As you can see I have a reference counter in the class template so I know how many references I have to the body. In my case it's the Integer wrapper class which is the body. This template...
11
2179
by: Doug | last post by:
Is there any harm in passing an object into a method with the 'ref' keyword if the object is already a reference variable? If not, is there any benefit?
11
9034
by: Steve Jorgensen | last post by:
I just came up with a really tidy little solution to the VB/VBA circular reference issue. It only works with Access 2000 or newer, but that's about the only down-side. The issue... You need an object model that includes a container object, and the object in the container need to interact with the container itself. The problem is that, to...
2
3005
by: Steve Jorgensen | last post by:
When writing VB or VBA code that works with databases or other external libraries that cannot be trusted to automatically do the right thing when references to their objects are arbitrarily released, some thought must be put into how to make sure the objects will all be closed and released in the correct order, even in the result of an error....
5
1545
by: Javier Campos | last post by:
WARNING: This is an HTML post, for the sake of readability, if your client can see HTML posts, do it, it doesn't contain any script or virus :-) I can reformat a non-HTML post if you want me to (and if this doesn't see correctly with non-HTML viewers) Ok, I'm fed up with this so I'll explain the situation here and my approach (which sucks),...
2
5300
by: Giovanni Bassi | last post by:
Hello All, I have encountered a problem. I am using visual inheritance and my base form adds an event handler on Form Load using the AddHandler Keyword. The problem is that if the Event Handler code is there, when I create the inherited form I get the error "Object Reference not set to an instance of an object". If it is not I get no...
2
1344
by: Friskusen | last post by:
Hello VB.net programmers ! I'm just trying to understand what happens with an object which is initialised over and over again. The code fragment below illustrates my problem. This code is simply a tcp listener which creates a new client object and starts a new thread inside that object to
51
4414
by: Kuku | last post by:
What is the difference between a reference and a pointer?
0
7524
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...
0
7451
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...
0
7960
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...
1
7475
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...
1
5372
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...
0
3501
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...
0
3483
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1944
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1061
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.