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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:54:33+00:00 2026-06-12T18:54:33+00:00

#include <iostream> using namespace std; class Exem { int *a; public: Exem() { a

  • 0
#include <iostream>
using namespace std;

class Exem {
    int *a;

    public:
        Exem() { a = new int; *a = 0; };
        Exem (int x) { a = new int; *a = x; };

        ~Exem () { delete a; };

        int f (void);

        Exem operator+ (Exem);
};

int Exem::f (void) {
    return *a * 2;
}

Exem Exem::operator+ (Exem nimda) {
    Exem aux;

    *aux.a = *a + *nimda.a;

    return aux;
}

int main() {
    Exem adabo(1);
    Exem inakos(2);

    adabo = adabo + inakos;

    cout << adabo.f();
    cin.get();
}

This is my code, an example class made to showcase the problem. The output of main() would, in theory, be ‘6’, but all that actually shows up are nonsensical numbers.

This apparently has to do with the class’ destructor, which, from what I understood, get called too early at the end of the operator+ function – aux gets lost before it is actually passed. I came to such conclusion because ~Exem(), when commented, allows the program to execute as expected.

I’m guessing this has to do with these two compilers, because when I tried to compile the exact same code in Embarcadero RAD Studio it would work.

  • 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-12T18:54:34+00:00Added an answer on June 12, 2026 at 6:54 pm

    You need to explicitly define a copy constructor and assignment operator for Exem as you have a dynamically allocated member variable.

    If a copy constructor and assignment operator are not explicitly defined for a class the compiler generates default versions of these, which are not suitable for a class that has dynamically allocated members. The reason the default generated versions are unsuitable is that they perform a shallow copy of the members. In the case of Exem, when an instance of it is copied more than one instance of Exem is pointing to the same dynamically allocated int member named a. When one of the instances is destroyed that a deleted, and leaves the other instance with a dangling pointer and undefined behaviour.

    See the rule of three.

    A simple fix for Exem would be change a from an int* to an int. The default copy constructor, assignment operator and destructor would be correct.


    Note that Exem::operator+() should take a const Exem& parameter as it does not change its argument.

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

Sidebar

Related Questions

#include <iostream> using namespace std; class Imp { public: int X(int) {return 50;} int
#include <iostream> using namespace std; class B { public: int getMsg(int i) { return
#include<iostream> using namespace std; class A { private: int value; public: A(int init):value(init){} void
#include<iostream> using namespace std; class A { private: const int a=9; public: void display()
#include <iostream> using namespace std; struct testarray{ int element; public: testarray(int a):element(a){} }; class
#include <iostream> using namespace std; class A{ int b; public: A(){ cout<<Constructor for class
#include<iostream> using namespace std; class Abc { public: int a; Abc() { cout<<Def cstr
Consider this code: #include <iostream> using namespace std; class hello{ public: void f(){ cout<<f<<endl;
#include <iostream> using namespace std; class Ex { private: int i; float f; public:
#include<iostream> using namespace std; class Abs { public: virtual void hi()=0; }; class B:public

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.