tags:

views:

65

answers:

6
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.

A: 

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.

Alexander Rafferty
+1  A: 

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

PigBen
A: 

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

MdaG
`this.width`? Are you sure?
James McNellis
this is a pointer. Should be this->width.
PigBen
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.
MdaG
Yeah, I can't stand this-> everywhere just because all of the member variables and arguments conflict.
Ed Swangren
A: 

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;
};
Jon Reid
A: 

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

liaK
A: 

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.

Kiril Kirov