If I have a class that has many variables int, floatand enumis it considered efficient and / or good practice to return them as links, not copies, and return constant links, where no change should be made? Or is there a reason why I should return them as copies?
int
float
enum
There is no reason to return the primitive types, such as intand floatthe link if you do not want them to be changed. Returning them by reference is actually less efficient because it does not save anything ( intand pointers usually have the same size), while dereferencing actually adds overhead.
If they are permalinks, this is probably normal. If they are not permalinks, perhaps not.
As for efficiency - on a 64-bit machine, links will be 64-bit values (hidden pointers); intand floatthey enumwill be less. If you return the link, you intend to achieve a level of indirection; it is less effective.
, , , .
:
operator[] . . .
operator[]
int &operator[](int index); // by reference int operator[](int index) const; // by value
, , , . . , , , , .
, , "v" - STL.
v.at(1) = 2 vs *(v.at(1)) = 2;
, , . - , , . , , , .
, ( ) , ( ).
, . ints , , , ( ) .
, . ++ var ( )
, , const. , . :
struct SomeClass { std::vector<int> const & SomeInts () const; void AddAnInt (int i); // Adds an integer to the vector of ints. private: std::vector<int> m_someInts; }; bool ShouldIAddThisInt(int i); void F (SomeClass & sc) { auto someInts = sc.SomeInts (); auto end = someInts.end (); for (auto iter = someInts.begin (); iter != end; ++iter) { if (ShouldIAddThisInt(*iter)) { // oops invalidates the iterators sc.AddAnInt (*iter); } } }
Getters Exhaust Car.emit(), Exhaust.
Exhaust Car.emit()
Exhaust
const Seat& Car.get_front_seat()Driver, , - . , Car.get_in_driver(Driver)seat.sit_into(Driver).
const Seat& Car.get_front_seat()
Driver
Car.get_in_driver(Driver)
seat.sit_into(Driver)
, get_front_seat, , . , !:)
get_front_seat
: ( ) , .
: , , .. , , .
: ++, - const ref, , ref, , ref. , . , . .