Return by refernce

Hi There :)

I am reading about returning by reference, and I just wonder how this can be useful. Below there is code a bit modified from book.

I have global variable x, and function that changes value of x as it would be variable (ok cool??? hmm, maybe cool) but in original example this function didn't have any arguments so it was just setX(), but then what a use of such function???? so I tried to add integer as argument so now I have setX(int)
and I tried to assign new value in "normal/usual" way, but this seems not providing a result as expected.
any advice?

thanks :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
  #include <iostream>

int x; //global variable
int& setX(int); // function declaration look at & by Int as in same way with pointer!!
//  my modification, original line was int& setX(); 

int main()
{
	
	std::cout << "Value of X B4 Function call: " << x << std::endl; // should this be eaqul to 0?
	setX(0) = 92; // function call on the left side  my modification original value was setX() = 92; 
	std::cout << "X: " << x << std::endl;

	int y = setX(25); // this line is completely mine for purpose of experimenting 

	std::cout << "Y: " << y << std::endl;  // this line is completely mine for purpose of experimenting 


	system("pause");
    return 0;
}
int& setX(int) //  original line is int& setX()
{
	return x; // returns the value to be modified
}
Last edited on
function that changes value of x

No. Your function does not change x.

Return by reference is commonly used to return member variables.
1
2
3
4
5
6
7
8
9
10
class foo
{  
private:
  std::string  bar;

public:
  const std::string & get_bar () const
  {  return bar;
  }
};

Since a reference is returned, a copy is avoided.
Look at your function implementation:int& setX(int)
When you have a parameter without a name in a function implementation it means that the function call must have a value for this parameter but the function will not be using this parameter. If you want to pass a value into a function you need to name the parameter. Also it is usually better to also name the parameter in your prototype.

If your book is teaching using global variables and returning reference to the global variable you should find another book IMO.

Last edited on
this is very bad example to use references.

i agree with @jlb, you should get another book.
now iam reading c++ primer 5th and it's fantastic i recommend it.
you can view this guide : http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list
Last edited on
hi both,

thanks for that :)

@jlb I am reading my Object Orientated Programming in C++ 4th edition. This is quite old book (in one of the screen shoots you can see that console have MS DOS icon :D)
my tutor based his course on this book, I have asked here if anyone could share their experience about this book, but I don't think many people came across it.

On positive side, book does explain many things in very clear way, better in other books I came across.

This example was about global variables and passing by reference, they didn't advocate to use global variables as such, and to give them (authors) credit they do list issues with global variables
This example was about global variables and passing by reference, they didn't advocate to use global variables as such, and to give them (authors) credit they do list issues with global variables

No this was an example about global variables and returning by reference, not passing by reference. And IMO they are advocating the use of bad practices and "trick" features. Returning by reference should really be taught as they cover classes and operator overloading.

On positive side, book does explain many things in very clear way, better in other books I came across.

On the negative side it is using global variables and "trick" features that probably shouldn't be taught till much later, if at all.

This is quite old book

Yes, very old published about 2002. Which is why you probably didn't get many answers to your post about sharing their experience with the book. Even if people were aware of the book they may not remember enough about it to offer advice.

that is what I was worried, i told my tutor that books is very outdated :( sadly I paid money for the course.

in your opinion what should I avoid in this book?
Instead of using an old book, I suggest you to use YouTube where you can visually see how stuff works. There are great tutorials in C++ if you search carefully (not the type of "Learn C++ in 1 hour").
Last edited on
in your opinion what should I avoid in this book?

Since I don't have access to the book all I can go by is the limited preview from the publisher. That limited preview isn't really showing too much content. However I would say that the biggest problem will probably not be what is there, but what is missing. That book will only have coverage of C++98 (the original C++ standard) and possibly portions of C++03, it will not have any references to C++11 and later. C++11 added quite a bit to the language and today it should be the starting point for teaching the language, C++14 would be better but since C++14 only added "minor" changes you can learn about those changes on your own.

Hopefully the standard containers, std::vector, std::list, std::string, etc will be introduced and used before arrays, home grown lists, and C-strings. But that's probably just a dream in a book this old.

Hi jlb somehow I missed this one :/

thanks for this! yes I asked about vector, apparently we will cover it.

At the moment we had example that was using integer called vector it was quite confusing.

I asked my tutor for clarification does he means vector as integer or does he refers to vector class.
But he told me he will cover vector class so feeling a bit better :)
> I am reading about returning by reference, and I just wonder how this can be useful.
1
2
while( hydra.is_alive() )
	hercules.cut_off( hydra.head() );

now imagine that `.head()' returned a copy, then the demigod would be wielding his sword for eternity thinking that the head grows back.
@ne555 :DDDDD very funny who would think 3000 years ago (or when Greek myths where created) that someone will use it to describe how code works ;)
Topic archived. No new replies allowed.