在C++中通常用某种前缀命名成员变量来表示它们是成员变量而不是局部变量或参数.如果你来自MFC背景,你可能会使用m_foo.我myFoo偶尔也见过.
C#(或者可能只是.NET)似乎建议只使用下划线,如_foo.这是否允许C++标准?
我有以下程序,其中两个变量将通过引用传递给函数,其中它们的值将在返回之前基于外部因素确定,main()以便它们可以被其他函数使用.我试图传递的第一个变量是一个int,这很好,但另一个是一个字符串数组,这引起了一些问题.
我已经对此做了足够的研究,知道你不能有一个数组或参考(虽然我还没弄清楚为什么),我想知道是否有人可以帮助我弄清楚如何做到这一点?我尝试过的各种方法都产生了segmentation faults.
注意:下面的代码让数组按值传递,因为我只是不知道要为它写什么.
更新:我需要在课程中使用数组.其他一些数据结构,例如vector已经提出的数据结构,会很棒,但我必须使用特定的结构.
void initialise_existing_devices(int& no_of_existing_devices, string existing_devices[100]);
int main()
{
int no_of_existing_devices = 0;
string existing_devices[100];
initialise_existing_devices(no_of_existing_devices, existing_devices[100]);
}
void initialise_existing_devices(int& no_of_existing_devices, string existing_devices[100])
{
string line;
ifstream DeviceList;
DeviceList.open("devices/device_list");
while (true)
{
getline(DeviceList, line, '\n');
if (DeviceList.eof())
{
break;
}
++ no_of_existing_devices;
}
DeviceList.close();
DeviceList.open("devices/device_list");
for (int i = 0; i < no_of_existing_devices; i ++)
{
getline(DeviceList, line, '\n');
existing_devices[i] = line;
}
}
Run Code Online (Sandbox Code Playgroud)