C++ 将字符数组存储在类中然后返回它
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1642091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Storing char array in a class and then returning it
提问by Tatu Ulmanen
I need to store a char array inside a class and then return it. I have to admit that I'm a bit confused about pointers and have tried everything I can think of but can't get it to work. Here's what I have:
我需要在类中存储一个字符数组,然后返回它。我不得不承认我对指针有点困惑,并且已经尝试了我能想到的所有方法,但无法让它发挥作用。这是我所拥有的:
#include <iostream>
using namespace std;
class Test {
public:
void setName(char *name);
char getName();
private:
char m_name[30];
};
void Test::setName(char *name) {
strcpy(m_name, name);
}
char Test::getName() {
return *m_name;
}
void main() {
Test foobar;
char name[] = "Testing";
foobar.setName(name);
cout << foobar.getName();
}
Of course, I expect setName() to store the string "Testing" inside the class, and getName() should return "Testing". But instead, I get only the first letter T. What am I doing wrong?
当然,我希望 setName() 在类中存储字符串“Testing”,而 getName() 应该返回“Testing”。但相反,我只得到第一个字母 T。我做错了什么?
I guess I should be using std strings but first I would like to understand why this does not work. As far as I know, this should work with char arrays as well?
我想我应该使用 std 字符串,但首先我想了解为什么这不起作用。据我所知,这也适用于 char 数组?
回答by sharptooth
Just return a pointer:
只需返回一个指针:
const char* Test::getName() const
{
return m_name;
}
and add a constructor for the class Test
that would null-terminate the encapsulated array:
并为class Test
将空终止封装数组添加一个构造函数:
Test::Test()
{
m_name[0] = 0;
}
so that you don't ask for trouble if someone instantiates class Test
and doesn't call setName()
on the instance.
这样如果有人实例化class Test
并且不调用实例,您就不会自找麻烦setName()
。
回答by Naveen
In Test::getName()
you are just returning one character (first character). Instead you should return the address of the first character from where the string begins i.e. change the return type to char*
and the return statement to return m_name;
在Test::getName()
你只是返回一个字符(第一个字符)。相反,您应该返回字符串开始处的第一个字符的地址,即将返回类型更改为char*
,并将返回语句更改为return m_name;
回答by unwind
When you have a pointer, p
, the pointer dereferencing operator *
"follows" the pointer so the expression *p
evaluates to whatever object the pointer is pointing at.
当您有指针时p
,指针解引用运算符*
“跟随”指针,因此表达式的*p
计算结果为指针指向的任何对象。
In many situations, the name of an array such as m_name
, can behave like a pointer. Thus, *m_name
evaluates to the first char
in the array, since that is the type the array name, when interpreted as a pointer, points at.
在许多情况下,诸如m_name
,之类的数组名称的行为类似于指针。因此,*m_name
计算为char
数组中的第一个,因为这是数组名称在解释为指针时指向的类型。
Since strings in C are represented as pointers to characters, you should not dereference the pointer, but return it intact.
由于 C 中的字符串表示为指向字符的指针,因此不应取消对指针的引用,而应完整地返回它。
Many have suggested you use strncpy()
to write the input string into your array, since it does (sort of) bounds checking. However, it is not optimal, it's semantics are odd and copying a string into a limited buffer is really not what it was designed for. It is better to investigate if you have a variety of the snprintf()
function in your environment, and use that like so:
许多人建议您使用strncpy()
将输入字符串写入数组,因为它执行(某种)边界检查。但是,它不是最佳的,它的语义很奇怪,将字符串复制到有限的缓冲区中实际上并不是它的设计目的。最好调查一下您snprintf()
的环境中是否有多种函数,并像这样使用它:
snprintf(m_name, sizeof m_name, "%s", name);
回答by Mizipzor
If you want this to be C++ simply dont use char pointers unless you have a very, very specific reason to do so!
如果您希望它成为 C++,请不要使用字符指针,除非您有非常非常具体的理由这样做!
Switch from char pointer to std::string and see if that solves your problem:
从 char 指针切换到 std::string ,看看是否能解决您的问题:
#include <iostream>
#include <string>
using namespace std;
class Test {
public:
void setName(std::string name);
std::string getName();
private:
std::string m_name;
};
void Test::setName(std::string name) {
m_name = name;
}
std::string Test::getName() {
return m_name;
}
void main() {
Test foobar;
foobar.setName("Testing");
cout << foobar.getName();
}
For bonus points, make the parameter type in setName a const std::string&.
对于加分,将 setName 中的参数类型设为 const std::string&。
回答by Mizipzor
And it is safer to use strncpy()
than strcpy()
in void Test::setName()
而且使用起来strncpy()
比strcpy()
在里面更安全void Test::setName()