Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > BOOK: Professional C++
|
BOOK: Professional C++
This is the forum to discuss the Wrox book Professional C++ by Nicholas A. Solter, Scott J. Kleper; ISBN: 9780764574849
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional C++ section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 12th, 2009, 03:05 AM
Registered User
 
Join Date: Nov 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Having problem in operator overloading

Hi
I have problem in operator overloading.
In c++ first i create file h1.h and write this code into
Code:
class a;
class b
{
   friend class a;
   private:
      int data;
};
Then i create file h2.h and write this code into
Code:
#include "h1.h"
class a
{
   public:
      a(int);
      ~a();
      int get(){ return node->data;}
      a operator+(const a&);
      a operator=(const a&);
   private:
      b* node;
};
//`````````````````
a::a(int d)
{
   node = new b;
   node->data = d;
}
//`````````````````
a::~a()
{
   delete node;
}
//`````````````````
a a::operator+(const a& b)
{
   int data = ( node->data + (((b.node)->data)) );
   a temp(data);
   return temp;
}
//`````````````````
a a::operator=(const a& b1)
{
   delete node;
   node = new b;
   node->data = (b1.node)->data;
   return *this;
}
after all i create file main.cpp and insert the following code into:
Code:
#include "h2.h"
#include <conio.h>
#include <iostream.h>
int main()
{
   a a1(20);
   a a2(30);
   a a3(40);
   a3 = a1 + a2;
   cout<<a3.get();
   getch();
   return 0;
}
When I compile the Main.cpp no error occurs but if program works true must print 50 in screen,but it print number such as 9890.
Can Any One Help Me?
Thanks
 
Old March 29th, 2010, 08:33 PM
Authorized User
 
Join Date: Nov 2009
Posts: 26
Thanks: 1
Thanked 1 Time in 1 Post
Default

That was exactly my own problem.

When you dynamically allocate memory for pointers( as a filed of your object) and your program have functions and operators that accept objects as arguments, you need to create a copy constructor. Why?

Two important parts that trigger the copy constructor are:
1-passing objects to functions
2-functions that return objects

Think in this way:
Because objects are data types that contain other data types in themselves, in these two situations they need to copy their contents to the corresponding variable in the function's parameter list. Even if a function's parameters are reference type (like your = and + operators).

To speak generally, when these types of functions are called through your program, copy constructor comes to the scene. If you haven't defined a copy constructor, a default copy constructor is created by the compiler and initializes your objects in these functions. The results is loss of data like unexpected number you get through your program execution

Here is the definition of a copy constructor for your class a:
Code:
a(a &);
Note that copy constructor always gets a reference to object of it's own class.

This is the implementation:
Code:
a::a(a &obj)
{
node = new b;
node->data=(obj.node)->data;
}
Remember that copy constructor calls the destructor automatically. Because of that, we did not write "delete node;" at the beginning.

So, be sure to add these codes to your class a
I hope you understood the copy constructor.There are lots written about it in many books.

Thanks for such a good question !!!!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Operator overloading issue stuartlittle C# 2008 aka C# 3.0 6 October 31st, 2009 07:09 AM
Invalid operator for data type. Operator equals di Pusstiu SQL Server 2000 2 August 10th, 2007 04:51 AM
Operator Overloading In Inheritance ahmedsalam C# 0 October 18th, 2005 11:25 AM
operator overloading in VB.NET. rupen General .NET 4 April 21st, 2005 10:15 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.