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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:26:22+00:00 2026-05-29T11:26:22+00:00

Having the following code: #include <iostream> struct A { int x; A(){} ~A(){std::cout <<~A(<<x<<)\n;}

  • 0

Having the following code:

#include <iostream>

struct A
{
    int x;
    A(){}
    ~A(){std::cout <<"~A("<<x<<")\n";}
};
struct B: public A
{
};
void f(A a)
{
    a.x = 2;
}

void main()
{
    B b;
    std::cout <<"----------\n";
    b.x = 1;
    f(b);
    b.x = 3;
    std::cout <<"----------\n";
}

In this case the output is following:

----------
~A(2)
~A(1)
----------
~A(3)

The same code but virtual function is added to parent class:

#include <iostream>

struct A
{
    int x;
    A(){}
    ~A(){std::cout <<"~A("<<x<<")\n";}
    virtual void ff() {}
    A(A& ca): x(ca.x){}
};
struct B: public A
{
};
void f(A a)
{
    a.x = 2;
}

void main()
{
    B b;
    std::cout <<"----------\n";
    b.x = 1;
    f(b);
    b.x = 3;
    std::cout <<"----------\n";
}

In this case we have the following output:

----------
~A(2)
----------
~A(3)

EDIT:
Compiler is: MSVCPP 10


The questions are the following:

  1. Why we have double copying in the first case?
  2. Why we have only one copying in the second case?
  3. The 1st case: why does compiler not optimizes the 1st case (by reducing copy operations quantity)?
  4. Does the following code:

    void f(int);
    //...
    double d;
    f(d);
    

also make double-copying?

  • 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-29T11:26:22+00:00Added an answer on May 29, 2026 at 11:26 am

    The output on gcc 4.3.4 for both cases is:

    ----------
    ~A(2)
    ----------
    ~B
    ~A(3)
    

    Example 1
    Example 2

    This output can be explained as:
    When you call f(b); an copy of the object of type B is created since it is pass by value.
    But there is Object slicing, since the function parameter takes on object of type A. Thus the object in the function now is of the type A and it gets destroyed while returning from the function resulting in call output ~A(2).

    The following two traces are from the destruction of the object b which is created in the main(), Since it is of the type of B destructors for base class A and derived class B both are called for it.

    I am not sure if I missed out any obvious optimization that gcc might be performing in this case but for me the outputs look pretty much as expected.

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

Sidebar

Related Questions

Having the following code: #include <iostream> #include <set> #include <string> #include <functional> using namespace
I'm having an issue with the following code: private void DataPortal_Fetch(TaskCriteria criteria) { using
I'm having problems understanding why following code leaks in one case, and not in
I'm having a problem with the following code: #include <list> #include <boost/shared_ptr.hpp> #include Protocol/IMessage.hpp
When I try and compile the following code... #include <vector> template <class T> void
Im having an issue using GLUT with monodevelop. Im using the following code: #include
I have the following code: struct simple { simple (int a1, int a2) :
I'm having trouble compiling the following code: typedef std::map<mUUID, block_ptr_t> BlockMap; BlockMap _store; std::pair<
I'm having trouble finding the error in the following code: #include <stdio.h> #define LOOP
I have the following code: [test.h] class MyClass { public: string Name; MyClass(); void

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.