Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Drakkcon

C++ classes help

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

class Integer {
    int i;
    sign s;
  public:
    Integer(int ii = 0) 
      : i(ii),
        s(i >= 0 ? positive : negative)
    {}
    sign getSign() const { return s; }
    void setSign(sign sgn) { s = sgn; }
    // ...
  };
  
what do those single colons mean? In every C++ tutorial I have, it just uses them, and I have no clue what they mean. Specifically the one before i(ii), and after positive. The true general first seeks victory, then seeks battle - Sun Tzu [edited by - Drakkcon on April 14, 2004 5:27:39 PM]

Share this post


Link to post
Share on other sites
Advertisement
The two samples of single colons here both have very diferent meanings.

The first, directly after the contructor is used for initializing base classes and variables. So Integer(int ii = 0) : i(ii) is the equivelant of putting i = ii; in the first line of the contructor, or at least it boils down to the same thing.

The second example, s(i >= 0 ? positive : negative), is used for replacing small if statements.

if (condition){
x = statement_a;
}
else {
x = statement_b;
}


is the same as

x = condition ? statement_a : statement_b;


So what it is doing is setting s to either positive or negative based on the value of i.

Hope that helps. If you need further clarification just ask.

EDIT: May typos burn in hell

[edited by - stro on April 14, 2004 5:34:34 PM]

Share this post


Link to post
Share on other sites
Ah thank you! but I still don''t understand the first colon you mentioned, does it mean that the value of I is doubled in the constructer then?

Share this post


Link to post
Share on other sites
No. The value of i is being set to the value stored in ii which is declared in the paramater list. It would be the same as

Integer (int z = 0) : i(z)

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!