"Kev" <di**************@pht.zzz> wrote in message
news:Xn**********************@216.168.3.44
"John Carson" <jc****************@netspace.net.au> wrote in
news:daii34 $1*****@otis.netspace.net.au:
void Setabc(Outer &o)
{
Outer::Inner::DoubleInner & shortcut = o.inner.doubleinner;
shortcut.a = 1;
shortcut.b = 2;
shortcut.c = 3;
}
Would this work if Outer could represent more than one classtype, but
sharing the same base? Perhaps using the base classname instead?
Similar to using a base pointer for a derived class? In this case
DoubleInner would be in the base, common to all.
Do you mean the class DoubleInner or the variable doubleinner or both? If
Outer is a base class, say:
struct Derived1 : public Outer
{
// stuff
};
struct Derived2 : public Outer
{
// stuff
}
and we have an instance of a derived class:
Derived1 d1;
then we can call the original function with no change:
Setabc(d1);
All the action is in the base class and the fact that it is derived from is
irrelevant.
The more general point is this: if you can get access to member variables
within the function via a series of objects/pointers:
a.b->c.d.e->f.g.member1
a.b->c.d.e->f.g.member2
a.b->c.d.e->f.g.member3
then you can produce a shortcut with which to access members 1-3 as follows:
A::B::C::D::E::F::G & shortcut = a.b->c.d.e->f.g;
where A is the type of a, B* is the type of b, C is the type of c and so on.
You then call it with
shortcut.member1
shortcut.member2
shortcut.member3
If the last thing in the original chain before the members is a pointer
rather than an object, i.e.,
a.b->c.d.e->f.g->member1
a.b->c.d.e->f.g->member2
a.b->c.d.e->f.g->member3
then the definition of shortcut changes via the addition of an asterisk so
that it becomes a reference to a pointer:
A::B::C::D::E::F::G *& shortcut = a.b->c.d.e->f.g;
(note that pointers earlier in the chain affect the right-hand side of the
assignment, but not the left-hand side; by contrast, if the final thing in
the chain is a pointer, it affects the left-hand side but not the right-hand
side). You use shortcut as follows:
shortcut->member1
shortcut->member2
shortcut->member3
You can also transform a pointer into a reference or a reference into a
pointer. This is left as an exercise for the reader.
--
John Carson