使用指针和strcpy获取值的C ++类 [英] C++ classes getting value using pointers and strcpy

查看:195
本文介绍了使用指针和strcpy获取值的C ++类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解C ++。任何人都可以解释这段代码是什么,我知道它是一些类型的setter和getter在Java,但我不知道。

I am trying to understand C++. Can anybody explain what this code does exactly, I understood that it is some type of setter and getter in Java but I am not sure.

Comm::Comm(const char* id)
{
strcpy(this->id, id);
}


char* Comm::getId()
{
   return id;
}


推荐答案

它会烧伤孩子的眼睛。

char * char [N] 。没有setter本身,但 Comm 的构造函数尝试将其输入复制到该成员变量。 getId 函数是此成员变量的获取器。

The assumption here is that the class Comm has a member variable of type char* or char[N]. There is no "setter" per se, but Comm's constructor attempts to copy its input to that member variable. The getId function is a getter for this member variable.

根据代码的其余部分,完全缺陷由于缺乏内存分配,缺乏内存分配,和缺乏复制语义。在最佳中,成员是一个数组,然后在 strcpy 调用中缺少范围检查是一个严重的安全风险。

Depending on the rest of the code, this could be totally flawed due to lack of memory allocation, lack of memory de-allocation, and lack of copy semantics. At best the member is an array and then the lack of range checking in the strcpy call is a serious security risk.

类将使用 std :: string 更好地重新设计。

The class would be much better redesigned with the use of std::string.

我会鼓励你学习这段程式码片段。

I would not encourage you to learn from this code snippet.

href =http://stackoverflow.com/q/388242/560648>一本好书。

Instead, learn from a good book.

这篇关于使用指针和strcpy获取值的C ++类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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