Having read this:
Is the practice of returning a C++ reference variable, evil?
and a few other resources on references, I was wondering if something like this is OK or should I be returning a pointer?
#include <iostream>
class engine
{
public:
int cc;
};
class car
{
protected:
engine eng;
public:
car (void) : eng()
{
eng.cc = 500;
}
const engine& get_engine(void) const
{
return eng;
}
};
int main (void)
{
car c = car();
engine en = c.get_engine();
std::cout << en.cc << std::endl;
}
Also:
What would be the difference if I changed the line
engine en = c.get_engine();
to
engine& en = c.get_engine();
both compile and give the same output but are there any subtle/not-so-subtle differences?
EDIT: as Vlad from Moscow pointed out, that last one should be const engine& en = c.get_engine();
.