返回字符数组的标准? [英] Standard for returning character arrays?

查看:63
本文介绍了返回字符数组的标准?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class Foo

{

受保护:

char foo_stuff [16];


public:

char * get_foo_stuff();

}


给出上面的例子。我想知道的是适当的/标准的。方式

在访问者函数中返回foo_stuff而不给用户

直接访问数据。我目前设置它的方法是创建一个全新的char数组和strcpy foo_stuff并返回新数组的

地址。我并不特别喜欢这种方法,因为它需要调用者手动删除返回的数据。所以,我猜这个时候我会把它留给专家。

谢谢,


~Justin

class Foo
{
protected:
char foo_stuff[16];

public:
char* get_foo_stuff();
}

Given the above example. What I want to know is the "proper/standard" way
of returning foo_stuff in an accessor function without giving the user
access to the data directly. The way I currently have it set up is to creat
an entirely new char array and strcpy foo_stuff into it and return the
address of the new array. I don''t particularly like that method as it
requires that the caller manually delete the data that is returned. So, I
guess at this point I will leave it to the experts.
Thanks,

~Justin

推荐答案

Justin Naidl写道:
Justin Naidl wrote:
class Foo
{
protected:
char foo_stuff [ 16];

公开:
char * get_foo_stuff();
}
;
鉴于上面的例子。我想知道的是适当的/标准的。在访问者函数中返回foo_stuff而不给用户直接访问数据的方法。


只要指针在你返回后仍然有效,直接返回''foo_stuff''就没有

的问题。

我目前设置的方法是创建一个全新的char数组和strcpy foo_stuff并返回新数组的
地址。


这只是另一种方式。用户必须在完成后删除

数组。

我不是特别喜欢这种方法,因为它需要调用者手动删除返回的数据。所以,我想在这一点上我会留给专家。
class Foo
{
protected:
char foo_stuff[16];

public:
char* get_foo_stuff();
} ;
Given the above example. What I want to know is the "proper/standard" way
of returning foo_stuff in an accessor function without giving the user
access to the data directly.
As long as the pointer is still valid after you return it, there is no
problem with returning ''foo_stuff'' directly.
The way I currently have it set up is to creat
an entirely new char array and strcpy foo_stuff into it and return the
address of the new array.
That''s just another way. You charge the user with having to delete the
array after they are done with it.
I don''t particularly like that method as it
requires that the caller manually delete the data that is returned. So, I
guess at this point I will leave it to the experts.




专家会告诉你没有什么特别的,可能会使用

''std :: string''。当然,为了确保专家知道''foo_stuff''的含义是什么以及用户应该如何使用''
值'' get_foo_stuff''...


V



There is nothing special the expert will tell you except, maybe, to use
''std::string''. Of course, to be sure an expert would know what the
meaning of ''foo_stuff'' is and how the user is supposed to use the return
value of ''get_foo_stuff''...

V


" Justin Naidl" < JN *** @ lsol.net>在消息中写道

news:jb ******************* @ monger.newsread.com
"Justin Naidl" <jn***@lsol.net> wrote in message
news:jb*******************@monger.newsread.com
class Foo <公开:
char * get_foo_stuff();
}
适当/标准在访问者函数中返回foo_stuff的方法
而不让用户直接访问数据。我目前设置它的方式是创建一个全新的char数组并将
strcpy foo_stuff放入其中并返回新数组的地址。我并不特别喜欢这种方法,因为它要求调用者手动删除返回的数据。所以,我想在这一点上
我会留给专家。
class Foo
{
protected:
char foo_stuff[16];

public:
char* get_foo_stuff();
}

Given the above example. What I want to know is the
"proper/standard" way of returning foo_stuff in an accessor function
without giving the user access to the data directly. The way I
currently have it set up is to creat an entirely new char array and
strcpy foo_stuff into it and return the address of the new array. I
don''t particularly like that method as it requires that the caller
manually delete the data that is returned. So, I guess at this point
I will leave it to the experts.




没有适当/标准的办法。这取决于你想要实现的价格,这部分取决于你如何看待

来电者使用的数据。


一种方法是返回一个指向const char的指针。这给了

来电者只读。访问数据。如果用户想要他们自己的

可修改的数据副本,那么用户可以制作副本并承担管理该副本的全部责任。一般来说,如果分配内存的人有责任删除

,那么问题就更少了。


另一种可能性是是定义一个operator []来一次一个元素来访问数据

。这个操作符可以返回一个char并且不需要动态的
内存分配。


-

John Carson


Victor Bazarov写道:
Victor Bazarov wrote:
Justin Naidl写道:
Justin Naidl wrote:
class Foo
{
受保护:
char foo_stuff [16];

公开:
char * get_foo_stuff();
}
class Foo
{
protected:
char foo_stuff[16];

public:
char* get_foo_stuff();
}


;


鉴于上述例子。我想知道的是正确/标准的方式在访问者函数中返回foo_stuff而不直接让
用户访问数据。

Given the above example. What I want to know is the "proper/standard"
way of returning foo_stuff in an accessor function without giving the
user access to the data directly.


只要指针在返回后仍然有效,直接返回''foo_stuff''就没有问题。



As long as the pointer is still valid after you return it, there is no
problem with returning ''foo_stuff'' directly.




有一个问题:用户现在可以修改它,即使它不是公共成员的b $ b。 get_foo_stuff()函数公开它,顺便说一下。

意味着这个函数现在已经没有任何意义了。只需将

成员公开,你就可以获得相同的功能,而无需额外支付




There is one problem: The user is now able to modify it, even though it''s
not a public member. The get_foo_stuff() function exposes it, which btw.
means that this function now serves no purpose at all anymore. Just make
the member public and you have the same functionality without the need for
an additional function.

>我目前设置它的方法是创建一个全新的char数组和strcpy foo_stuff到它并返回新数组的
地址。
> The way I currently have it set up is to creat
an entirely new char array and strcpy foo_stuff into it and return the
address of the new array.



那只是另一种方式。用户必须在完成后删除
数组。



That''s just another way. You charge the user with having to delete the
array after they are done with it.




其他方法是在内部存储指针并删除[]数组

使用修改对象的下一个函数。我认为std :: string的一些

实现就是这样做的。另一种方法是返回一些类型的智能指针而不是原始指针。



Other ways would be to store the pointer internally and delete [] the array
on use of the next function that modifies the object. I think some
implementations of std::string do this. Another way is to return some type
of smart pointer instead of a raw pointer.

>我并不特别喜欢这种方法,因为它要求调用者手动删除返回的数据。所以,
我想在这一点上我会把它留给专家。
> I don''t particularly like that method as it
requires that the caller manually delete the data that is returned. So,
I guess at this point I will leave it to the experts.



专家会告诉你没有什么特别的,可能,使用
'' 的std :: string ''。当然,要确保专家知道''foo_stuff''的含义是什么以及用户应该如何使用''get_foo_stuff''的返回值...

V



There is nothing special the expert will tell you except, maybe, to use
''std::string''. Of course, to be sure an expert would know what the
meaning of ''foo_stuff'' is and how the user is supposed to use the return
value of ''get_foo_stuff''...

V






这篇关于返回字符数组的标准?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