Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ char*=新字符和char*=新字符[N]_C++_Pointers_Char - Fatal编程技术网

C++ char*=新字符和char*=新字符[N]

C++ char*=新字符和char*=新字符[N],c++,pointers,char,C++,Pointers,Char,我构造了一个名为CMyString的类,如下所示: class CMyString { public: CMyString(); CMyString(char* pData); CMyString(const CMyString& str); ~CMyString(void); char* getData(); void setData(char* pData); CMyString& operator=(const C

我构造了一个名为CMyString的类,如下所示:

class CMyString {
public:
    CMyString();
    CMyString(char* pData);
    CMyString(const CMyString& str);
    ~CMyString(void);

    char* getData();
    void setData(char* pData);

    CMyString& operator=(const CMyString& str);

private:
    char* m_pData;
};

    CMyString::CMyString() {
    m_pData = new char;
}

CMyString::CMyString(char* pData) {
//    m_pData = new char;
    m_pData = pData;
}

CMyString::CMyString(const CMyString& str) {
    // 为指针分配内存
//    m_pData = new char;
    // 拷贝值
    m_pData = str.m_pData;
}

CMyString::~CMyString(void) {
//    delete m_pData;
}

CMyString& CMyString::operator=(const CMyString& str) {
    if (this == &str)
        return *this;

    delete m_pData;
    m_pData = nullptr;
    m_pData = new char[strlen(str.m_pData) + 1];
    strcpy(m_pData, str.m_pData);

    return *this;
}

char* CMyString::getData() {
    return m_pData;
}

void CMyString::setData(char *pData) {
    m_pData = pData;
}
以下是我的主要内容。cpp:

#include <iostream>
#include "CMyString.h"

using std::cout;
using std::endl;

int main() {
    char* pData = "What are you worrying about?";
    std::cout << pData << std::endl;

    cout << strlen(pData) << endl;
    char* test = new char[30];
    cout << sizeof(test) << endl;
    char* test2 = new char;
    test2 = "23";
    cout << test2 << endl;

    strcpy(test, pData);

    cout << endl << test << endl << endl;

    CMyString str(pData);
    std::cout << str.getData() << std::endl;

    CMyString str2, str3;
    str3 = str2 = str;

    std::cout << str3.getData() << endl;

    char* pData2 = "Data has been changed.";
    str3.setData(pData2);

    cout << str.getData() << endl;
    cout << str2.getData() << endl;
    cout << str3.getData() << endl;

    return 0;
}
我在课堂实施中是对的吗? 我怎样才能分辨出这两个不同的指针? 我是否正确编写构造函数和反构造函数函数以及运算符=?如果没有,怎么写

char* pData = new char;  -> Allocates one piece / slot of memory
char* pData2 = new char[30]; -> Allocates 30 continuous pieces / slots of memory
第一个使用
delete pData
,第二个使用
delete[]pData

除此之外,您正在使用
char*pData=“STRING”
,我不建议您使用它,因为您正在设置指向该
字符串的指针,而不是将其复制到某个位置

当涉及到
操作符=
时,如果您传递了正确的数据,我可以看到它看起来很好

编辑: 看到其他评论说我没有看到复制构造函数

Copy构造函数它构造/生成一个与构造函数相同的实例,因此如果您在构造函数和类中动态分配内存,那么您也应该在Copy构造函数中这样做

// Ofcourse #include<cstring>
class CMyString {
public:
CMyString();
CMyString(char* pData);
CMyString(const CMyString& str);
~CMyString(void);

char* getData();
void setData(char* pData);

CMyString& operator=(const CMyString& str);

private:
    char* m_pData;
};

    CMyString::CMyString() {
    m_pData = nullptr; // so it doesn't hang
}

CMyString::CMyString(char* pData) {
    m_pData = new char[strlen(pData) + 1]; // allocates slots of memory with length of the string pData
    strcpy(m_pData, pData)
}

CMyString::CMyString(const CMyString& str) {
    m_pData = new char[strlen(str.m_pData) + 1];
    strcpy(m_pData, str.m_pData);
    // Here you are constructing an instance so you should allocate memory for the dynamic string
}

CMyString::~CMyString(void) {
   delete [] m_pData; // Since we allocated few slots
}

CMyString& CMyString::operator=(const CMyString& str) {
    if (this == &str)
        return *this;

    delete [] m_pData; // Few slots of memory should be released
    // m_pData = nullptr; No need for this since you are allocating after
    m_pData = new char[strlen(str.m_pData) + 1];
    strcpy(m_pData, str.m_pData);

    return *this;
}

char* CMyString::getData() {
    return m_pData;
}

void CMyString::setData(char *pData) {
     // m_pData = pData; Illegal don't get the address of a string make a copy of it (Don't point to a string you want a copy)
     // If it isn't set. If it is, you will need to delete it
     m_pData = new char[strlen(pData) + 1];
     strcpy(m_pData, pData);
}
//当然#包括
类CMyString{
公众:
CMyString();
CMyString(char*pData);
CMyString(常数CMyString和str);
~CMyString(void);
char*getData();
无效设置数据(字符*数据);
CMyString和operator=(常量CMyString和str);
私人:
char*m_pData;
};
CMyString::CMyString(){
m_pData=nullptr;//所以它不会挂起
}
CMyString::CMyString(char*pData){
m_pData=new char[strlen(pData)+1];//使用字符串pData的长度分配内存插槽
strcpy(m_pData,pData)
}
CMyString::CMyString(常量CMyString&str){
m_pData=新字符[strlen(str.m_pData)+1];
strcpy(m_pData,str.m_pData);
//这里您正在构造一个实例,因此应该为动态字符串分配内存
}
CMyString::~CMyString(void){
删除[]m_pData;//因为我们分配了几个插槽
}
CMyString&CMyString::operator=(常量CMyString&str){
if(this==&str)
归还*这个;
delete[]m_pData;//应该释放几个内存插槽
//m_pData=nullptr;不需要这样做,因为您是在
m_pData=新字符[strlen(str.m_pData)+1];
strcpy(m_pData,str.m_pData);
归还*这个;
}
char*cmysting::getData(){
返回m_pData;
}
void cmysting::setData(char*pData){
//m_pData=pData;非法不获取字符串的地址复制该字符串(不要指向要复制的字符串)
//如果未设置,则需要将其删除
m_pData=新字符[strlen(pData)+1];
strcpy(m_pData,pData);
}
可能有一些打字错误,所以如果需要编辑或添加任何内容,请与我联系

注意:test=“String”是非法的。如果您有意,请始终复制内容(strcpy)。如果有人因为我的知识有限(还是新手)而离开,请看其他评论。不过,我会尽力帮忙的

重要提示:分配时,请尝试理解其背后的概念,以及您想做什么和您已经做了什么。不要忘记删除/删除[]:)
祝你好运

嗯,你有很多虫子。只是不在
操作符=
中。这里只有一个毫无意义的
m_pData=nullptr

newchar
分配一个字符,而
newchar[30]
分配一个包含30个字符的数组。指针是相同的,您必须知道它是两个指针中的哪一个。每一个新的必须通过删除来平衡,每一个新的[]必须通过删除[]来平衡。所以最好不要把两者混在一起。使用
新字符[1]

代码其余部分的一些注释:

1)
m_pData=str.m_pData
复制数组的地址,因此现在两个类使用相同的数组,并且旧数组永远不会被释放

