confusion with snippet

Hi,

I am confused about what data structure this code snippet is. Can someone disambiguate?
HERE ****
1
2
3
4
5
 
Cents operator+(Cents &c2, Cents &c1)
{
	return {c2.getVal() + c1.getVal()};
}


??Is this a type of function? Is Cents instantiating an object here - operator?

Full:

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
#include<iostream>

class Cents
{
private:
	int priVal;

public:
	Cents(int ConCents){priVal = ConCents;};
	int getVal(){return priVal;};
};

Cents operator+(Cents &c2, Cents &c1)
{
	return {c2.getVal() + c1.getVal()};
}

int main()
{
	Cents cents1(87);
	Cents cents2(990);
	Cents ObjPlus = cents2 + cents1;
	std::cout<< ObjPlus.getVal();

	return 0;
}
Last edited on
??Is this a type of function?

Yes, this is an overload of the + operator.

The first snippet (and lines 13-16) are an overload of the + operator, allowing two Cents objects to be added together (see line 22). The values from c2 and c1 are added together and returned in a new object.

BTW, it is not necessary to use the getter getVal() in the overloaded operator. The operator overload has access to private in both c 1 and c2.


Edit: Removed last paragraph.
Last edited on
Hi AA,

Thx for the reply. I get it, just thought the fx would have some sort of"type".

Also, in my noobishness a semantics question: why is it referred to as "overloaded"?

That would help a great deal. Thx
In C++, it is possible to define multiple functions with the same name, differing only in terms of their formal parameter list and optionally their return values.
1
2
3
4
int foo(int) {}
int foo(double) {}
int foo() {}
double foo(char) {}

Those functions are called "overloaded", each foo is a particular overload.

Conceptually, an + is just another function -- one with (at least) "implicit" overloads for primitives --- int and double and such. That's not exactly true, but you are regardless overloading the meaning of `+' for another type.

Edit:
Actually functions do have a type. The expression &Cents::operator+ has a type of Cents (Cents::*)(Cents&, Cents&).

It's not a member function, and if it was, the signature wouldn't make sense.
Last edited on
Some clarifications. This is the code that I am seeing (in case the OP has been or will be edited)...

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
#include<iostream>

class Cents
{
private:
	int priVal;

public:
	Cents(int ConCents){priVal = ConCents;};
	int getVal(){return priVal;};
};

Cents operator+(Cents &c2, Cents &c1)
{
	return {c2.getVal() + c1.getVal()};
}

int main()
{
	Cents cents1(87);
	Cents cents2(990);
	Cents ObjPlus = cents2 + cents1;
	std::cout<< ObjPlus.getVal();

	return 0;
}


The + operator being defined is not a member function of the Cents class. As such, the type is not Cents(Cents::*)(Cents&, Cents&), but Cents(*)(Cents&, Cents&).

Also, because the function is not a member function, the getVal() function IS necessary.
@doug4 - Good catch on operator + not being a member function. I missed that.
Topic archived. No new replies allowed.