Ivan Vecerina wrote:
[color=blue]
> NB: style-wise, you'd actually use a reference rather than a pointer:
> class B {
> public:
> A const& geta() const { return a; }
> private:
> A a;
> };
>[/color]
Still strange to me the diff between A * geta(...) and A & geta(...)...
I think I'll have to check that out. In both cases won't geta just
return a pointer to A, i.e., a block of memory containing the address
of the object itself?
[color=blue][color=green]
> > or
> > class B {
> > public:
> > A * geta() const { return a; }
> > private:
> > A * a;
> > };[/color]
>
> The (corrected) first option has several advantages:
> - the lifetime of the data member a is clearly defined
> (you know it will be constructed at the same time as B, etc)
> - the const-semantics are also explicit (b.a is const if b is const)
> - you'll avoid allocating a separate memory block for
> the A object (possibly a performance issue).
>
> The second approach, however, has the advantage that the definition
> of A does not have to be seen to use class B. It is related to
> the "pimpl" idiom, which is relatively common in C++.
> See
http://search.yahoo.com/search?p=pimpl+idiom
>
>[/color]
Just read about the idiom. Never heard about it before. Interesting but
not why I'm used to return the pointer.
[color=blue]
> Hope this helps,[/color]
Well, helped a lot. Thanks... At least just made think! :)
[color=blue]
> Ivan
> --
>
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact[/color]
form[color=blue]
> Brainbench MVP for C++ <>
http://www.brainbench.com[/color]