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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:13:31+00:00 2026-05-26T13:13:31+00:00

myClassVar = MyClass(3); I expected destructor being called on the previously created myClassVar on

  • 0
myClassVar = MyClass(3);  

I expected destructor being called on the previously created myClassVar on the left.
But it is actually being called on the new object that’s created by MyClass(3).

My full test code and output follows..

edit

How do I fix the problem?
Implement an assignment operator?
MyClass actually has pointers, and MYSQL_STMT*, I wonder how should I deal with MYSQL_STMT* variable.

I just need MyClassVar(3) object not the MyClassVar() which was first created when ClientClass object was created.

I came across this situation fairly often, and wonder if there’s a good way to do it.

#include <stdio.h>

class MyClass
{
public:
    MyClass() { printf("MyClass %p\n", this); }
    MyClass(int a) { printf("Myclass(int) %p\n", this); }
    ~MyClass() { printf("~MyClass %p\n", this); }

private:
    int mA;
};


class ClientClass
{
public:
    void Foo()
    {
        printf("before &myClassVar : %p\n", &myClassVar);
        myClassVar = MyClass(3); // this is the important line
        printf("after &myClassVar : %p\n", &myClassVar);
    }

private:
    MyClass myClassVar;
};

int main()
{   
    ClientClass c;
    c.Foo();
    return 0;
}

MyClass 0x7fff5fbfeba0
before &myClassVar : 0x7fff5fbfeba0
Myclass(int) 0x7fff5fbfeb70
~MyClass 0x7fff5fbfeb70 // <--- here destructor is called on the newly created object
after &myClassVar : 0x7fff5fbfeba0
~MyClass 0x7fff5fbfeba0
  • 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-26T13:13:31+00:00Added an answer on May 26, 2026 at 1:13 pm

    Here’s how the critical line breaks down:

    myClassVar = MyClass(3);
    

    First, MyClass(3) calls constructor and returns the object.

    Second, myClassVar = copies the object to myClassVar.

    Then the statement ends. The object (which is an immediate) is dead, and thus the destructor is invoked.

    EDIT :

    As for how to get around this. The only way I can think of is to use a placement new. I’m not sure if there’s a better solution other than making a “set” method.

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

Sidebar

Related Questions

Given the following... public MyClass { public void MyClass() { var myWorkerClass = new
I have a custom class that looks like: function myClass(){ var thing; var thingtype;
I have a Flex tree control that holds a tree full of various types
void SomeClass::mySlot(MyClass *var){ ... } void SomeClass::SomeFunction(){ MyClass *myVar; QPushButton *button = new QPushButton(this);
I have created a class like so: function MyClass() { var myInt = 1;
I recently refactored code like this ( MyClass to MyClassR ). #include <iostream> class
Let's say I have: class myClass std::list<myClass> myList where myClass does not define the
Is there a way in C++ to construct your class such that given a
in java is it possible to make a method aware of who called it
Hay, when should variables be set within a PHP class? <?php class MyClass {

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.