Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > BOOK: Professional C++
Password Reminder
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
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

I have problem in operator overloading.
In c++ first i create file h1.h and write this code into
class a;
class b
   friend class a;
      int data;
Then i create file h2.h and write this code into
#include "h1.h"
class a
      int get(){ return node->data;}
      a operator+(const a&);
      a operator=(const a&);
      b* node;
a::a(int d)
   node = new b;
   node->data = d;
   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:
#include "h2.h"
#include <conio.h>
#include <iostream.h>
int main()
   a a1(20);
   a a2(30);
   a a3(40);
   a3 = a1 + a2;
   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?
  #2 (permalink)  
Old March 29th, 2010, 08:33 PM
Authorized User
Points: 100, Level: 1
Points: 100, Level: 1 Points: 100, Level: 1 Points: 100, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Nov 2009
Posts: 26
Thanks: 1
Thanked 1 Time in 1 Post

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:
a(a &);
Note that copy constructor always gets a reference to object of it's own class.

This is the implementation:
a::a(a &obj)
node = new b;
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 !!!!

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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

All times are GMT -4. The time now is 04:29 PM.

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