C++ / Passing a variable by reference to a class member function.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
C++ / Passing a variable by reference to a class member function.
I would like to start by showing my appreciation for such a site. Thank you.
I am a n00b coder in C++. I haven't been at it long at all and it is my first language other than HTML. With this background in mind I have written this code: (PLEASE DO NOT RUN THIS, It doesn't end. And please remember I submit this in it's current state. I have edited it since the last compile and fear that it would not compile at this time.)
Code:
#include <iostream>
unsigned short int v_current_time = 0;
unsigned short int v_current_date = 0;
unsigned short int v_current_period = 0;
void f_time_cycle();
class c_entity
{
public:
c_entity(unsigned short int v_current_age); // My delima!
~c_entity();
unsigned short int f_return_status();
void f_adjust_age(unsigned short int v_current_age); // My delima!
unsigned short int f_return_age();
bool v_status;
unsigned short int v_current_age;
};
///////////////////////////////////////////////////////////////////////////////
int main()
{
std::cout << "NamelessRPG" << std::endl;
c_entity tester(0);
bool v_current_status = (tester.f_return_status());
while(v_current_status != false)
{
f_time_cycle();
tester.f_adjust_age(tester.v_current_age);
tester.f_return_age();
std::cout << tester.v_current_age << std::endl;
}
return 0;
}
///////////////////////////////////////////////////////////////////////////////
void f_time_cycle()
{
v_current_time++;
if(v_current_time == 50000)
{
v_current_date++;
v_current_time = 0;
}
if(v_current_date==1000)
{
v_current_period++;
v_current_date = 0;
}
std::cout << v_current_time << std::endl;
std::cout << v_current_date << std::endl;
std::cout << v_current_period << std::endl;
}
c_entity::c_entity(unsigned short int v_current_age) // My delima!
{
v_current_age = v_birth_age;
std::cout << "Its ALIVE!!!" << std::endl;
}
c_entity::~c_entity()
{
std::cout << "Its DEAD!!!" << std::endl;
}
unsigned short int c_entity::f_return_status()
{
return (v_status);
}
void c_entity::f_adjust_age(unsigned short int v_current_age) // My delima!
{
v_current_age++;
}
unsigned short int c_entity::f_return_age()
{
return (v_current_age);
}
The goal at this stage is to pass the v_current_age variable into appropriate member functions by reference not value. In its current state the routines do not "age" the entity class object as it should. I have fumbled with trying to use references to pass this variable to the right function by reference instead of value but I cannot seem to get it to work.
I realize that many will probably find much wrong with this code ranging from style to poor use of features. Please ignore these for now as I am not ready to delve beyond references at the moment. I would prefer to get a solid grasp on this before moving to something else.
If anyone could show me the proper way to impliment this I would be very appreciative. Thanks in advance for any and all replies.
You shouldn't have the parameter names the same as your member variable names. Doing so will mask the member variable.
For instance here:
Code:
c_entity::c_entity(unsigned short int v_current_age) // My delima!
{
// Here you are setting the local v_current_age to v_birth_age, not
// the member v_current_age.
// Additionally, v_birth_age hasn't been initialized so you are setting it
// to a garbage variable.
v_current_age = v_birth_age;
std::cout << "Its ALIVE!!!" << std::endl;
}
I think what you want above is actually something like so:
Code:
c_entity::c_entity(unsigned short int current_age) // My delima!
{
v_current_age = v_birth_age = current_age;
std::cout << "Its ALIVE!!!" << std::endl;
}
Now, this isn't passing by reference like you asked for. I'm not sure that passing by reference is really what you want, but in case it is, here's a simple example of how passing a value by reference is used.
Code:
// Pass by reference using the & operator on the paramter
void Double(int &x)
{
// Because x is passed by reference when we change it here, it will be
// changed outside the function as well.
x = x * 2;
}
int main()
{
int y = 10;
// After this call, y will equal 20
Double(y);
return 0;
}
NOTE: Please use the [ code] [ /code] tags in the future. It makes your code much more legible.
Please forgive. I had no knowledge of tag use for this type of board.
As I stated I had been editing the code prior to the post, since the last compile. I was aware of the "garbage variable" but I didn't edit it out prior (it was just something I was working on).
As it stands, when I last ran the program, it simply spit out the age of the entity. But I was getting nothing but zero's as the age through each cycle because the effects of the routine were on the copy of the variable being passed by value. I deduced that I would have to pass the variable by reference for it to impact the actual variable and not just a copy. That is when I became stumped and sought advice.
I greatly appreciate your feedback and am now about to "tinker" (for lack of a better term) with it. I shall post my results.
After my second observation of your reply another question arose. Do I not have to explicitly create a reference to the variable and then pass in the reference or can I just use the address of operator and that serve as a reference to the variable being passed in itself?
OR, is the use of &x just a place holder for y telling it to use a reference to y (or whatever other variable is passed) as opposed to a copy of y when the function executes?
I see now that this was the source of my initial confusion. I'm just not very certain at all how this works outside of general concept.
a c++ reference is an alias for the variable, what this means is it has some of the function of a pointer(but you cannot move it or make it point to anything else) and it doesn't have the overhead of passing by value. to use a reference you just pass the variable name to a func with accepts a ref. does that help?
Using a pointer to a variable is passing by reference in C. For C++, this feature is retained and is still passing by reference, however, with C++, a the approach for "passing by refernence" is declaring the argument a refernce with the ampersand symbol.
There are more differences here. A reference means that the argument is actually the same object as provided by the caller. A pointer to a variable is actually passing an address by value, however, this allows certain flexability that references prohibit. Primarily, you are allowed to provide constant addresses (useful for hard-coded address) and null pointer can indicate an invalid value with the slight overhead of checking against zero.
Code:
void Write( char *address )
{
memcpy( address, "Test", 5 );
}
void Write2( double &value )
{
value = 0.2423;
}
int main()
{
char buffer[32];
Write( &buffer ); // Ok.
Write( 0 ); // <-- Oops!! Null pointer!
Write( 0x03432 ); // <-- Oops!! Unknown address!
double d;
Write2( 0 ); // Error, not a reference!
Write2( &d ); // Error. Address of double (i.e. double* ) is not a double.
Write2( d ); // Ok.
return 0;
}
Both pointers and references avoid copying the referred object. This is usually a good thing, however, that means that the input argument should not be 'trashed' if still needed by the caller.
I understand. Thank you so much. You said more in those 2 lines than I was able to get from my 4 books.
To make sure I understand, if a function is written to accept a reference as the arguement:
Code:
int somefunc(&somevar)
{
......
}
then any variable you actually pass into it is passed by reference? So there is no need to actually make a reference to the desired variable for the sake of passing by reference. It is just a way to tell the funtion to accept the variable by reference not value?
Like:
Code:
somefunc(somevar);
coupled with the above code would pass somevar by reference with no need to create a reference to somevar?
This leads me to another question. In the function prototype, you dont need the name of the actual variable to be passed in, correct? If so, what does the prototype need? I am assuming the variable type?
int main()
{
char buffer[32];
Write( &buffer ); // <-------------------not ok this is a pointer to a pointer
Write( buffer ); //OK
Write( 0 ); // <-- Oops!! Null pointer!
Write( 0x03432 ); // <-- Oops!! Unknown address!
double d;
Write2( 0 ); // Error, not a reference!
Write2( &d ); // Error. Address of double (i.e. double* ) is not a double.
Write2( d ); // Ok.
return 0;
}
then any variable you actually pass into it is passed by reference? So there is no need to actually make a reference to the desired variable for the sake of passing by reference. It is just a way to tell the funtion to accept the variable by reference not value?
correct
[quote]
This leads me to another question. In the function prototype, you dont need the name of the actual variable to be passed in, correct?
Quote:
correct
Quote:
If so, what does the prototype need? I am assuming the variable type?
im not quite sure about this one, maybe someone can verify it
for a normal func which takes an int as a paramter and returns nothing the following is suffisant as a prototype
void foo(int);
for a func which takes a ref to an int and returns nothing
Code:
void foo(int&);
damn smilies
edit--
yes the reference prototype is correct, to be honest i dont use references alot in my code i prefer pointers, maybe this is because i learnt c before c++
Well, I am here to say thank you again. It would seem that I have a somewhat firm grasp on the idea of passing variables to functions by reference now (using references anyway, pointers are another issue but I have used them without too much problem as of yet). The code did exactly what I wanted it to do and aside from a couple of oversights (typo's) compiled without a hitch.
I have also went back and looked at some code examples that previously eluded me and they also make much more sense now. For this I also thank you for the indirect help you gave me.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.