Jump to content
  • Advertisement
Sign in to follow this  
eedok

Do I have to break this java habit when using C++?

This topic is 4624 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

Well I have a habit in java, which is using the same variable names as arguments to a function/contructor as the class has(sorry if I'm not saying it right), here's a simple example of what I'm trying to do:
class Date
{
        int d;
public:
        Date (int d);
};

int main()
{
        Date  date = Date(1);
        return 0;
}

Date::Date(int d)
{
        this.d=d;
}

what I'm trying to do is set the member of Date called d to the d passed to the constructor, this gives me the error: date.cpp:16: error: request for member 'd' in 'this', which is of non-class type 'Date* const' Is there a way to do this, or will I have to completely break this habit?

Share this post


Link to post
Share on other sites
Advertisement
You would use "this->d" instead of "this.d"; but it's still a trifle unclear to the casual reader what you mean. It's generally a good idea to use different names for arguments than for member variables for the sake of clarity.

EDIT: Beaten to the fruit-flavored alcoholic beverage, as usual.

Share this post


Link to post
Share on other sites
Of course when using anything other than a builtin type you're better off using the initialiser list:
class Date
{

int d;

public:

Date(int d);

};

int main()
{
Date date(1);
return 0;
}

Date::Date(int d)
:
d(d)
{
}

Enigma

Share this post


Link to post
Share on other sites
Quote:
Original post by TDragon
EDIT: Beaten to the fruit-flavored alcoholic beverage, as usual.


"SiCrane" is a fruity beverage?? Google doesn't say anything.

Feeble attempt to justify this post: As you can probably tell from the ->, in C++ this is a pointer to the object instead of a reference as is the case in Java.

Share this post


Link to post
Share on other sites
Quote:
Original post by load_bitmap_file
Quote:
Original post by TDragon
EDIT: Beaten to the fruit-flavored alcoholic beverage, as usual.


"SiCrane" is a fruity beverage?? Google doesn't say anything.


I understood it as a general state of affairs.



jfl.

Share this post


Link to post
Share on other sites
punch
n.
A beverage of fruit juices and sometimes a soft drink or carbonated water, often spiced and mixed with a wine or liquor base.

I was beaten to the punch.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!