2) 析构函数中注释掉的delete是防止内存泄漏所必需的,但应该是delete[]。但是1使这不可能

3)
test2=“23”使用不兼容的指针类型。C字符串是常量,编译器应该对此发出警告

4)
test2=“23”复制数组的addsress,覆盖旧值。旧阵列是内存泄漏

5)
cmystringstr(pData)导致将pData的地址复制到类中。2中的delete[]稍后将尝试释放它,但它从未被new[]分配。构造函数和setData应该像在operator=中那样真正复制字符串


6) 看看std::move、std::uniq_ptr、std::shared_ptr。

两件事:你不想混合
新字符和
新字符[n]
。它们需要稍微不同的删除(
delete
delete[]
)并且几乎不可能从指针中找出要使用哪一个。接下来,
char*str=“一个字符串”
在C++11之前的代码中格式不好,之后是非法的。“字符串”是字符串文字,可以存储在不可写存储器中。它应该是一个
const char*
,以防止意外的程序崩溃写入无法写入的int。复制构造函数会导致源和副本指向同一字符串。一旦您完全实现析构函数,这是不好的,因为它们都会试图销毁相同的字符串和其他不好的东西。您需要分配一个新的缓冲区来保存源字符串的副本,然后将其复制到该缓冲区中。在C++中,没有人需要一个类,如<代码> cMySyth。扔掉它,使用
std::string
。我已经找到了^ ^。如果需要什么,请联系我(刚刚完成这部分哈哈)
// Ofcourse #include<cstring>
class CMyString {
public:
CMyString();
CMyString(char* pData);
CMyString(const CMyString& str);
~CMyString(void);

char* getData();
void setData(char* pData);

CMyString& operator=(const CMyString& str);

private:
    char* m_pData;
};

    CMyString::CMyString() {
    m_pData = nullptr; // so it doesn't hang
}

CMyString::CMyString(char* pData) {
    m_pData = new char[strlen(pData) + 1]; // allocates slots of memory with length of the string pData
    strcpy(m_pData, pData)
}

CMyString::CMyString(const CMyString& str) {
    m_pData = new char[strlen(str.m_pData) + 1];
    strcpy(m_pData, str.m_pData);
    // Here you are constructing an instance so you should allocate memory for the dynamic string
}

CMyString::~CMyString(void) {
   delete [] m_pData; // Since we allocated few slots
}

CMyString& CMyString::operator=(const CMyString& str) {
    if (this == &str)
        return *this;

    delete [] m_pData; // Few slots of memory should be released
    // m_pData = nullptr; No need for this since you are allocating after
    m_pData = new char[strlen(str.m_pData) + 1];
    strcpy(m_pData, str.m_pData);

    return *this;
}

char* CMyString::getData() {
    return m_pData;
}

void CMyString::setData(char *pData) {
     // m_pData = pData; Illegal don't get the address of a string make a copy of it (Don't point to a string you want a copy)
     // If it isn't set. If it is, you will need to delete it
     m_pData = new char[strlen(pData) + 1];
     strcpy(m_pData, pData);
}