C++/Re:

Advertisement


Question
QUESTION: Hi Zelatko,
You didn't answer that in this program Where and How can I use reference variables?

Regards.

ANSWER: Hello Tina

Sorry that I missed your question. You can use references in many places. When passing parameters to functions you can pass by reference, instead of passing a copy. When you pass by reference, changes made to the parameter actually change the variable being passed in. I need to show you with an example. Here are 2 functions and a main

void passByCopy(int x)
{
  x = x + 1;
}

void passByRef(int &x) // The & character means that x is a reference
{
  x = x + 1;
}

main()
{
  int a;
  a = 0;

  passByCopy(a);
  passByRef(a);

}

After the call to passByCopy, the variable a still has 0 because when passByCopy is called, a copy of 'a' is made and passed to it. The original is not touched.

After the call to passByRef, the variable a has 1. It has been modified by passByRef because an actual "handle" to the variable was passed in.

Passing by reference is done when you want a function to make a change to the variable, or when passing structures or classes. Often structures and classes are large, so you don't want to make copies when passing them to functions. Pass by reference is much like passing by pointer, with the advantage that a reference can never be NULL.

Let me know if you have more questions.

Best regards
Zlatko

---------- FOLLOW-UP ----------

QUESTION: Hi Zlatko,
Thanks for your answer.In my program I've used some functions and so many variables and I don't know:

. which one(variable) should be declare as reference variable.The parameters in all functions should be declare as reference?
. How to initialize reference variable in main program?(I don't have any speciall value ,like above example :int a , a=0)
Please give me an exemple in my program.


Regards,
Tina

Answer
Hello Tina

The general rule is that simple types (like int, float, char, ...) should be passed by value and not by reference unless the function needs to make a change to those values for the caller to see.

In my example earlier, the caller was main. The function passByRef had the job of changing the variable passed in so that the caller could see the change, so the function took a reference to the variable, instead of the value of the variable.

The other general rule is that objects (like string) are passed by reference, and not by value. That is because passing an object by value causes a copy to be made, and later that copy is destroyed. Often making a copy takes extra time and sometimes can actually cause errors. The problems with making a copy depend on just what the object does. A function can show that it will not change the variable being referenced by declaring the reference to be const.

With these general rules, we can decide how to declare the functions in your program.

The correctdate functon needs no parameters, and returns a string, so it can be declared like this
string correctdate(void);

The following functions take only simple types, so they are good.
bool leapyear(int year);
int daysInMonth(int month, int year );
int dayFrom1900(int year, int month, int days);

The days function takes strings, and does calculations based on the strings, but should not be changing the strings. As I mentioned in other messages, the idea if putting in spaces into the datum1 and datum2 so that they can be parsed is not good.
datum1[4] =' '; <---- this is wrong
datum1[7] =' '; <---- this is wrong
So the function can take const references to the strings
int days(const string& datum1, const string& datum2);
The "&" character means that the function will get a reference to the variable being passed in.

Your second question is:
"How to initialize reference variable in main program?(I don't have any special value ,like above example :int a , a=0)"

The variable "a" is not a reference variable, it is just a variable. It may be initialized or it may not be. The reference is made automatically when you pass the variable to a function that takes a reference. All you have to do is declare the function as taking a reference, and when you pass "a" to it, the function will get a reference to "a". The function is in control of how the variable will be passed, not the caller. As I said, the variable does not need to be initialized. It may be the job of the function to initialize the variable.

I think you should send me the latest version of your program so that I can check it for you.


Best regards
Zlatko

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2012 About.com, a part of The New York Times Company. All rights reserved.