Hi,
consider the following code, VS2005/C++:
class _Bar;
class _Foo {
public:
_Foo() {}
private:
_Bar _member;
};
Using encapsulate field on _member results in the functions
_Bar getMember() const { return _member; }
void setMember(_Bar val) { _member = val; }
below the member.
I have two problems with that:
(1) The encapsulated functions shouldn't be private. I realize that it may not be clear where the implementation should be placed (if there are several public sections), but placing them in private or protected sections will be reliably wrong.
(2) I would prefer passing the parameters as const reference, i.e.
_Bar const& getMember() const { return _member; }
void setMember(_Bar const& val) { _member = val; }
(I do know that I can edit the snipped and I did, but I would still suggest to change the default snippet.).
Apart from the obvious performance issues (although the optimizer should handle most cases) if the copy constructor is expensive, this is risky code if the member is something like a STL container and calling programs try to construct iterators from the result of getMember().
Maybe it would make sense to have two variations of encapsulate field, one that passes parameters as const reference and one that doesn't.
Regards, Thomas