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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:28:07+00:00 2026-06-15T02:28:07+00:00

So, the destructor is called twice if my object isn’t passed by the reference.

  • 0

So, the destructor is called twice if my object isn’t passed by the reference.
But when i pass it by reference everything is ok.

<!-- language: lang-cpp -->

ostream& operator<<(ostream& os,Counter_naslednik_1 &object){
 cout<<endl<<object.date<<endl;
 return os;
 }

//-----------------
void main(){
Counter_naslednik_1 new_object;
cout<<new_object;
}

Why is the destructor called twice? And why should I pass my object by reference when passing it as a parameter to an overloaded operator?

  • 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-06-15T02:28:08+00:00Added an answer on June 15, 2026 at 2:28 am

    It’s called twice because when you pass by value you are actually making a copy of your object, so there are two objects to destroy.

    With respect to your second question, once again, passing the new object by reference avoids making a new copy of your data. This is more efficient and avoids unexpected behaviour, in case you have not designed your copy constructor.

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

Sidebar

Related Questions

Possible Duplicate: Why is the destructor not called for the returned object from the
hey there, why is the base destructor called twice at the end of this
When an object is created in your main() function, is its destructor called upon
I'm trying to understand where an important object gets his destructor called. I made
Can I trust that an object is destroyed and its destructor is called immediately
This question is inspired by Using an object after it's destructor is called Let's
Does a destructor get called if the app crashes? If it's an unhandled exception
I am trying to attach a function-object to be called on destruction of a
Does the destructor deallocate memory assigned to the object which it belongs to or
Does calling a destructor of an boost::lock object explicitly have any consequence at all?

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.