Click here to Skip to main content
14,733,541 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello guys..
Hope You Are At Good Sate Of Health. Well i'm trying to refresh the concept of operator overloading, I've written a simple code here:
#include <iostream>
using namespace std;

class Test
{
private:
	int a;
	int *b;
public:
    Test();
	Test(int x, int y);
	void print();
	Test(const Test& yourTest); // Copy Constructor -> const className(const className&)
	const Test& operator=(const Test& anotherTest); // Assignment Operator -> const className &operator=(const className&)
	~Test(); // Destructor
};
Test::Test()
{
    a = -69;
    b = new int;
}
Test::Test(int x, int y)
{
	cout << "Constructor Called .. " << endl;
	a = x;
	b = new int(y);
	//*b = y;
}
void Test::print()
{
	cout << "A = " << a  << "\t" << "B = " << b << "\n\n";
}
Test::Test(const Test& yourTest)
{
	cout << "Copy Constructor Called .. " << endl;
	a = yourTest.a;
	b = new int;
	*b = *(yourTest.b);
}
const Test& Test::operator=(const Test& anotherTest)
{
	cout << "Assignment Operator Called .. " << endl;
	if (this != &anotherTest){
		a = anotherTest.a;
		delete b;
		*b = *(anotherTest.b);
		return *this;
	}
	else
	cout << "Self Assignment .. " << endl;

}
Test::~Test(){
	delete b;
}
int main()
{
	Test T1(2,3)
	Test T2(T1);
	T1.print();
	T2.print();
	return 0;
}


The issue is, the value of int *b is garbage, could you please help me to identify the mistake .. ??!

I keenly waiting You Reply ..
Posted

You should print *b instead of b, in the Test::print function.


Another issue: You should remove the delete b; line from operator=. You don't have to delete the memory, just assign a new value to it.

   
v2
Comments
Usman Hunjra 6-Apr-14 3:40am
   
Thank you sir.. :)
But it also give the error while i build the project how ever it compiles correctly... i.e
error link 2019: unresolved external symbol_WinMain@16 reference in function__Tmain CRT stratup
error link1120: 1 unresolved externals

Can you please rectify that .. :?
#include <conio.h>
#include <iostream>
using namespace std;
 
class Test
{
private:
	int a;
	int *b;
public:
    Test();
	Test(int x, int y);
	void print();
	Test(const Test& yourTest); // Copy Constructor -> const className(const className&)
	const Test& operator=(const Test& anotherTest); // Assignment Operator -> const className &operator=(const className&)
	~Test(); // Destructor
};

Test::Test()
{
    a = -69;
    b = new int;
	*b=0;
}

Test::Test(int x, int y)
{
	cout << "Constructor Called .. " << endl;
	a = x;
	b = new int;
	*b = y;
}

void Test::print()
{
	cout << "A = " << a  << "\t" << "B = " << *b << "\n\n";
}

Test::Test(const Test& yourTest)
{
	cout << "Copy Constructor Called .. " << endl;
	a = yourTest.a;
	b = new int;
	*b = *(yourTest.b);
}
const Test& Test::operator=(const Test& anotherTest)
{
	cout << "Assignment Operator Called .. " << endl;
	if (this != &anotherTest){
		a = anotherTest.a;
		//delete b;
		*b = *(anotherTest.b);
		return *this;
	}
	else
         {
	cout << "Self Assignment .. " << endl;
         return *this; 
         }
 
}

Test::~Test(){
	delete b;
}

int main()
{
	Test T1(2,3);
	Test T2(T1);
	T1.print();
	T2.print();

	getch();
	return 0;
}
   
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900