Jump to content
  • Advertisement
Sign in to follow this  
Tree Penguin

Constructor argument writing style?

This topic is 4055 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!