It is currently Sun Jun 25, 2017 12:12 pm

All times are UTC - 5 hours




 Page 1 of 1 [ 4 posts ] 
Author Message
 Post subject: GameDev VMK 25 - Texture Transforms
PostPosted: Sun Feb 11, 2007 12:19 am 

Joined: Wed Feb 14, 2007 10:48 am
Posts: 7
Here is a snippet of code:
<code>#include <iostream>
using namespace std;

int strLength(char *str, int &strL); /*referencing operator*/

void main()
{
  int strL;
  strLength("Frank", strL);
  cout << strL << endl;
}

int strLength(char *str, int &strL)
{
  int length = 0;
  while (str != '0')
    length++;

  strL = length;
  return length;
}
</code>I was practicing using pointers and referencing operators. I think I understand why the referencing operator works in the function declaration, but I wanted to get another explanation because I'm still not 100% clear on what it's doing. Also, what are some advantages to using this method? Any other examples would be great too. Thanks!
 


Offline
 Profile  
 
 Post subject: Re: referencing operators
PostPosted: Sun Feb 11, 2007 10:21 am 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1094
Location: Ontario Canada
Two things I would like to say about the code snippet, before I talk about referencing operators. Since the function is already returning an integer value you really don't need to pass in your second parameter. You could re-write your function as the following and still get the same results:
<code>
int strLength(char *str) {
  int length = 0;
  while (str != '0')
    length++;

  return length;
}

void main() {
  int strL;
  strL=strLength("Frank");
  cout << strL << endl;
}</code>
Also if you are trying to get the length of a string, you can use a built in C++ function like this:
<code>#include <string.h>
void main() {
  int strL;
  strL = strlen("Frank");
  cout << strL << endl;
}</code>
However if you just set this code up to get familiar with referencing pointers then see below, and forget my comments above.

A referencing operator is used to allow a function to "return" a value. As you may know already, when you create a function such as:
int FuncName(float fParam);

The first part (int) indicates what the function will be returning an integer value when the function is done.

There is another way to return values from a function without using this method, and that is with referencing operators.

The advantage of using the reference method is that you are allowed to return as many items as you like from your function. With the first method, you can only return one value/class type. This is handy if your function does a lot of work and needs to return all the results.

one thing to also note, is that when you are passing parameters in by reference, you are really using a pointer in a fancy way. You could re-write the code above to the following and it would work exactly the same way:
<code>
void strLength(char *pStr, int *pStrLen) {
  int length = 0;
  while (pStr != '0')
    length++;

  (*pStrLen) = length; /* assigns the length to the value of where ever this pointer is pointer to */

}

void main() {
  int strL;
  strLength("Frank", &strL);
  cout << strL << endl;
}
</code>


Offline
 Profile  
 
 Post subject: Re: referencing operators
PostPosted: Wed Feb 14, 2007 9:05 pm 

Joined: Wed Feb 14, 2007 10:48 am
Posts: 7
So when you use the reference operator as an argument you're not passing a value but rather a spot in memory that you could store a value? I think your pointer example makes a little more sense to me. In the function definition you have a pointer as an argument called pStrLen, and in the function you store the value of whatever the function does (increment length by 1) into this pointer. Then, in main when you use this function you pass a reference operator (does it matter what you name it? could it be called the same name as the pointer?) which will store the value of whatever the pointer was pointing at as defined in the function definition. Does that summarize it? Thanks for the help!


Offline
 Profile  
 
 Post subject: Re: referencing operators
PostPosted: Thu Feb 15, 2007 9:15 am 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1094
Location: Ontario Canada
Yup that pretty much summarizes it.  People like to use "pass by reference" using the & sign in the function definition because then in the body of their function they can access the variable using the regular "." dot technique.  ie myPlayer.m_fHealth;  as opposed to using pointers ie myPlayer->m_fHealth;
Passing a pointer or passing a reference results in the same thing though.


Offline
 Profile  
 
Display posts from previous:  Sort by  
 Page 1 of 1 [ 4 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


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

Jump to:  

cron