Reputation: 29
I was going through the below link to understand the topic. Over Loading Typecasts C++
class Cents
{
private:
int m_nCents;
public:
Cents(int nCents=0)
{
m_nCents = nCents;
}
// Overloaded int cast
operator int() { return m_nCents; }
int GetCents() { return m_nCents; }
void SetCents(int nCents) { m_nCents = nCents; }
};
Now in our example, we call PrintInt() like this:
int main()
{
Cents cCents(7);
PrintInt(cCents); // print 7
return 0;
}
I cannot understand How PrintInt(cCents) maps to to printing 7 and using the overloaded int operator.
Upvotes: 0
Views: 1108
Reputation: 3143
Overloading int cast means when a Cents
variable is casted to int, it'll return the m_nCents
// Overloaded int cast
operator int() { return m_nCents; }
Upvotes: 2