Constructor argument writing style?

Started by
5 comments, last by Maxamor 16 years, 5 months ago
Hi, i was wondering what style is usually used for constructor arguments and for what reason. I usually do something like:

class A{
 int a;
 int b;
public:
 A(int _a, int _b){
  a=_a;
  b=_b;
 }
}

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. But i've also seen things like:

class A{
 int a;
 int b;
public:
 A(int a, int b){
  this.a=a;
  this.b=b;
 }
}

Which seems kind of dangerous at first glance. Is there a generally accepted way of doing this? Thanks
Advertisement
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).
Quote:Original post by jykSince this is a pointer (effectively), it would actually be:

Right, sometimes mix Java and C++ a little :).

Anyway, thanks, i didn't know using initializer lists by doing a(a) was ok.
Quote:Original post by Tree Penguin
But i've also seen things like:

*** Source Snippet Removed ***

Which seems kind of dangerous at first glance.


Notwithstanding what was said about initializer lists, not only is this not "kind of dangerous" at all, it's exactly why the 'this' keyword exists. (In fact, the only places I can think of where you "need to be careful about using 'this'" are places where it's helpful *to* use it even though you would expect to be able to omit it.)
Quote:Original post by Zahlman
Notwithstanding what was said about initializer lists, not only is this not "kind of dangerous" at all, it's exactly why the 'this' keyword exists.

Well, actually the existence of the this keyword is an implementation artifact from CPre, the compiler for C with Classes, the language that would eventually become C++. CPre would compile the class member functions into functions with the first parameter being a pointer to a struct, and that parameter was called this. It was made into a keyword when Stu Feldman pointed out that a programmer couldn't actually refer to the this parameter, which was necessary for things like making linked lists in member functions. As such, the addition of this to the language predates things like modern C++ variable scoping rules.
... ;_;
This has already been touched on, but it was something I wondered about to coming from a Java background...

[10.7] Should you use the this pointer in the constructor?

This topic is closed to new replies.

Advertisement