3 ms·
Without references there would be tons of problems. How would you implement a copy constructor or copy assignment operator without them? Those two things are n
by Buge 12y ago
Without references there would be tons of problems.
How would you implement a copy constructor or copy assignment operator without them? Those two things are necessary for user defined types to be able to be treated like built in types, which is necessary if you want templates to be fully generic, which is essential for all the STL containers.
- userbinator 12y agoGiven that references are implemented with pointers, those special functions are really taking pointer parameters anyway. Syntax-wise things might look a little different, but I don't think it poses much of a real problem.
- Buge 12y agoBut you lose the constant syntax that allows STL containers to work. vector<int> and vector<Person> work because vector can be templated, and int i1; int i2 = i1; Person p1; Person p2 = p1; both have the same syntax. If suddenly p2 = p1 no longer compiles and you have to do p2 = &p1 that looks really weird because the two sides of the = have different types and vector<Person> no longer will compile. On the other hand if we say all overloaded operators automatically convert operands to their addresses, then we are sort of back where we started with references: you can modify a variable inside a function without having to say &. Only this would be much less flexible than what we have now because sometimes you want an overloaded operator to take something by value, but wouldn't be able to do it.