About Operator overloading please help

hi bros and experts
i have two question about this chapter of c++ contain.
1-please tell me about the argument, i mean why unary operator dont have arguments and totaly do me a favor and tell me about getting argument of the operators should be have.
2-why this block of class code cant have object that we can do something on it like this:
ob1=++ob2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Counter
{
private:
unsigned int count;
public:
Counter() : count(0)
{ }
unsigned int get_count()
{ return count; }
void operator ++ ()
{
++count;
}
};
int main()
{
Counter ob1, ob2;
ob1=++ob2;
}

Last edited on
closed account (yUq2Nwbp)
answer for your first question.............when you write ++ob1 it means

ob1.operator++()

and it works with the count of ob1......so there is no point to have parameters for operator........

and answer for your second question....

your operator++() returns nothing so you can't ascribe ob1 to ++ob2....change return type of operator++() for example to int...
closed account (D80DSL3A)
@david91 Returning an int won't work directly because a Counter isn't equivalent to an int. ie:
Counter = int won't make sense to the compiler unless you tell it how to convert.
A constructor which takes an int argument would tell the compiler how to "convert" an int to a Count object.
The following code works:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Counter
{
private:
	unsigned int count;
public:
	Counter() : count(0){}
	Counter(int x) : count(x){cout << "int ctor called\n";}// this allows a cast from int to Counter to be made
	unsigned int get_count(){ return count; }
	int operator ++ (){ return ++count;}
};

int main()
{
	Counter ob1, ob2;
	ob1=++ob2;
	cout << "ob1.count = " << ob1.get_count() << "  ob2.count = " << ob2.get_count();

  	cout << endl;
	return 0;
}

Output:

int ctor called
ob1.count = 1 ob2.count = 1

You can see that the new constructor was called to make the conversion.
At any rate, I believe the correct way is to return a Count& from the ++operator:
1
2
3
4
5
Counter& operator ++ ()
{
	++count;
	return *this;// return a reference to the calling object
}

Output:
ob1.count = 1 ob2.count = 1

You can see that the int constructor was not called this time.
thanx for you two reply alot
yeah int ddnt work as compiler ddnt find any match for return but fun2code bro thanx for ur code
and may i use a temp for a return value i mean temp on operator function?
closed account (yUq2Nwbp)
fun2code yes of course.....i wasn't careful.....of course returning type must be Counter...
closed account (D80DSL3A)
You may use a temp for returning a value. This is useful for overloading the post-increment operator (as opposed to the pre-increment operator which you have already defined).
Consider:
1
2
3
4
5
6
Counter operator ++ (int)// don't return a reference in this case. Returning address of local variable = no-no
{
	Counter temp;
	temp.count = count++;
	return temp;
}

Then this code in main() :
1
2
3
Counter ob1, ob2;
ob1=ob2++;// post-increment
cout << "ob1.count = " << ob1.get_count() << "  ob2.count = " << ob2.get_count();

produces:
ob1.count = 0 ob2.count = 1

@david91. I figured it was an oversight. Still, I think experimenting like this improves understanding of how everything works together, so it's worthwhile.

EDIT: The above operator definition can be simplified if we make use of the int based constructor introduced in my previous post:
1
2
3
4
Counter operator ++ (int)
{
	return Counter(count++);// constructing the returned object right here
}
Last edited on
closed account (yUq2Nwbp)
your last version is right but you create a temporary object which isn't necessary......so i prefer your this version

Counter& operator ++ ()
{
++count;
return *this;// return a reference to the calling object
}
Last edited on
closed account (D80DSL3A)
That was for the pre-increment operator( ++obj ).
My last post was about the post-increment operator( obj++ ).
Last edited on
closed account (yUq2Nwbp)
in that case i agree with you...........it is very short and correct version...thanks...
fun2code i understanding why u add a argument on operation function lemme explain and please notify me if there is any misunderstanding in my explanation:
u add one ctor for declare count to default value and then declare another ctor for returning x to oporeation function(set it to int for conversion)at last our operation function with argument going for ctor that have argument and get return value.then add a post fix ++ on a value of our object.
and another is i think for doing postfix we have to add a argument for telling to compiler that we have integer before a operator like:
our integer is
OBJ1 and then we have operation after that
is my analysis right?
closed account (D80DSL3A)
I think your analysis may be over complicating the way it works. Sorry if my several posts led to that.
On each point:
u add one ctor for declare count to default value

