The current code i have right now:
Code:
class main {
public:
std::vector<std::string> &get_vector() { return m_vector; }
const std::vector<std::string> &get_vector() const { return m_vector; }
private:
std::vector<std::string> m_vector;
};
class subclass {
private:
main &m_main;
void somefunction() {
std::vector<std::string>::iterator from = m_main.get_vector().end() - 2;
callsomefunc(std::vector<std::string>(from, m_main.get_vector().end()) ); //call somefunc and pass vector with last 2 elements
}
};
I want to redesign it to:
Code:
class main {
public:
std::vector<std::string> get_last_elements() { return std::vector<std::string>(m_vector.end() - 2, m_vector.end() ); }
private:
std::vector<std::string> m_vector;
};
class subclass {
private:
main &m_main;
void somefunction() {
callsomefunc(m_main.get_last_elements());
}
};
but I have some preformance considerations on second design.. Will it be slower because of passing value directly instead of reference?
Or will my compiler optimise that? Which design should I go for if theres a lot of copying in my application?
Many thanks in advance!