Re: ? Best Way to Set/Initialize Multiple Parameters
"Alec S." <nospam@127.0.0.1> wrote in message
news:OqGFZErUJHA.6060@TK2MSFTNGP06.phx.gbl...
Alan Carre wrote (in news:eP9v1gqUJHA.5344@TK2MSFTNGP06.phx.gbl):
Over time I've come to the following "standard" for simple types:
class MyClass
{
struct tagMems
{.
That seems to be a kind of hybrid of the two methods and looks pretty
good. I
can imagine leaving the protected/private members as is and putting public
variables in a structure and using a flag to indicate valid fields.
The problem with a structure however is when adding new members because
struct foobar {int a; int b;};
is not the same as
struct foobar {int a; int b; CString c;};
I'd rather not resort to
struct foobar {int a; int b;};
struct foobar2 {foobar 1; CString c;};
No, and you shouldn't. The 'm' struct I was referring to contained only POD
(plain old data) objects. Anything more complicated would have it's own
constructor anyway so you have no need to initialize them. I'm only talking
about the headache of adding new bools and ints etc which means going to the
constructor(s) and adding yet another ", m_bBoolNumber60(false)"... [which
you'll forget to do now and again] and the problem of remembering what the
hell you called it in the first place!
I mean you could add a second struct just for easy name lookups and
grouping, but I never seem to find I need to do that. Usually I have a great
big struct full of bools and counters and floats, and just a few more
complex objects (like a CString or something). But if you find you have lots
of non-trivial members then by all means, add a new struct to keep them
together (but seperate from your PODs).
Note as well that creating a copy operator/constructor becomes a piece of
cake with "m":
MyClass::MyClass(const MyClass& mc) {m=mc.m; (plus extras);}
MyClass& operator= (const MyClass& mc) {m = mc.m; (plus extras);}
So when you add a new member and it automatically winds up in the copy
operator/constructor(s). The point is you don't have to *remember* to add
stuff to your copying funcs or your constructors. That means fewer mistakes
and less work. ++ so to speak ;)
- Alan Carre