如何设计具有char *指针作为类成员变量的类? [英] how to design class that has char* pointer as class member variable?

查看:152
本文介绍了如何设计具有char *指针作为类成员变量的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我想介绍一下我的情况:



我写了一些类,将char *指针作为私有类成员。同时这个项目有GUI,点击按钮,一些函数可能执行多次。这些类在项目中设计为单类。但是一些函数可以执行多次。然后我发现我的项目有内存泄漏。 / p>

所以我想问下列问题:


  1. 函数?

  2. 如何设计使用char *成员变量的其他函数?

  3. 如何设计类operator = function?



  class A:
{
public:
setStr(char * s){//需要new just use =?};
A& operator =(const A& other){// also need new?};
manyTimesFunctions(char * other)
{
//需要使用chars其他赋值str
//如何仔细使用new以避免内存泄漏?
//其他可能是另一个类的locality没有const变量

}
private:
char * str;因此,项目只有init类A一次,但可以使用<$ c










$ c> setStr
manyTimesFunctions 多次。






可能是答案:



我想我发现我需要照顾: 复制该课程 ,这些答案对我来说非常有用。


<这个答案将用于比较给定的状态的其他答案使用 std :: string

c>(和那些答案是正确的 - 使用 std :: string )。



只使用 char * ,你不能因为某些原因使用 std :: string NULL终止字符串。这是一个概述你的实现将要做什么(请简单地使用 std :: string )进行比较:

  #include< algorithm> 
#include< cstring>

class A
{
public:
//构造空字符串
A():str(new char [1]

//从非空构造
A(const char * s):str(new char [strlen(s)+ 1])$ ​​b $ b {strcpy(str,s ); }

// copy construct
A(const A& rhs):str(new char [strlen(rhs.str)+ 1])$ ​​b $ b {strcpy(str,rhs .str); }

// destruct
〜A(){delete [] str; }

// assign
A& operator =(const A& rhs)
{
A temp(rhs);
std :: swap(str,temp.str);
return * this;
}

// setter
void setStr(char * s)
{
temp(s);
* this = temp;
}

// getter
const char * getStr(){return str; }

private:
char * str;
};

活动示例



添加几个构造函数和getter函数后,它位于第3条规则



您看到我们需要添加多少代码才能使类安全地可复制和分配?这就是为什么使用 std :: string 比使用 char * 对于 std :: string ,与添加复制/赋值(和move,我没有显示)函数相比,单个行需要更改。



底线是在C ++中如果你想要字符串,使用字符串( std :: string )并尽量远离使用 char * (除非你有一个非常令人信服的理由使用 char * 来表示字符串数据) p>

First i want to introduce my situation :

I have write some classes that has char* pointer as private class member.And also this project has GUI, so when click buttons,some functions may execute more than one time.Those classes are designed single class in project.But some functions of them can execute more than one time.Then I found my project has memory leak.

so i want to ask the following questions:

  1. how to design the set function?
  2. how to design the other functions that use the char* member variable?
  3. how to design the class operator= function?

for example:

class A:
{
 public :
 setStr(char * s){//need new or just use =?};
 A & operator=(const A& other){//also need new?};
 manyTimesFunctions(char * other)
 {
  //need to use chars other to assignment str
  //how to carefully use new to avoid memory leak?
  //other may be another class's locality none const variable 

 } 
 private:
 char * str;
}

So ,the project only init class A once,but may use setStr and manyTimesFunctions many times.


May be the answer:

I think i have found what i need to take care of:copy that class,that answers are really useful to me.

解决方案

This answer will be used to contrast what the other answer(s) given that state to use std::string (and those answers are correct -- use std::string).

Let's assume that you could only use char *, you can't for some reason use std::string, and that you are dealing with NULL terminated strings. This is a synopsis of what your implementation would have to do (and please compare this with simply using std::string):

#include <algorithm>
#include <cstring>

class A
{
    public:
       // construct empty string
       A () : str(new char[1]()) {}

       // construct from non-empty
       A(const char *s) : str(new char[strlen(s) + 1])
       { strcpy(str, s); }

       // copy construct
       A(const A& rhs) : str(new char[strlen(rhs.str) + 1])
       { strcpy(str, rhs.str); }

       // destruct
       ~A() { delete [] str; }

       // assign
       A& operator=(const A& rhs)
       {
          A temp(rhs);
          std::swap(str, temp.str);
          return *this;
       }

       // setter
       void setStr(char * s)
       {
          A temp(s);
          *this = temp;
       }  

       // getter
       const char* getStr() { return str; }

     private:
        char * str;
};

Live Example

After adding a couple more constructors and a getter function, this follows the Rule of 3.

You see how much code we needed to add just to make the class safely copyable and assignable? That's why using std::string is much more convenient than using char * when it comes to class members. For std::string a single line needs to be changed, compared to adding the copy / assignment (and move, which I didn't show) functions.

The bottom line is that in C++ if you want strings, use strings (std::string) and try to keep away from using char * (unless you have a very compelling reason to be using char * to represent string data).

这篇关于如何设计具有char *指针作为类成员变量的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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