Most posts I have read regarding implementation of copy constructor is that you must also overload the assignment operator. I don't understand why that is true. I can implement a copy constructor without doing any operator overloading and it works fine. Can you please explain what I'm missing or why I need to follow this protocol?
Here is some basic code that is working the way I would expect:
//
// 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;
}
Thanks for the info