2013-05-13 89 views
1

我对如何执行char *的深层复制有点困惑。这是我有:如何执行char *的深层副本?

Appointment(Appointment& a) 
{ 
    subject = new char; 
    *subject = *(a.subject); 
} 

Appointment(Appointment& b) 
{ 
    location = new char; 
    *location = *(b.location); 
} 

char *subject; 
char *location; 

我想执行一个深色副本的指针主题和位置。这会工作吗?如果没有,关于如何去做这件事的任何建议?

+3

你需要这些字符串的长度和std :: copy。或者避免使用char *并使用std :: string,这可以在复制时自行处理。 – nurettin 2013-05-13 05:53:12

+1

如果你在内存位置有一个数组,你可能不想使用'new char',因为这只会为1个字符分配足够的内存,所以你会遇到[未定义的行为](http:// en。维基百科。org/wiki/Undefined_behavior)如果你试图访问数组的其余部分(意味着它可能工作,但不一定是所有的时间)。你需要改为说'new char [len]'。 – Dukeling 2013-05-13 05:59:21

+1

而你有两个相同参数化的半合格拷贝。这段代码甚至不会*按原样编译,更不用说是正确的。你需要* a *单数符合copy-ctor(使参数成为一个const引用),以正确地初始化* both *成员,或者更好的是,使用'std :: string'并使所有这些完全不相关。 – WhozCraig 2013-05-13 06:04:15

回答

2

class Appointment 
{ 
public: 
    std::string subject; 
    std::string location; 

    Appointment() 
    { 
    } 

    Appointment(const Appointment& src) : 
     subject(src.subject), location(src.location) 
    { 
    } 

    Appointment& operator=(const Appointment& lhs) 
    { 
     subject = lhs.subject; 
     location = lhs.location; 
    } 
}; 

这可以进一步简化,由编译器生成的默认构造函数和赋值运算符足以自动为您深度的值复制C++,你应该使用std::string来满足你的字符串需求。

下面的代码,你写

Appointment(Appointment& a) 
{ 
    subject = new char; 
    *subject = *(a.subject); 
} 

不会做你认为它会做的,上面你分配一个字符(new char)则a.subject第一字符分配给它(*subject = *(a.subject)

为了复制char*指向的字符串,您必须首先确定字符串长度,分配内存以保存字符串,然后复制字符。

Appointment(Appointment& a) 
{ 
    size_t len = strlen(a.subject)+1; 
    subject = new char [len]; // allocate for string and ending \0 
    strcpy_s(subject,len,a.subject); 
} 

char*的另一种方法是使用一个std::vector<char>,这取决于你想用字符串做什么。

+0

谢谢!很好解释! – user2278489 2013-05-13 07:05:33

0

没有。

您需要分配足够的内存来存储您要复制

subject = new char [strlen(a.subject + 1]; // +1 to allow for null charcter terminating the string. 

然后用strncpymemcpy或复制所有的字符在循环中复制字符串

1

你必须跟踪char*长度,以便使它们的副本,如:

class Appointment 
{ 
public: 
    char *subject; 
    int subject_len; 

    char *location; 
    int location_len; 

    Appointment() : 
     subject(NULL), subject_len(0), 
     location(NULL), location_len(0) 
    { 
    } 

    ~Appointment() 
    { 
     delete[] subject; 
     delete[] location; 
    } 

    Appointment(const Appointment& src) : 
     subject(new char[src.subject_len]), subject_len(src.subject_len), 
     location(new char[src.location_len]), location_len(src.location_len) 
    { 
     std::copy(src.subject, src.subject + src.subject_len, subject); 
     std::copy(src.location, src.location + src.location_len, location); 
    } 

    Appointment& operator=(const Appointment& lhs) 
    { 
     delete[] subject; 
     subject = NULL; 

     delete[] location; 
     location = NULL; 

     subject = new char[lhs.subject_len]; 
     subject_len = lhs.subject_len; 
     std::copy(lhs.subject, lhs.subject + lhs.subject_len, subject); 

     location = new char[lhs.location_len]; 
     location_len = lhs.location_len; 
     std::copy(lhs.location, lhs.location + lhs.location_len, location); 
    } 
}; 

在哪种情况下,你最好使用std::string相反:由于您使用的

class Appointment 
{ 
public: 
    std::string subject; 
    std::string location; 
}; 
+0

+1为希望不言自明的证明为什么聪明的成员是一件好事*。很好的逐步总结。 – WhozCraig 2013-05-13 06:14:58