0
class Stack{
         public:
            char data_[100];
            int top_;
          };

class Stack{
         public:
            char data[100];
            int top;
          };

What is the difference between the above two classes ? When I am using the class where the variable names are like int top_ ,the stack operations are running fine but when I am using the class with variables int top, errors like this are popping up : error: invalid use of member ( did you forget the '&' ?) error: conflicts with previous declaration. What is the role of the _(underscore) in this code ? Why is it making such a difference ?

#include<iostream>
#include<cstring>
using namespace std;
class Stack{
         public:
            char data[100];
            int top;
            bool empty()
            {
                return (top == -1);
            }
            char top()
            {
                return (data[top]);
            }
            void pop()
            {
               top--;
            }
            void push(char c)
            {
                data[++top] = c;
            }
           };
int main()
{
    Stack s;
    s.top = -1;
    char str[10] = "ABCDEFGH";
    for(int i=0;i<strlen(str);i++)
        s.push(str[i]);
    cout<<str<<endl;
    cout<<"Reversed string is : ";
    while(!s.empty())
    {
        cout<<s.top()<<" ";
        s.pop();
    }

}
  • 7
    There is no difference - your problem lies somewhere else. –  Aug 24 '18 at 16:37
  • Could you show us the code that your definitions are conflicting with? Underscores do not affect the validity of symbol names. – Xirema Aug 24 '18 at 16:38
  • It is the same difference as `int x` and `int y` – Slava Aug 24 '18 at 16:39
  • @Xirema "Underscores do not affect the validity of symbol names" - well, they do, but not in this case. –  Aug 24 '18 at 16:39
  • I have added my code below the question, and it is generating errors, please help me identify what's wrong with it – Satyaki Majumder Aug 24 '18 at 16:43
  • Possible duplicate of [Is using underscore suffix for members beneficial?](https://stackoverflow.com/questions/1630412/is-using-underscore-suffix-for-members-beneficial) – Michał Karpacki Aug 24 '18 at 16:51
  • It is just used to differentiate between two identifiers of same name. – Ruks Aug 24 '18 at 16:53

1 Answers1

5

What is the role of the _(underscore) in this code ?

It makes top and top_ 2 different identifiers. The same way you can make it top1 or topFOOBAR.

Why is it making such a difference ?

When you use top for member here you have a conflict with method named top as well. Changing top to top_ or top1 would make that conflict to disappear - you cannot have 2 different things in your class with the same name.

Some people have a habit to give member variables special names, like m_member or member_ or even _member (last one is not safe but still used sometimes). This decoration allows reader to see that code is dealing with member var on one side and avoiding name collisions like one you had on another.

Slava
  • 43,454
  • 1
  • 47
  • 90
  • 1
    `_member` is safe as long as the identifier is not at the global scope (which it would not be for a class or struct member variable), and that the second character is lowercase. – Eljay Aug 24 '18 at 17:46
  • @Eljay if you have that habit there is chance you will use member with uppercase after underscore or there could be other issues. That's why I said it is unsafe in my opinion, otherwise I would say it is illegal. – Slava Aug 24 '18 at 17:51