Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 131,562 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,186 people online right now. Registration is fast and FREE... Join Now!




Data Types

 
Reply to this topicStart new topic

Data Types

net nerd865
post 8 Aug, 2008 - 04:26 AM
Post #1


New D.I.C Head

*
Joined: 16 Jun, 2008
Posts: 44



Thanked 2 times
My Contributions


I'm working on a custom Vector3d class to help reinforce my understanding of calc stuff that I'm doing.

I was writing some code and I came accross the overloaded = () and Length() which were like so:
CODE

    Vector3d& operator = (const Vector3d& u, const Vector3d& v);
    float& Length(const Vector3d& u);


My question is: Is the & in float& and Vector3d& absolutely necessary? And if so, what is different about float&/Vector3d& as opposed to just float/Vector3d? I've come across it in ray tracing at school but I never fully understood why you have a data type reference.
User is offlineProfile CardPM

Go to the top of the page


Bench
post 8 Aug, 2008 - 09:38 AM
Post #2


D.I.C Addict

Group Icon
Joined: 20 Aug, 2007
Posts: 599



Thanked 8 times

Dream Kudos: 150

Expert In: C/C++

My Contributions


Generally its to prevent copying which would occur if you weren't passing by-reference. Sometimes classes are designed in such a way that objects are not intended to be copied (Generally by making the copy constructor private), so in order to allow the objects to be passed around, references are used instead. Another common issue is that some types may be excessively large (containers for example), and performing a complete copy may be deemed as being a very inefficient operation, especially if the function is to be called frequently - passing by reference would be far more efficient in this case.

If the reference were non-const then it may also be the case that the function has side effects, and modifies one or more of the objects which are passed in.

Incidentally, with an overloaded operator = the first parameter would usually be non-const, since assignment is an operation which modifies the left-hand side of the expression

You should also be a little careful with returning by-reference, that the object you're referring to has a lifetime beyond that of the function - if you return a reference to an object which is local to the function, then you get undefined behaviour
User is offlineProfile CardPM

Go to the top of the page

NickDMax
post 8 Aug, 2008 - 09:49 AM
Post #3


2B||!2B

Group Icon
Joined: 18 Feb, 2007
Posts: 2,856



Thanked 46 times

Dream Kudos: 550
My Contributions


First off: Why use references? There are several reasons.

The first is not really all that important here since these are const referances (implying that the function does not change the argument) But if these were not constant references we may pass an object by referance to ensure that the changes we make stick.

When you create a function: void function(int argument) { ... } this is a "pass by value" function, this means that a copy of the argument is placed onto the stack. When the function return the stack pointer is reset and the data is lost -- i.e. this variable "argument" is local to the scope of the function. So if I want to change the value I need to "pass by referance" which means passing a pointer or reference.

The next reason, more applicable in the functions above:

Objects are often very large. They are often stored in dynamic memory. It just does not make sense to pass these objects on the stack. By using the const keyword we ensure that we don't accidentally change the original values (it also communicates to the user of the function that no manipulation will occur).

This is why I am confused by your operator= declaration there. As far as I know this is not valid, nor even logical. Assignment operators must be non-static member functions. The new C++ specification *may have a non-member assignment operation, but that specification has not been made official yet.

User is offlineProfile CardPM

Go to the top of the page

net nerd865
post 8 Aug, 2008 - 11:53 AM
Post #4


New D.I.C Head

*
Joined: 16 Jun, 2008
Posts: 44



Thanked 2 times
My Contributions


Ok. So basically a data type reference returns a pointer to the data type.

You also are right, the operator= is wrong. Upon closer inspection of my code and what purposes it would serve, I decided to nix the overloaded assignment. It's basically a custom 3d Vector class that I made to help reinforce my understanding of calculus since I have to take it in the fall.

If anyone's interested I'll post the code.
User is offlineProfile CardPM

Go to the top of the page

Bench
post 8 Aug, 2008 - 12:09 PM
Post #5


D.I.C Addict

Group Icon
Joined: 20 Aug, 2007
Posts: 599



Thanked 8 times

Dream Kudos: 150

Expert In: C/C++

My Contributions


QUOTE(net nerd865 @ 8 Aug, 2008 - 08:53 PM) *
Ok. So basically a data type reference returns a pointer to the data type.
Almost, although a closer comparison might be 'const pointer' Although they're not really that either. A reference is like an alias (a nickname) for an object or variable which already exists somewhere else in the program. references allow objects to be directly accessed beyond their usual scope.

Consider this code
cpp
void foo(int& bar)
{
bar = 5;
}

int main()
{
int num = 3;
foo( num );
}
The foo function has direct access to the variable called 'num' - even though the foo function should not even be able to see 'num' - the reference mechanism has caused the compiler to extend the visibility of the object owned by 'num' to the foo function.

Conceptually, its the same idea as a pointer, the big difference is that pointers in C++ are objects themselves, who's values can change at runtime just like any normal object. A reference has no size of its own, and is set-in-stone (Which can get tricky if you attempt to mix references with dynamically allocated memory, since its still possible to have a dangling reference)

This post has been edited by Bench: 8 Aug, 2008 - 12:35 PM
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/20/08 02:58AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month