0
class line
{
    public:
        line(int Width)
        : width(Width)
        {
        }

    private:
        int width;
};

I could name Width something like wantedWidth, but I'm just wondering, is there a convention for this or a better way? I don't want to use Hungarian notation.

3
  • @Prasoon: It's no longer possible for the OP to make a question CW. Commented Oct 16, 2010 at 4:19
  • There is no one convention. You can do just about anything to differentiate the two, depending on your preferences. Whatever you do, just make sure you do it consistently. That said, I've had great success with two different styles: prefixing members with m and suffixing members with _. Commented Oct 16, 2010 at 4:21
  • @James : Then this should be closed (as subjective and argumentative), voted to close. Commented Oct 16, 2010 at 4:25

6 Answers 6

1

Yes there's a convention, hundreds of them. Mine is to simply postfix my member names with an underscore.

Sign up to request clarification or add additional context in comments.

Comments

1

There is only one convention.. That is to follow it consistently..

Comments

0

There is no convention. Having arguments with the same name as a member "shadows" the member:

class Foo {
  int mint;
  void hi(int mint) {
    mint = 2; // refers to the parameter
  }
};

I usually just put a _ before the parameter names to prevent clashing. E.g.

void Myclass::memberFunction (int _param1, int _param2)
{
  param1 = _param1;
  member = _param2-3;
}

Make up your own convention if you want.

Comments

0

this->width = width (or in your case Width)

2 Comments

I'm pretty sure I've used "this" before, but I usually just prefix member variables with "m_" and arguments with "a_". And PigBen is correct. Editing.
Yeah, I can't stand this-> everywhere just because all of the member variables and arguments conflict.
0

A common approach is to use "m" to differentiate member variables. This is most commonly used as a prefix, either mWidth or m_width. But another style (used by ASL, the Adobe Source Libraries) conforms more to the standard C++ names such as size_t by using the "m" as a suffix: width_m.

class line
{
    public:
        line(int width)
        : width_m(width)
        {
        }

    private:
        int width_m;
};

Comments

0

I would write this code like this:

class Line
{
public:
    Line( int nWidth )
        : m_nWidth( nWidth )
    {
    }

private:
    int m_nwidth;
};

In my code - m_* stands for member, n* stands for number (if unsigned number - un and so on ..), all classes are named with capital letters, public is always on the top.

I mean, I prefer the Hungarian notation, it's very clean and useful. You could read this article for more information http://en.wikipedia.org/wiki/Hungarian_notation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.