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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:41:32+00:00 2026-05-31T15:41:32+00:00

I am doing the following test: #include <iostream> #include <vector> using namespace std; class

  • 0

I am doing the following test:

#include <iostream>
#include <vector>

using namespace std;
class A
{
private:
   int i;
public:
   A():i(1){cout<<"A constr"<<endl;}
   A(const A & a):i(a.i){cout<<"A copy"<<endl;}
   virtual ~A(){cout<<"destruct A"<<endl;}
   void operator=(const A a){cout<<"A assign"<<endl;}
};


int main()
{
   A o1; 
   A o2; 
   o2=o1;
}

And the output is:

A constr
A constr
A copy
A assign
destruct A
destruct A
destruct A

It seems that “o2=o1” did a copy first followed by an assignment, and I wonder what’s the story behind it. Thanks!

  • 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-31T15:41:34+00:00Added an answer on May 31, 2026 at 3:41 pm

    Because you pass by value into your assignment operator:

    void operator=(const A a)
    

    You probably meant to pass by reference and you should also return a reference to the assigned-to object:

    A& operator=(const A& a) { std::cout << "A assign" << std::endl; return *this; }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got the following code: #include <iostream> using namespace std; int main(int argc, char
I am doing the following class RuleObject implements Comparable{ @Override public String toString() {
I have the following C++ code: #pragma once #include StdAfx.h #include <string> using namespace
I'm a beginner using ANTLR. I'm just doing the following for test purposes and
So I have the following test code: #include <string.h> #include <stdio.h> int main(int argc,
Given the following code: [example.h] class MyClass { public: MyClass(); std::string name; std::string address;
I have the following files in this directory: ~/test/lib/liblost.dylib ~/test/include/lost.h I am trying to
Why is the following code giving segmentation fault? #include <stdio.h> #include <stdlib.h> int main()
I my index lets say I have field field named full_name. I'm doing following
I'm currently doing the following to use typed datasets in vs2008: Right click on

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.