views:

150

answers:

4

While reviewing one of our C++ class through coverity, it showed an error message on a particular class. The class is as follows:

class InputRecord
{
    /* Construtor */
    ...
    InputRecord::RejectRecord();
    ...
    /* Destructor */
}

What is the significance of using an identifier inside the class? Is it a good practice to follow this?

Thanks, Mathew Liju

+2  A: 

G'day,

I always thought that sort of identifier was only used in the implementation and wasn't necessary in the class definition.

A class declaration is

class InputRecord;

What you've got there is a class definition.

class InputRecord
{
    /* Construtor */
    ...
    RejectRecord();
    ...
    /* Destructor */
}

then in your .cpp file you have the implementation

InputRecord::RejectRecord()
{
    ...
}
Rob Wells
Even that is my understanding. Before making any decision on changing the program, I just want to confirm my understanding. Anyway thanks for the reply :-)
Liju Mathew
+2  A: 

In general, there aren't any cases where the disambiguation provided by the explicit InputRecord:: in your code sample are likely to be anything other than redundant.

If the code is doing complex manipulations where the specific class is relevant (say you're passing it to a base class that has a shadowed version), then it may help code clarity to make it explicit.

It's a bit like using this-> (or this. in C#/Java).

Personally I'd remove any such redundant specifiers and find another way of getting the point across.

Ruben Bartelink
Actually I think both C++ and Java should force people to use this for member variables, so we dont have to name them mValue, m_value, m_Value, value_ etc anymore.
Viktor Sehr
We have to name them `m...` ? :P From an Functional Programming perspective you'd like to be foreced to think before using state/ From an OO perspective it woudl be 'normal'. For certain types of things this would look *really* ugly. Some programming 'standards' say no prefixing as it should be clear from the code whether it is a member or param - which is fine if you're writing proper code. (I personally dont use Hungarian Notation but cling to prefixing [with '_'] knowing its wrong. I'd detest having to prefix `this.` everywhere). I think this is a clear case of there being no OSFA solution.
Ruben Bartelink
+7  A: 

On gcc compilers (v4.1 and above) this would fail to compile with "error: extra qualification". It's therefore good practice not to put it there!

See here which discusses the extra qualification as not legal C++.

Jeff Foster
not just gcc, any compiler except visual studio.
caspin
+2  A: 

In the class definition, I only use class identifiers if I have parameters that are named similarly (or the same) to private members of the class. It avoids ambiguity and also makes the code easier to read at the same time. I can't think of another case in which you would need to use it.