我写的我的访问者会被接受吗?如果没有,我怎么能更好呢? [英] Would my accessors I've written be acceptable? If not, how can I better it?

查看:68
本文介绍了我写的我的访问者会被接受吗?如果没有,我怎么能更好呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

// In HelloWorld.h

#include <string>
using std::string;

class HelloWorld {

public:
  HelloWorld();
  string getText();
  void setText(string);
  virtual ~HelloWorld();

private:
  string* text;

};

// In HelloWorld.cpp
#include "HelloWorld.h"

// Constructors
HelloWorld::HelloWorld() {
  text = new string("HelloWorld");
}

// Accessors
string HelloWorld::getText() {
  return *text;
}

// Mutators
void HelloWorld::setText(string text) {
  *(this->text) = text;
  return;
}

// Destructor
HelloWorld::~HelloWorld() {
  delete text;
}

推荐答案

class HelloWorld {
 
public:
  HelloWorld();
  string getText() const { return text;}
  void setText(const string &s) { text = s;}
  virtual ~HelloWorld();
 
private:
  string text;
 
};
 
//Constructors
HelloWorld::HelloWorld() {
  text = string("HelloWorld");
}



通常的做法是内联定义get / set方法。另外,不要将std :: string定义为指针。如果你真的想要一个指针,请使用std :: string的c_str()方法。


It''s common practice to define get/set methods inline. Also, don''t define std::string as pointer. If you really want a pointer, use the c_str() method of std::string.


让我们从文本成员开始。为什么要将它声明为指针?如果没有其他理由反对,请将其声明为

Let''s start with the text member. Why have you declared it as pointer? If no other reasons speak against it, just declare it as
string text;



因此你不必关心它的分配和删除。



getText访问器原则上很好。在这里,我只考虑两件事:

- 并非所有程序都使用std :: string类进行字符串处理;将char *(或wchar_t *)返回到以null结尾的字符串会更为通用。在这种情况下,您将使用c_str函数。

- 为什么不使访问函数 const 并声明它不会修改基础对象?



setText setter函数没问题。当你将文本声明为字符串(而不是指针)时,它看起来会简单得多:


And thereby you don''t have to care about its allocation and deletion.

The getText accessor is in principal fine. Here I would just consider two things:
- not all programs do string handling with the std::string class; it would be more general to just return a char* (or wchar_t*) to a null-terminated string. In that case you would be using the c_str function.
- why not make the access function const and declare thereby that it''s not modifying the base object?

The setText setter function is ok. When you declart text as string (and not as a pointer) it will look a lot simpler:

void HelloWorld::setText (const string& t)
    {text = t;}





请注意,我使参数成为const引用,这避免了将字符串复制到临时变量。



只是警告:不要从getter返回const引用,除非你知道你要返回的对象的有效期比函数长。



Note that I made the argument a const reference, which avoids copying the string to a temp variable.

Just a warning: Do NOT return a const reference from a getter, except you know that the object you are returning has a longer live span than the function.


不,这是不可接受的。如果您的班级成员 text 被声明为字符串(而不是指针)。

请考虑以下情形:

No, it is not acceptable. It would be if your class member text was declared as a string (not as a pointer).
Consider the following scenario:
#include "HelloWorld.h"
int main()
{
  string foo = "hi";
  HelloWorld hw; // (1) here a new string object is allocated on the heap.

  hw.setText(foo); // (2) here hw.text point to the constant string "hi".

} // (3) here the disaster: the destructor does NOT delete object (1) but tries to delete the constant string of point (2)


这篇关于我写的我的访问者会被接受吗?如果没有,我怎么能更好呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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