如何在默认构造函数中将 char 数组初始化为 null

How to initialize a char array as null within a default constructor?

本文关键字:数组 char 初始化 null 默认 构造函数      更新时间:2023-10-16

我正在尝试在默认构造函数中设置一个等于 null ('\0'( 的字符数组。当没有传递参数时,我正在尝试将已初始化的 char 指针(类的受保护成员(的值设置为 null。如果传递了正确的参数,程序可以完美运行,但是当我在默认构造函数中设置它们时,我得到一个分段错误:11 错误。

这是头文件:

#ifndef _DVD_H_
#define _DVD_H_
class DVD {
protected:
    unsigned int id;
    char* title;
    char* director;
    char* makecopy(const char*);
public:
    DVD(unsigned int i, const char* t, const char* dir);
    void display();
    DVD () 
    {
        const char temp[] = {''};
        id = 0;
        title = makecopy(temp);
        director = makecopy(temp);
    }
};
#endif /* _DVD_H_ */ 

这是.cpp文件:

#include <iostream>
#include "DVD.h"
using namespace std;
DVD::DVD (unsigned int i, const char* t, const char* dir)
{
    //Constructing id
    id = i;
    //Constructing title
    title = makecopy(t);
    //Constructing director
    director = makecopy(dir);
}
void DVD::display()
{
    //Creating variables to track the length of the title/director and the location of the space. 
    int len_t,len_dir,space_dir;
    for (len_t = 0; title[len_t] != ''; len_t++)
        ;
    for (len_dir = 0; director[len_dir] != ''; len_dir++)
        ;
    for (space_dir = 0; director[space_dir] != ' '; space_dir++)
        ;
    //Display the information in the desired format. 
    cout << '[' << id << ". " << title << '/';
    for (int p = 0; p < space_dir; p++)
     cout << director[p];
    for (int j = space_dir; j < len_dir; j++)
        cout << director[j];
    cout << ']' << endl;
}
char* DVD::makecopy(const char* str)
{
    int len;
    char* copy;
    for (len = 0; str[len] != ''; len++)
        ;
    copy = new char[len+1];
    for (int i = 0; i < len+1; i++)
        copy[i] = str[i];
    return copy;
}

这是另一个.cpp文件

#include<iostream>
using namespace std;
#include"DVD.h"
int main()
{
    char str[] = "Gandhi";
    DVD d1(4, str, "Richard Attenborough");
    DVD d2;
    cout << "First step: " << endl;
    cout << "After constructors:" << endl;
    d1.display(); cout << endl; // [4.  Gandhi/Richard Attenborough]
    d2.display(); cout << endl; // [0.  /]

     cout << "Test for dynamically allocated copies" << endl;
     d1.display(); cout << endl; // [4.  Gandhi/Richard Attenborough]
     d2.display(); cout << endl; // [0.  /]
}

你没有实施三法则,你的makecopy()是可疑的。

尝试更多类似的东西:

#ifndef _DVD_H_
#define _DVD_H_
class DVD
{
protected:
    unsigned int id;
    char* title;
    char* director;
    static char* makecopy(const char* str);
public:
    DVD();
    DVD(unsigned int i, const char* t, const char* dir);
    DVD(const DVD &src);
    ~DVD();
    DVD& operator=(const DVD &rhs);
    void display() const;
};

#include "DVD.h"
#include <iostream>
DVD::DVD() 
    : id(0), title(NULL), director(NULL)
{
}
DVD::DVD(unsigned int i, const char* t, const char* dir)
    id(i), title(makecopy(t)), director(makecopy(dir))
{
}
DVD::DVD(const DVD& src)
    id(src.id), title(makecopy(src.title)), director(makecopy(src.director))
{
}
DVD::~DVD()
{
    delete[] title;
    delete[] director;
}
DVD& DVD::operator=(const DVD& rhs)
{
    if (this != &rhs)
    {
        DVD copy(rhs);
        id = copy.id;
        char *c_temp = copy.title;
        copy.title = title;
        title = c_temp;
        c_temp = copy.director;
        copy.director = director;
        director = c_temp;
    }
    return *this;
}
void DVD::display() const
{
    //Display the information in the desired format. 
    std::cout << '[' << id << ". " << title << '/' << director << ']' << std::endl;
}
char* DVD::makecopy(const char* str)
{
    int len = 0;
    if (str)
    {
        while (str[len] != '')
            ++len;
    }
    char* copy = new char[len+1];
    for (int i = 0; i < len; ++i)
        copy[i] = str[i];
    copy[len] = '';
    return copy;
}

话虽如此,我强烈建议您改用std::string。 让它和编译器为您处理所有内存管理:

#ifndef _DVD_H_
#define _DVD_H_
#include <string>
class DVD
{
protected:
    unsigned int id;
    std::string title;
    std::string director;
public:
    DVD();
    DVD(unsigned int i, const std::string& t, const std::string& dir);
    void display() const;
};
#endif /* _DVD_H_ */ 

#include "DVD.h"
#include <iostream>
DVD::DVD()
    : id(0)
{
}
DVD::DVD(unsigned int i, const std::string& t, const std::string& dir)
    : id(i), title(t), director(dir)
{
}
void DVD::display() const
{
    //Display the information in the desired format. 
    std::cout << '[' << id << ". " << title << '/' << director << ']' << std::endl;
}