Re: Most efficient way to access a large member struct from another
class
On 4 Jan, 21:45, James Kanze <james.ka...@gmail.com> wrote:
On Jan 3, 6:33 pm, Angus <anguscom...@gmail.com> wrote:
On 3 Jan, 18:20, "Leigh Johnston" <le...@i42.co.uk> wrote:
Simply dereferencing a pointer does not copy the object it points to.
struct foo
{
void mf() {}
};
foo* f = new foo;
f->mf(); // no copy
foo f2 = *f; // copies
Is this the way to do it?
const BigStruct& getBigStruct() const { return m_struct; }
Maybe. Before worrying about performance, you should worry
about semantics. Using a reference instead of a value has
different semantics. In the case of passing a const reference
rather than a value, the difference is generally insignificant
in the context of a function call, and can be ignored. (Whence
the frequent recommendation to pass class types by const
reference, rather than by value.) In the case of a return
value, this may sometimes be true as well, but it's far from
obvious, and you really have to think about what the desired
semantics are.
--
James Kanze
The scenario is as follows:
1. class contacts which contains a list of contacts (rows) obtained
via an arcane interface to a legacy system.
2. class notifications which receives notifications about changes to
contacts. Eg adds/changes/deletes.
3. class notifications has a member pointer to contacts.
4. When an add notification is received, then the new contact must be
added to the rows member of contacts.
So what I do is call a function on contacts which returns a reference
to rows and add the new row (from within notifications). But thinking
about it now it might have been more logical to simply call a
AddContact function on contacts.
I would be interested to know what you think.