The 1st ctor is the one you already had: Counter() : count(0){}

and then declare another ctor for returning x to oporeation function

I added the 2nd ctor:
Counter(int x) : count(x){)
so that it is possible to convert an int value to a Counter object. It is needed if we want the post-fix operator to be defined like this:
1
2
3
4
Counter operator ++ (int)
{
	return Counter(count++);// int ctor used here
}

However, if we do this instead:
1
2
3
4
5
6
Counter operator ++ (int)
{
	Counter temp;// your original ctor used here
	temp.count = count++;
	return temp;
}

then the int ctor isn't needed at all.

About the post-fix operator. I threw that in there because you had asked about using a temp Counter variable in the ++ operator. A temp Counter variable is needed for the post-fix form, but not for the pre-fix form.
i think for doing postfix we have to add a argument for telling to compiler that we have integer before a operator


I haven't ever seen a satisfying explanation for the int argument being used except that it is there to distinguish the function from the pre-fix one and it is just done that way. Note that no variable name is supplied with the argument as we never intend to actually use it.

To summarize, I have the following for your Counter class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Counter
{
private:
	unsigned int count;
public:
	Counter() : count(0){}
	Counter(int x) : count(x){}// needed only for making the postfix ++ operator work
	unsigned int get_count(){ return count; }
	Counter& operator ++ ()// prefix
	{
		++count;
		return *this;
	}
	Counter operator ++ (int)// postfix
	{
		return Counter(count++);// int ctor is used here
	}
};

I hope this reply is helpful and did not make the confusion worse.

got it bros u rock thanx alot for ur replying hope someday u'll get favors back in ur some complicated project:D
bros hi again
another question and another favors from yours.
see these code below
declared an overloading for + operator that have one arg as type of Distance class.
ok dont have problem with that but see in main and tell how its function work.i dont have problem for something like this and working like a charm.
dist3=dist1+dist2;
but how about that i add another object and use it as a operand.
like this
1
2
Distance dist5(10,6.75);
dist3=dist1+dist2+dist5;

u can see i add another operand but still have one arg but its work without problem why?
unless dont we have to add another arg for another operand?
i know for first operand we have direct access to class member but after operator we have class type access like d2.dist1 but ddnt got it how is that work without problem?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <conio.h>

using namespace std;

class Distance{
    int feet;
    float inches;
public:
Distance():feet(0),inches(0.0)
{}
Distance(int ft,float in):feet(ft),inches(in)
{}
void GetDis()
{
    cout<<"Please Enter feet then inches:";
    cin>>feet>>inches;
}
void ShowDist()
{ cout << feet << " - " << inches << " "; }

Distance operator +(Distance) const;
};
Distance Distance:: operator+(Distance d2) const
{
     int f=feet+d2.feet;
     float i=inches+d2.inches;
     if(i>=12.0){
     i-=12;
     f++;
     }
     return Distance(f,i);
}

int main()
{
    Distance dist1,dist3,dist4;
    dist1.GetDis();

    Distance dist2(11,6.2);
    dist3=dist1+dist2;
    dist4=dist3+dist2+dist1;
    cout << "dist1 = "; dist1.ShowDist(); cout << endl;
    cout << "dist2 = "; dist2.ShowDist(); cout << endl;
    cout << "dist3 = "; dist3.ShowDist(); cout << endl;
    cout << "dist4 = "; dist4.ShowDist(); cout << endl;
    return 0;
}
Last edited on
1
2
3
4
5
6
dist1 + dist2; //same as
dist1.operator+(dist2);
//----------------------------------------
dist1 + dist2 + dist3; //same as
(dist1 + dist2) + dist3; //same as
dist1.operator+(dist2).operator+(dist3);
Last edited on
closed account (yUq2Nwbp)
hexa look....you said

dist4=dist3+dist2+dist1;....it works in this way....

depend on your compiler implementation it starts count from rigth or left....but it doesn't matter...suppose it starts count from left....

first it does this---> dist3.operator(dist2)......it returns temporary Distance object....lets name it dist4...then it does dist4.operator+(dist1).......that's all.......

in other words it works in this way.. (dist3.operator+(dist2)).operator+(dist1)........is it clear to you??

got it bros thanx i thought exactly what u clarified for me here for sure asking.
thanx mathhead200 and u david91 bro hope its help for some another noobs here.
closed account (yUq2Nwbp)
you are quite welcome Hexa
Topic archived. No new replies allowed.