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 190859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:17:31+00:00 2026-05-11T16:17:31+00:00

I wrote a small test program with a sample class containing also self-defined constructor,

  • 0

I wrote a small test program with a sample class containing also self-defined constructor, destructor, copy constructor and assignment operator. I was surprised when I realized that the copy constructor was not called at all, even though I implemented functions with return values of my class and lines like Object o1; Object o2(o1);

innerclass.hpp:

#include <iostream>

class OuterClass
{
public:
OuterClass()
{
    std::cout << "OuterClass Constructor" << std::endl;
}
~OuterClass()
{
    std::cout << "OuterClass Destructor" << std::endl;
}
OuterClass(const OuterClass & rhs)
{
    std::cout << "OuterClass Copy" << std::endl;
}
OuterClass & operator=(const OuterClass & rhs)
{
    std::cout << "OuterClass Assignment" << std::endl;
}

class InnerClass
{
public:
    InnerClass() : m_int(0)
    {
        std::cout << "InnerClass Constructor" << std::endl;
    }
    InnerClass(const InnerClass & rhs) : m_int(rhs.m_int)
    {
        std::cout << "InnerClass Copy" << std::endl;
    }
    InnerClass & operator=(const InnerClass & rhs)
    {
        std::cout << "InnerClass Assignment" << std::endl;
        m_int = rhs.m_int;
        return *this;
    }
    ~InnerClass()
    {
        std::cout << "InnerClass Destructor" << std::endl;
    }
    void sayHello()
    {
        std::cout << "Hello!" << std::endl;
    }

private:
    int m_int;
};

InnerClass innerClass()
{
    InnerClass ic;
    std::cout << "innerClass() method" << std::endl;
    return ic;
}
};

innerclass.cpp:

#include "innerclass.hpp"

int main(void)
{
std::cout << std::endl << "1st try:" << std::endl;


OuterClass oc;
OuterClass oc2(oc);
oc.innerClass().sayHello();

std::cout << std::endl << "2nd try:" << std::endl;

OuterClass::InnerClass ic(oc.innerClass());
ic = oc.innerClass();
}

Output:

 1st try:
 OuterClass Constructor
 OuterClass Copy
 InnerClass Constructor
 innerClass() method
 Hello!
 InnerClass Destructor

 2nd try:
 InnerClass Constructor
 innerClass() method
 InnerClass Constructor
 innerClass() method
 InnerClass Assignment
 InnerClass Destructor
 InnerClass Destructor
 OuterClass Destructor
 OuterClass Destructor

After some research I read that there is no guarantee that the compiler will use the explicitely defined copy constructor. I do not understand this behavior. Why does the copy constructor even exist then, if we do not know that it is called? How does the compiler decide if it uses it?

Or, even better, is there a way to force the compiler to use the self-defined copy constructor?

  • 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-11T16:17:31+00:00Added an answer on May 11, 2026 at 4:17 pm

    Just for completeness with the other answers, the standard allows the compiler to omit the copy constructor in certain situations (what other answers refer to as “Return Value Optimization” or “Named Return Value Optimization” – RVO/NRVO):

    12.8 Copying class objects, paragraph 15 (C++98)

    Whenever a temporary class object is copied using a copy constructor, and this object and the copy have the same cv-unqualified type, an implementation is permitted to treat the original and the copy as two different ways of referring to the same object and not perform a copy at all, even if the class copy constructor or destructor have side effects. For a function with a class return type, if the expression in the return statement is the name of a local object, and the cv-unqualified type of the local object is the same as the function return type, an implementation is permitted to omit creating the temporary object to hold the function return value, even if the class copy constructor or destructor has side effects. In these cases, the object is destroyed at the later of times when the original and the copy would have been destroyed without the optimization.

    So in your innerClass() method, the copy constructor you might think would be called at the return is permitted to be optimized away:

    InnerClass innerClass() {
        InnerClass ic;
        std::cout << "innerClass() method" << std::endl;
        return ic;    // this might not call copy ctor
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 122k
  • Answers 122k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If I were doing this, publishing a WCF-based REST/POX service,… May 12, 2026 at 12:41 am
  • Editorial Team
    Editorial Team added an answer Try adding keyword volatile to the variable running: private volatile… May 12, 2026 at 12:41 am
  • Editorial Team
    Editorial Team added an answer As you said, the DataContext is set to the ViewModel… May 12, 2026 at 12:41 am

Related Questions

I wrote a small application to use as a sandbox for testing ideas that
Is it possible to open a file in .NET with non exclusive write access?
I'm stuck on a portion of the Java tutorial, specifically this exercise . The
In my project I found a piece of code in which a method was

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.