Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7629925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:57:58+00:00 2026-05-31T05:57:58+00:00

Most posts I have read regarding implementation of copy constructor is that you must

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T05:58:00+00:00Added an answer on May 31, 2026 at 5:58 am

    The assignment operator doesn’t affect the copy constructor in any way, you CAN define the copy constructor without an assignment operator.

    But do you want to? Classes that need custom copy behavior usually want that custom behavior to apply to both copy construction and copy assignment. This is the rationale for the Rule of 3 (now 5). If you have raw pointers or other members that can’t be default copied and default destroyed, then the default copy assignment operator probably isn’t correct either.

    Of course, it’s perfectly reasonable to simply disable the copy assignment operator if you don’t like the default, instead of writing a new one.

    In the case of your particular code, you’re right that you don’t need a custom copy assignment operator. But you didn’t need a custom copy constructor either, since the default behavior would already be what you wanted. In fact, the compiler-generated default copy constructor is better than the one you defined, since it would copy construct members instead of default constructing and then re-assigning.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have read most of the posts on here regarding floating point, and I
I've read most of the posts regarding this matter but I find nothing that
Most of the posts that I read pertaining to these utilities usually suggest using
OK I have read many posts regarding Dual Licensing using MIT and GPL licenses.
I have read through related posts but have not yet found an answer so
I have read pretty much EVERY blog post, article and help document published regarding
I have read several articles and several stackoverflow.com posts about expression tree. It is
I have read countless blogs, posts and StackOverflow questions about the new features of
Alright...I've given the site a fair search and have read over many posts about
I have already read a lot of posts around this subject but I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.