I'm having trouble declaring and initializing a char array. It always displays random characters. I created a smaller bit of code to show what I'm trying in my larger program:
我在声明和初始化一个char数组时遇到了麻烦。它总是显示随机字符。我创建了一个更小的代码来展示我在我的大程序中所尝试的:
class test
{
private:
char name[40];
int x;
public:
test();
void display()
{
std::cout<<name<<std::endl;
std::cin>>x;
}
};
test::test()
{
char name [] = "Standard";
}
int main()
{ test *test1 = new test;
test1->display();
}
And sorry if my formatting is bad, I can barely figure out this website let alone how to fix my code :(
抱歉,如果我的格式不好,我几乎不知道这个网站怎么修改我的代码:(
5 个解决方案
#1
9
If there are no particular reasons to not use std::string
, do use std::string
.
如果没有特别的理由不使用std::string,请使用std::string。
But if you really need to initialize that character array member, then:
但如果您确实需要初始化字符数组成员,则:
#include <assert.h>
#include <iostream>
#include <string.h>
using namespace std;
class test
{
private:
char name[40];
int x;
public:
test();
void display() const
{
std::cout<<name<<std::endl;
}
};
test::test()
{
static char const nameData[] = "Standard";
assert( strlen( nameData ) < sizeof( name ) );
strcpy( name, nameData );
}
int main()
{
test().display();
}
#2
6
Your constructor is not setting the member variable name
, it's declaring a local variable. Once the local variable goes out of scope at the end of the constructor, it disappears. Meanwhile the member variable still isn't initialized and is filled with random garbage.
构造函数没有设置成员变量名,而是声明一个局部变量。一旦本地变量在构造函数的末尾超出范围,它就会消失。同时,成员变量仍然没有初始化,并且填充了随机的垃圾。
If you're going to use old-fashioned character arrays you'll also need to use an old-fashioned function like strcpy
to copy into the member variable. If all you want to do is set it to an empty string you can initialize it with name[0] = 0
.
如果要使用老式的字符数组,还需要使用像strcpy这样的老式函数来复制到成员变量中。如果你想做的就是将它设置为一个空字符串,你可以用[0]= 0来初始化它。
#3
5
Considering you tagged the question as C++, you should use std::string
:
考虑到您将问题标记为c++,您应该使用std::string:
#include <string>
class test
{
private:
std::string name;
int x;
public:
test();
void display()
{
std::cout<<name<<std::endl;
std::cin>>x;
}
};
test::test() : name("Standard")
{
}
#4
4
Since you are using C++, I suggest using strings instead of char arrays. Otherwise you'd need to employ strcpy (or friends).
由于您正在使用c++,我建议使用字符串而不是char数组。否则你需要雇佣strcpy(或朋友)。
Also, you forgot to delete the test1 instance.
此外,您还忘记删除test1实例。
#include <iostream>
#include <string>
class test
{
private:
std::string name;
int x;
public:
test();
void display()
{
std::cout<<name<<std::endl;
}
};
test::test()
{
name = "Standard";
}
int main()
{
test test1;
test1.display();
std::cin>>x;
}
#5
2
c++11 actually provides two ways of doing this. You can default the member on it's declaration line or you can use the constructor initialization list.
c++11实际上提供了两种方法。您可以在它的声明行上默认成员,或者您可以使用构造函数初始化列表。
Example of declaration line initialization:
声明行初始化示例:
class test1 {
char name[40] = "Standard";
public:
void display() { cout << name << endl; }
};
Example of constructor initialization:
构造函数初始化的例子:
class test2 {
char name[40];
public:
test2() : name("Standard") {};
void display() { cout << name << endl; }
};
You can see a live example of both of these here: http://ideone.com/zC8We9
您可以在这里看到两个实例:http://ideone.com/zC8We9
My personal preference is to use the declaration line initialization because:
我个人的偏好是使用声明行初始化,因为:
- Where no other variables must be constructed this allows the generated default constructor to be used
- 如果不需要构造其他变量,那么可以使用生成的默认构造函数
- Where multiple constructors are required this allows the variable to be initialized in only one place rather than in all the constructor initialization lists
- 在需要多个构造函数的情况下,这允许在一个地方初始化变量,而不是在所有的构造函数初始化列表中初始化变量
Having said all this, using a char[]
may be considered damaging as the generated default assignment operator, and copy/move constructors won't work. This can be solved by:
说了这么多,使用char[]可能被认为是破坏性的,因为生成的默认赋值操作符,复制/移动构造函数不能工作。这可以通过:
- Making the member
const
- 使成员常量
- Using a
char*
(this won't work if the member will hold anything but a literal string) - 使用char*
- In the general case
std::string
should be preferred - 一般情况下,std::字符串优先
#1
9
If there are no particular reasons to not use std::string
, do use std::string
.
如果没有特别的理由不使用std::string,请使用std::string。
But if you really need to initialize that character array member, then:
但如果您确实需要初始化字符数组成员,则:
#include <assert.h>
#include <iostream>
#include <string.h>
using namespace std;
class test
{
private:
char name[40];
int x;
public:
test();
void display() const
{
std::cout<<name<<std::endl;
}
};
test::test()
{
static char const nameData[] = "Standard";
assert( strlen( nameData ) < sizeof( name ) );
strcpy( name, nameData );
}
int main()
{
test().display();
}
#2
6
Your constructor is not setting the member variable name
, it's declaring a local variable. Once the local variable goes out of scope at the end of the constructor, it disappears. Meanwhile the member variable still isn't initialized and is filled with random garbage.
构造函数没有设置成员变量名,而是声明一个局部变量。一旦本地变量在构造函数的末尾超出范围,它就会消失。同时,成员变量仍然没有初始化,并且填充了随机的垃圾。
If you're going to use old-fashioned character arrays you'll also need to use an old-fashioned function like strcpy
to copy into the member variable. If all you want to do is set it to an empty string you can initialize it with name[0] = 0
.
如果要使用老式的字符数组,还需要使用像strcpy这样的老式函数来复制到成员变量中。如果你想做的就是将它设置为一个空字符串,你可以用[0]= 0来初始化它。
#3
5
Considering you tagged the question as C++, you should use std::string
:
考虑到您将问题标记为c++,您应该使用std::string:
#include <string>
class test
{
private:
std::string name;
int x;
public:
test();
void display()
{
std::cout<<name<<std::endl;
std::cin>>x;
}
};
test::test() : name("Standard")
{
}
#4
4
Since you are using C++, I suggest using strings instead of char arrays. Otherwise you'd need to employ strcpy (or friends).
由于您正在使用c++,我建议使用字符串而不是char数组。否则你需要雇佣strcpy(或朋友)。
Also, you forgot to delete the test1 instance.
此外,您还忘记删除test1实例。
#include <iostream>
#include <string>
class test
{
private:
std::string name;
int x;
public:
test();
void display()
{
std::cout<<name<<std::endl;
}
};
test::test()
{
name = "Standard";
}
int main()
{
test test1;
test1.display();
std::cin>>x;
}
#5
2
c++11 actually provides two ways of doing this. You can default the member on it's declaration line or you can use the constructor initialization list.
c++11实际上提供了两种方法。您可以在它的声明行上默认成员,或者您可以使用构造函数初始化列表。
Example of declaration line initialization:
声明行初始化示例:
class test1 {
char name[40] = "Standard";
public:
void display() { cout << name << endl; }
};
Example of constructor initialization:
构造函数初始化的例子:
class test2 {
char name[40];
public:
test2() : name("Standard") {};
void display() { cout << name << endl; }
};
You can see a live example of both of these here: http://ideone.com/zC8We9
您可以在这里看到两个实例:http://ideone.com/zC8We9
My personal preference is to use the declaration line initialization because:
我个人的偏好是使用声明行初始化,因为:
- Where no other variables must be constructed this allows the generated default constructor to be used
- 如果不需要构造其他变量,那么可以使用生成的默认构造函数
- Where multiple constructors are required this allows the variable to be initialized in only one place rather than in all the constructor initialization lists
- 在需要多个构造函数的情况下,这允许在一个地方初始化变量,而不是在所有的构造函数初始化列表中初始化变量
Having said all this, using a char[]
may be considered damaging as the generated default assignment operator, and copy/move constructors won't work. This can be solved by:
说了这么多,使用char[]可能被认为是破坏性的,因为生成的默认赋值操作符,复制/移动构造函数不能工作。这可以通过:
- Making the member
const
- 使成员常量
- Using a
char*
(this won't work if the member will hold anything but a literal string) - 使用char*
- In the general case
std::string
should be preferred - 一般情况下,std::字符串优先