SEAL Forum Index SEAL
The SEAL Forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

C++ question

 
Post new topic   Reply to topic    SEAL Forum Index -> Programming Related
View previous topic :: View next topic  
Author Message
tobbe_sn



Joined: 01 Dec 2001
Posts: 97
Location: Sweden

PostPosted: Sun Jun 09, 2002 1:57 am    Post subject: C++ question Reply with quote

So, I'm learning C++ and there's quite a lot of new useful stuff compared to C. I have one question though that maybe someone can answer for me:

What is the difference between a reference variable and a pointer? I don't really get it...
_________________
Tobias Johansson
tobbe_snodd@hotmail.com

"Killers are quiet..."
Back to top
View user's profile Send private message Send e-mail Visit poster's website
orudge
Administrator


Joined: 07 Oct 2001
Posts: 1332
Location: United Kingdom

PostPosted: Sun Jun 09, 2002 4:40 am    Post subject: Reply with quote

Erm... what's a reference variable? My knowledge of C++ isn't that advanced...
_________________
Owen Rudge
http://www.owenrudge.net/

Currently Playing (last time I was online, anyway):
Back to top
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger
_xduffy_
Administrator


Joined: 15 Mar 2002
Posts: 894
Location: Sweden

PostPosted: Sun Jun 09, 2002 4:41 am    Post subject: Reply with quote

The difference is more about how the variable is treated later on in the function...

int hoopla(t_view &view1, t_view *view2)
{
view1.draw(&view1);
view2->draw(view2);
}


A pointer takes less cpu to handle (and memory) since the pointer only contains an adress. When you send a reference you do it because you want to change the actual variable and not only copy it into another.
But it will still be an variable when you use it in the function...

I can clearify in swedish if this is nonsense for you
_________________
http://xduffystuff.sourceforge.net/Desktop/
www.xduffy.com
Back to top
View user's profile Send private message Send e-mail Visit poster's website MSN Messenger
_xduffy_
Administrator


Joined: 15 Mar 2002
Posts: 894
Location: Sweden

PostPosted: Sun Jun 09, 2002 4:43 am    Post subject: Reply with quote

You know... a refernece variable is just an variable... but you send the adress....

Let me illustrate:
int test(int &ref, int normal)
{
ref /= 2;
normal /= 2;
return true;
}

int main()
{
int a = 100;
int b = 100;
test(a, b);
cout << a << "\n";
cout << b << "\n";
return true;
}

The output of this example will be:
50
100

Get the point?
_________________
http://xduffystuff.sourceforge.net/Desktop/
www.xduffy.com
Back to top
View user's profile Send private message Send e-mail Visit poster's website MSN Messenger
orudge
Administrator


Joined: 07 Oct 2001
Posts: 1332
Location: United Kingdom

PostPosted: Sun Jun 09, 2002 5:04 am    Post subject: Reply with quote

Oh yes, I've come across that, just didn't know it was called that.
_________________
Owen Rudge
http://www.owenrudge.net/

Currently Playing (last time I was online, anyway):
Back to top
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger
tobbe_sn



Joined: 01 Dec 2001
Posts: 97
Location: Sweden

PostPosted: Sun Jun 09, 2002 5:19 am    Post subject: Reply with quote

Yes, I think I get it now. Thanx!
_________________
Tobias Johansson
tobbe_snodd@hotmail.com

"Killers are quiet..."
Back to top
View user's profile Send private message Send e-mail Visit poster's website
_xduffy_
Administrator


Joined: 15 Mar 2002
Posts: 894
Location: Sweden

PostPosted: Sun Jun 09, 2002 5:22 am    Post subject: Reply with quote

Good
_________________
http://xduffystuff.sourceforge.net/Desktop/
www.xduffy.com
Back to top
View user's profile Send private message Send e-mail Visit poster's website MSN Messenger
Iamryan2002



Joined: 09 Apr 2002
Posts: 525
Location: San Diego, California United States

PostPosted: Sun Jun 09, 2002 4:20 pm    Post subject: Reply with quote

So basically, you are turning a, b into ref, normal, and then back to a, b?
_________________
HybridOS: Maintainer.

E-Mail: DigitalBliss2003@cs.com
MSN: digitalbliss2003@hotmail.com
AIM: DigitalBliss2003

Questions? Contact Me!
Back to top
View user's profile Send private message Send e-mail AIM Address MSN Messenger
_xduffy_
Administrator


Joined: 15 Mar 2002
Posts: 894
Location: Sweden

PostPosted: Mon Jun 10, 2002 2:20 am    Post subject: Reply with quote

No, ref and a are the very same variable... normal and b are different variables, but normal is a copy of b, and then normal gets changed, but not b.

a get's changes since ref does. And that is because ref and a are the same variable, same place in memory, understand?
_________________
http://xduffystuff.sourceforge.net/Desktop/
www.xduffy.com
Back to top
View user's profile Send private message Send e-mail Visit poster's website MSN Messenger
Iamryan2002



Joined: 09 Apr 2002
Posts: 525
Location: San Diego, California United States

PostPosted: Tue Jun 11, 2002 10:43 am    Post subject: Reply with quote

ohh ok
_________________
HybridOS: Maintainer.

E-Mail: DigitalBliss2003@cs.com
MSN: digitalbliss2003@hotmail.com
AIM: DigitalBliss2003

Questions? Contact Me!
Back to top
View user's profile Send private message Send e-mail AIM Address MSN Messenger
_xduffy_
Administrator


Joined: 15 Mar 2002
Posts: 894
Location: Sweden

PostPosted: Tue Jun 11, 2002 2:03 pm    Post subject: Reply with quote

Very good
_________________
http://xduffystuff.sourceforge.net/Desktop/
www.xduffy.com
Back to top
View user's profile Send private message Send e-mail Visit poster's website MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    SEAL Forum Index -> Programming Related All times are GMT - 7 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


Powered by phpBB © 2001, 2005 phpBB Group