Max Green
Max Green

Reputation: 25

C++ Instantiating "Implicit" Class Object

class Cents
{
private:
int m_nCents;

public:
Cents(int nCents) { m_nCents = nCents; }

// Overload cCents + int
friend Cents operator+(const Cents &cCents, int nCents);
int GetCents() { return m_nCents; }
};

// note: this function is not a member function!
Cents operator+(const Cents &cCents, int nCents)
{
return Cents(cCents.m_nCents + nCents);
}

int main()
{
Cents c1 = Cents(4) + 6;
std::cout << "I have " << c1.GetCents() << " cents." << std::endl;

return 0;
}

It's not clear to me how the expression

Cents(4)+6 

in line

Cents c1 = Cents(4) + 6;

is evaluated. Yeah I know that we're overloading operator "+" for operands of types Cents and int respectively.

As I understand Censt(4) is the constructor, right? So when

Cents operator+(const Cents &cCents, int nCents)
 {
 return Cents(cCents.m_nCents + nCents);
 }

is called does cCenst become a reference to Cents(4)?

From the line

return Cents(cCents.m_nCents + nCents);

one can deduce that cCenst is an object of type Censt since we access m_nCents via member selection operator "." But Censt(4) is a constructor and not a class object.

To me it doesn't seem to make sense for cCenst to be a reference to Cents(4) since they're not equivalent.

Upvotes: 1

Views: 686

Answers (3)

davmac
davmac

Reputation: 20671

As I understand Censt(4) is the constructor, right?

Cents(4) is an expression which creates a Cents object (resulting in a call to the constructor). The result of evaluating the expression is the object thus created.

So when [...] is called does cCenst become a reference to Cents(4)?

It becomes a reference to the object that was created when the Cents(4) subexpression was evaluated.

From the line return Cents(cCents.m_nCents + nCents); one can deduce that cCenst is an object of type Censt since we access m_nCents via member selection operator "."

cCents is of type const Cents &, because that is how it is declared.

But Censt(4) is a constructor and not a class object.

Cents(4) is an expression. It is not a constructor. To elaborate: it is an expression, which requires calling of a constructor to evaluate, and which results in a Cents object.

In the expression:

Cents c1 = Cents(4) + 6;

The sub-expressions are evaluated first (according to operator precedence etc). So, Cents(4) is evaluated and becomes a Cents object. The overall expression could then be considered to be:

Cents c1 = <a-newly-created-Cents-object> + 6;

The <a-newly-created-Cents-object> + 6 part is evaluated by next, by calling the defined + operator. In that operator method, the cCents parameter becomes a reference to <a-newly-created-Cents-object>.

Upvotes: 1

Gabrielle de Grimouard
Gabrielle de Grimouard

Reputation: 2005

the constructor will create the object so after you get an object and an int so it will take the object. It's not exactly a reference it's a const reference so you don't need to created it before because the object can't be modified.

Upvotes: 0

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385405

As I understand Censt(4) is the constructor, right?

No, not quite. You never call a constructor directly, even though this syntax makes it kind of seem like you do.

Here you're constructing a temporary of type Censt with constructor argument 4.

Think of it more like this:

Censt x(4);  // creates a `Censt` with name `x` from argument `4`
Censt(4);    // creates a `Censt` with no name from argument `4`

It's not a function call.

does cCenst become a reference to Cents(4)?

Yes.

But Censt(4) is a constructor and not a class object.

Again, no. It is an object.

Upvotes: 3

Related Questions