1
#include<iostream>
struct emp
{
    char name[20];
    int age;
    float sal;
};
emp e1={"Amol",21,2345.00};
emp e2={"Ajay",23,4500.75};
int main()
{
     emp &fun();
     fun()=e2;
     std::cout<<std::endl<<e1.name<<" "<<e1.age<<" "<<e1.sal<<std::endl;
     return 0;
}
emp &fun()
{
     std::cout<<std::endl<<e1.name<<" "<<e1.age<<" "<<e1.sal<<std::endl;
     return e1;
}

I don't understand the working of the above example, is this same as &b = a? How is the value of e2 being passed to &fun()?

walnut
  • 21,629
  • 4
  • 23
  • 59
Deepak Pawade
  • 150
  • 1
  • 13
  • Your program is ill-formed and should not compile: 1. `main` must always have a return type and it must be `int` 2. `cout` and `endl` are not imported from the `std` namespace. You need to write `std::cout` and `std::endl` instead or import them with `using std::cout;` and `using std::endl`. Ill-formedness aside: It strikes me as very bad style to forward-declare a function inside `main` rather than the global scope. Using global variables is also bad style and `name` should better be of type `std::string`. – walnut Dec 08 '19 at 23:40
  • `emp &fun()` is a function taking no argument and returning a reference to an `emp` instance. See e.g. [What are the differences between a pointer variable and a reference variable in C++?](https://stackoverflow.com/questions/57483/what-are-the-differences-between-a-pointer-variable-and-a-reference-variable-in) for explanation of what references are. If that doesn't answer your question, please specify what exactly you are confused about. – walnut Dec 08 '19 at 23:44
  • `using namespace std;` is not required if you qualify all uses with `std::`. Putting it before any `#include` statement is also bad style. It may cause name lookup issues with the headers that don't expect `std` to be imported. Btw.,, while `main` must have return type `int`, it does not require a `return` statement. If not given the returned value will default to `0`. (It is a special rule applying only to `main`.) – walnut Dec 08 '19 at 23:45
  • @walnut sorry of the ill-formedness. I directly copied the code that the lecturer gave me.That's how they teach ,so I have to be on stackoverflow for any doubts. I've edited the code. Excluding the declaration of function inside main and not using std::string for name, can you please tell me exactly what's happening inside the main function? – Deepak Pawade Dec 08 '19 at 23:50
  • @walnut Edit: thanks – Deepak Pawade Dec 08 '19 at 23:51
  • The code your lecturer gave you should not compile on any compiler following standard C++. The first C++ standard was published in 98, so what you are being taught is outdated since at least 20 years. If you cannot do anything about this, then I suggest you take a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to learn *actual C++* on the side. – walnut Dec 08 '19 at 23:52
  • They still use turbo c++ here which is very much outdated. A few of the concepts they teach are outdated too and they don't even bother to update their own knowledge. – Deepak Pawade Dec 08 '19 at 23:56
  • That is very unfortunate. As for the question, an answer was already posted and I gave you a link explaining what references are. If you need to know more I suggest you ask a more specific question and start reading one of the books I mentioned above. They should teach you what *references* are and how they work. – walnut Dec 08 '19 at 23:58
  • @walnut that's pretty much typical for anywhere and any country which isn't one of four big tech hubs in USA. In some countries it related to problems of even getting an approval for updating the lectures. Ideology behind CS began pacing too fast. – Swift - Friday Pie Dec 09 '19 at 00:07
  • @Swift-FridayPie I am not located in the US and while I see problems with e.g. teaching pre-C++11, I have not yet seen pre-standard C++ being taught. I do recognize though, that this may not be the same elsewhere. It is still unfortunate though, even if there isn't much one can do about it. – walnut Dec 09 '19 at 00:19
  • @DeepakPawade: For your next question I suggest you add the `turbo-c++` tag, then you might avoid the annoying comments such as mine, at least to some degree. – walnut Dec 09 '19 at 00:21
  • @Swift-FridayPie your comment wasn't annoying , it was informative and correct. Thanks for the glvalue and lvalue references which are fairly new to me. – Deepak Pawade Dec 09 '19 at 00:27

1 Answers1

3

Note: this is example of rather convoluted style of coding

First line of main() is a forward declaration of function fun which returns a reference to an object of emp. So expression fun() is treated as a glvalue, and it can be on left side of assignment, or be target of any other operation requiring an lvalue.

fun returns a reference to e1, so assignment is semantically equivalent to e1 = e2; preceded by side-effect of serializing values of e1's members to cout, as definition of fun() states.

Swift - Friday Pie
  • 12,777
  • 2
  • 19
  • 42