Most efficient way to access a large member struct from another class

A

Angus

I have a class B which is constructed with a pointer to a class A.

If class A is like this:

class A
{
public:
BigStruct getBigStruct() const { return m_struct; }

private:
BigStruct m_struct;
}

And I need to access m_struct from B, and B has a A* (pA) then when I
call:

pA->getBigStruct()->Operation();

What happens here?

As pA is a pointer, we are dereferencing the pointer and the call to
getBigStruct - does that do any copying?

I don't want to copy a laege object around. Should I be returning a
reference? What is best way to do this?

Angus
 
A

Angus

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

/Leigh

Is this the way to do it?

const BigStruct& getBigStruct() const { return m_struct; }
 
C

Carlo Milanesi

Angus said:
Is this the way to do it?

const BigStruct& getBigStruct() const { return m_struct; }

Yes.
In addition, you may also define:
BigStruct& getBigStruct() { return m_struct; }
for when you are going to modify the BigStruct inside B.
 
C

Carlo Milanesi

Angus said:
> Is this the way to do it?
>
> const BigStruct& getBigStruct() const { return m_struct; }

Yes.
In addition, you may also define:
BigStruct& getBigStruct() { return m_struct; }
for when you are going to modify the BigStruct inside A.
 
J

Jonathan Lee

pA->getBigStruct()->Operation();

You could also make Operation() a member of A which calls the
same function on m_struct and return the result.
I don't want to copy a laege object around.  Should I be returning a
reference?  What is best way to do this?

There's also the possibility of making B a friend class.

It might also make sense to write a function of A that "sends"
to B. Something like

void A::transform(B& b) const {
mstruct m2 = b.transform(m_struct);

}

Depending on your classes, some of these may be meaningful
and some not.

--Jonathan
 
J

James Kanze

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.
 
A

Angus

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.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top