Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


References vs. Pointers

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
WafflesFTW
Expert Cheater
Reputation: 0

Joined: 21 Mar 2008
Posts: 131

PostPosted: Fri Apr 04, 2008 3:44 pm    Post subject: References vs. Pointers Reply with quote

What are the advantages of using references? It seems pointers can carry out the same functions and more.
Back to top
View user's profile Send private message AIM Address
oib111
I post too much
Reputation: 0

Joined: 02 Apr 2007
Posts: 2947
Location: you wanna know why?

PostPosted: Fri Apr 04, 2008 5:50 pm    Post subject: Reply with quote

Well, there are times to use references and there are time to use pointers. Take the following line of code using pointers.

Code:

//I take no credit for this program
#include <iostream>

using namespace std;

void swap(int *x, int *y);

int main()
{
   int x=5, y=10;
   
   cout<<"Main. Before swap, x: "<< x <<" y: "<< y <<endl;
   swap(&x, &y);
   cout<<"Main. After swap, x: "<< x <<" y: "<< y <<endl;
   return 0;

void swap(int *px, int *py)
{
   int temp;
   
   cout<<"Swap. Before swap, *px: "<< *px <<" *py: "<< *py <<endl;
   temp = *px;
   *px = *py;
   *py = temp;
   cout<<"Swap. After swap, *px: "<< *px <<" *py: "<< *py <<endl;
}


This can be done, but it is more efficient to do it with references. As with this code.

Code:

//I take no credit for this program
#include <iostream>

using namespace std;

void swap(int &x, int &y);

int main()
{
   int x=5, y=10;
   
   cout<<"Main. Before swap, x: "<< x <<" y: "<< y <<endl;
   swap(x, y);
   cout<<"Main. After swap, x: "<< x <<" y: "<< y <<endl;
   return 0;

void swap(int &rx, int &ry)
{
   int temp;
   
   cout<<"Swap. Before swap, rx: "<< rx <<" ry: "<< ry <<endl;
   temp = rx;
   rx = ry;
   ry = temp;
   cout<<"Swap. After swap, rx: "<< rx <<" ry: "<< ry <<endl;
}

_________________


8D wrote:

cigs dont make people high, which weed does, which causes them to do bad stuff. like killing
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
WafflesFTW
Expert Cheater
Reputation: 0

Joined: 21 Mar 2008
Posts: 131

PostPosted: Fri Apr 04, 2008 7:56 pm    Post subject: Reply with quote

How exactly is it more efficient? I understand how both work, but everything that can be used through references can be used by pointers as well. It seems that references have no extra benefits whereas pointers can do more.
Back to top
View user's profile Send private message AIM Address
oib111
I post too much
Reputation: 0

Joined: 02 Apr 2007
Posts: 2947
Location: you wanna know why?

PostPosted: Fri Apr 04, 2008 9:37 pm    Post subject: Reply with quote

Functions can only return one value right. Well to overcome that you can use use references or pointers. But the thing is, is you are either pass by reference using a pointer, or you pass by reference using a reference. You also have to dereference the pointers within swap, which makes it error-prone. For example, if you don't dereference the pointer, the compiler will still let you assign an integer to the pointer, but you will get an addressing error. It's also harder to read. And finally, when using pointers, the way swap works is very apparent to the users.
_________________


8D wrote:

cigs dont make people high, which weed does, which causes them to do bad stuff. like killing
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
WafflesFTW
Expert Cheater
Reputation: 0

Joined: 21 Mar 2008
Posts: 131

PostPosted: Sat Apr 05, 2008 6:48 am    Post subject: Reply with quote

So the difference is basically cosmetic?
Back to top
View user's profile Send private message AIM Address
Robotex
Master Cheater
Reputation: 0

Joined: 05 Sep 2006
Posts: 378
Location: The pizza country!

PostPosted: Sat Apr 05, 2008 7:04 am    Post subject: Reply with quote

Use references whenever possible.
Pointers are usefull when they may be NULL, or you need to change them later

In assembly, the pointers are copied to the stack, so any modification won't affect the original pointer, while references puts the pointers directly to the stack

_________________

ASM/C++ Coder
Project Speranza lead developer
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites