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

  • Home
  • SEARCH
  • 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 7677481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:25:56+00:00 2026-05-31T17:25:56+00:00

I have never written copy constructor, so in order to avoid pain i wanted

  • 0

I have never written copy constructor, so in order to avoid pain i wanted to know if what i have coded is legit. It compiles but i am not sure that it works as a copy constructor should.

Also do i have to use const in the copy constructor or i can simply drop it. (What i dont like about const is that the compiler cries if i use some non const functions).

//EditNode.h
class EditNode
{
      explicit EditNode(QString elementName);
      EditNode(const EditNode &src);
}

//EditNodeContainer.h
class EditNodeContainer : public EditNode
{
      explicit EditNodeContainer(QString elementName);
      EditNodeContainer(const EditNodeContainer &src);
}

//EditNodeContainer.cpp
EditNodeContainer::EditNodeContainer(QString elementName):EditNode(elementName)
{
}       

//This seems to compile but not sure if it works
EditNodeContainer::EditNodeContainer(const EditNodeContainer &src):EditNode(src)
{

}


//the idea whould be to do something like this
EditNodeContainer *container1 = new EditNodeContainer("c1");
EditNodeContainer *copyContainer = new EditNodeContainer(container1);
  • 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-31T17:25:57+00:00Added an answer on May 31, 2026 at 5:25 pm

    A copy constructor is a constructor that has one of the following signatures:

    class A
    {
        A(A& other);
        //or
        A(const A& other);
        //or
        A(volatile A& other);
        //or
        A(const volatile A& other);
        //or any of the above + other parameters that have default arguments
        //example:
        A(const A& other, int x = 0) //this is also a copy constructor
    };
    

    The above is specified in 12.8.2 of the standard – C++03.
    so you are implementing correctly a copy constructor.

    The reason it should receive a const parameter is that you’re not changing the object you’re copying from. If you call non-const functions on it, you’re doing something wrong.

    Also, in your snippet

    EditNodeContainer *container1 = new EditNodeContainer("c1");
    EditNodeContainer *copyContainer = new EditNodeContainer(container1);
    

    you’re not calling a copy constructor, because you’re passing an EditNodeContainer* as parameter, not a EditNodeContainer.

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

Sidebar

Related Questions

I have never written a DSL, but I am considering it as a feature
Although I know the basic concepts of binary representation, I have never really written
First of all, I have never written a property editor from scratch, but had
I have never written a stateful IComparer<T> with a default constructor. All standard library
I have been writing specs for controllers and models, but I have never written
Ok I have written some basic generic webservices before but I have never tried
I have never written a python script in my life, but I have a
I have never hand-coded object creation code for SQL Server and foreign key decleration
First off, I know I can copy "this" on instantiation, but that doesn't work
I have never written any code that uses threads. I have a web application

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.