Quote:Original post by Tree Penguin
Hi, i was wondering what style is usually used for constructor arguments and for what reason. I usually do something like:
*** Source Snippet Removed ***
Using underscores to seperate the member variables from the arguments, keeping the same name (instead of an abbreviation) so it's clear what's what.
Typically you would use an initializer list for initialization of member variables, i.e.:
class A{ int a; int b;public: A(int a, int b) : a(a), b(b) {}};
Note that although the arguments and member variables share the same names, the compiler can resolve the ambiguity.
Quote:But i've also seen things like:
*** Source Snippet Removed ***
Which seems kind of dangerous at first glance.
Is there a generally accepted way of doing this?
Thanks
Since this is a pointer (effectively), it would actually be:
A(int a, int b){ this->a = a; this->b = b;}
And it's not at all dangerous (not that I know of at least - there are places where you have to be careful when using this, but the above isn't one of them, at least AFAIK).
Another option is to add a pre- or post-fix to member variable names (e.g. m_myMemberVariable). Some would argue however that this adds redundant information in some contexts (and that therefore the use of this-> should be preferred).