【发布时间】:2012-03-12 04:49:41
【问题描述】:
我读过的关于复制构造函数实现的大多数帖子是,您还必须重载赋值运算符。我不明白为什么这是真的。我可以在不进行任何运算符重载的情况下实现复制构造函数,并且工作正常。你能解释一下我缺少什么或为什么我需要遵循这个协议吗?
下面是一些基本代码,它们的工作方式与我期望的一样:
//
// Event.h
// PointerPractice
//
#ifndef PointerPractice_Event_h
#define PointerPractice_Event_h
#include <string>
class Event{
public:
Event();
Event(const Event& source);
~Event();
std::string getName();
void setname(std::string theName);
uint64_t getBeginTime();
void setBeginTime(uint64_t time);
uint64_t getDuration();
void setDuration(uint64_t dur);
private:
std::string name_;
uint64_t time_;
uint64_t duration_;
};
#endif
//
// Event.cpp
// PointerPractice
//
#include <iostream>
#include "Event.h"
Event::Event(){
this->name_ ="";
this->time_ = 0;
this->duration_ = 0;
}
Event::Event(const Event& source){
this->name_ = source.name_;
this->time_ = source.time_;
this->duration_ = source.duration_;
}
Event::~Event(){}
std::string Event::getName(){
return this->name_;
}
void Event::setname(std::string theName){
this->name_ = theName;
}
uint64_t Event::getBeginTime(){
return this->time_;
}
void Event::setBeginTime(uint64_t time){
this->time_ = time;
}
uint64_t Event::getDuration(){
return this->duration_;
}
void Event::setDuration(uint64_t dur){
this->duration_ = dur;
}
// main.cpp
// PointerPractice
//
#include <iostream>
#include <vector>
#include "Event.h"
int main (int argc, const char * argv[])
{
Event *firstPtr = new Event();
firstPtr->setname("DMNB");
firstPtr->setBeginTime(4560000);
firstPtr->setDuration(2000000);
std::cout<<"Test first Event object begin time "<<firstPtr->getBeginTime()<<std::endl;
Event *secPtr = new Event(*firstPtr);
secPtr->setBeginTime(2222222);
std::cout<<"Test first Event object begin time "<<firstPtr->getBeginTime()<<std::endl;
std::cout<<"Test second Event object begin time "<<secPtr->getBeginTime()<<std::endl;
return 0;
}
感谢您的信息
【问题讨论】:
-
看起来您正在尝试用 C++ 编写 Java。不要,C++ 中的良好实践有很大不同。例如,将琐碎的访问器(例如
getName、setName)内联函数定义在类中,您现在拥有的内容会削弱优化器。并了解 ctor-initializer-lists。 -
最后,这里没有充分的理由使用指针(但您的文件名表明您已经知道这一点并且只是为了练习而使用它们)。但是,如果您要练习使用指针,请练习正确释放内存(使用
delete)。 -
@Ben 你能告诉我关于“练习正确释放内存”的意思吗?我的理解是你不要删除字符串和原始数据类型,如整数。事实上,这会在 Xcode 中产生错误,所以对于这个特别简单的类,我认为使用带有空括号的析构函数是合适的。
-
@Miek:
firstPtr = new Event()后面应该跟delete firstPtr,否则你会泄漏内存。
标签: c++ copy-constructor assignment-operator