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?
#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
}
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.
@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.
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").
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.
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 :)