Jump to content
  • Advertisement
Sign in to follow this  
MadsGustaf

Why does my program act like this?

This topic is 4233 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

code:
#include <iostream>
#include <string>

using namespace std;

class Mads
{
 public:
  Mads(const string& name = "")
  {
   cout << "Constructor called!\n";
   m_pName = new string(name);
  }//end constructor
  
  ~Mads()
  {
   cout << "Destructor called!\n";
   delete m_pName;
  }//end destructor
  
  Mads(const Mads& m)
  {
   cout << "Copy constructor called!\n";
   m_pName = new string;
   *m_pName = m.GetName();
  }//end copy cunstructor
  
  Mads& operator=(const Mads& m)
  {
   cout << "The Overloaded Operator is called!\n";
   if (this == &m)
  return *this;
   
   else
   { *m_pName = m.GetName();
    return *this; 
   }
  }//end of overloading assignment operator

string GetName() const { return *m_pName; }
void SetName(const string& name = "") { *m_pName = name; }
void SayHi() const { cout << "Hello!, my name is " << GetName() << endl; }
//end of public section

 private:
  string* m_pName;
};
//end of the Mads class
void testDestructor();
void testCopyConstructor(Mads kopy);
void testAssignmentOp();
 
 
int main()
{
 testDestructor();
 cout << endl;
 
 Mads mads1("Fjering");
 cout << "&mads1: " << &mads1 << endl;
 mads1.SayHi();
 testCopyConstructor(mads1);
 cout << endl;
 
 testAssignmentOp();
 
return 0;
}//end of main()

void testDestructor()
{
 Mads mads1("Gense");
 cout << "Adressen af &mads1: " << &mads1 << endl;
 mads1.SayHi();
}

void testCopyConstructor(Mads kopi)
{
 kopi.SayHi();
 cout << "Adressen af &kopi " << &kopi << endl;
}

void testAssignmentOp()
{
 Mads mads1("Dendrit");
 Mads mads2("Goble");
 cout << "&mads1: " << &mads1 << endl;
 cout << "&mads2: " << &mads2 << endl;
 mads1 = mads2;
 cout << "&mads1: " << &mads1 << endl;
 cout << "&mads2: " << &mads2 << endl;
 mads1.SayHi();
 mads2.SayHi();
 
 cout << "Sætter navnet på mads1, tilbage på 'mads1'\n";
 mads1.SetName("Dendrit");
 mads1.SayHi();
 mads2.SayHi();
 cout << "&mads1: " << &mads1 << endl;
 cout << "&mads2: " << &mads2 << endl;
 
 Mads mads3("Ninja");
 mads3 = mads3;
}

when i run it, the testAssignmentOp() function displays "Calling Destructor" 4 times, but as far as i can see, only 3 objekts to the Mads class, and therefore, only 3 local pointers to memory on the heap was ever created. Hopefully someone can clear this up for me! :)

Share this post


Link to post
Share on other sites
Advertisement
The 'fourth' destructor is due to:
Mads mads1("Fjering");

From the main function being destroyed at the end of the program.

This will show it more clearly:
int main()
{
testDestructor();
cout << endl;

Mads mads1("Fjering");
cout << "&mads1: " << &mads1 << endl;
mads1.SayHi();
testCopyConstructor(mads1);
cout << endl;

testAssignmentOp();
cout << "Line before the end of the program" << endl;

return 0;
}//end of main()

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!