int x_ 和 int x 在C++中有什么区别
What is the difference between int x_ and int x in C++
class Stack{
public:
char data_[100];
int top_;
};
class Stack{
public:
char data[100];
int top;
};
以上两类有什么区别?当我使用变量名称类似于int top_的类时,堆栈操作运行良好,但是当我使用带有变量int top的类时,会弹出这样的错误: 错误:成员使用无效(您是否忘记了"&"? 错误:与以前的声明冲突。 _(下划线(在此代码中的作用是什么?为什么它会产生如此大的差异?
#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();
}
}
_(下划线( 在此代码中的角色是什么?
它使top
和top_
2 个不同的标识符。就像你可以让它top1
或topFOOBAR
一样。
为什么它会产生如此大的差异?
当您在此处使用top
for 成员时,您也与名为top
的方法发生冲突。将top
更改为top_
或top1
会使该冲突消失 - 您的类中不能有 2 个不同的东西具有相同的名称。
有些人习惯于给成员变量起特殊名称,比如m_member
或member_
甚至_member
(最后一个不安全,但有时仍然使用(。这种修饰允许读者看到代码在一侧处理成员 var,并避免像您在另一端那样发生名称冲突。
相关文章:
- int(c) 和 c-'0' 之间的区别。C++
- 向量 <int> a {N, 0} 和 int arr a[N] = {0} 的时间复杂度有什么区别
- 结构体 S { int align; } 之间的区别;(struct 关键字后的名称)和 struct { int al
- 在 C 和 C++ 中作为函数参数,int **a 和 int a[][] 之间有什么确切的区别
- 在 C/C++ 中将数组作为形式参数作为 int arr[] 和 int arr[N] 传递的区别
- int x_ 和 int x 在C++中有什么区别
- 无符号长整型和无符号 int 之间有什么区别,这 2 种类型应该如何在 c# 中封送?
- int (%d) 和 %.0lf 之间有什么区别?
- 打印一个带有静态 int 的函数,有一个 std::cout 和多个 std::cout 有什么区别?
- 有什么区别 - 常量 int x = 5000;和常量整数 x = 50'00;在C++?
- 杀死幻数:"const int" vs "constexpr int"(或者最后没有区别)
- uniform_int_distribution <>和 <int>有什么区别?
- “int*[1]”和“int(*)[1]”有什么区别
- std::vector<int> 和 std::vector* 有什么区别<int>?
- "auto x = vector<int>()"和"vector<int> x"有什么区别?
- int '*p = new int (5);' 和 'int *p = new int[5];' 有什么区别?
- C++ 中的 "int a, b =0" 和 "int a =0; int b = 0" 有什么区别?
- const int const&和const int&in C++有什么区别?
- "long int a=2"和"int a=2L"有什么区别?
- "long"和"long int"以及"long long"与"long long int"有什么区别