1
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.

4
  • You should reduce the number of tags here and apply the c++ tag to your question (if this is c++ that is). Commented Feb 7, 2015 at 18:01
  • I don't see what this question has to do with operator overloading. Why couldn't you pose it as it relates to some arbitrary function void foo(const Cents&) with the usage example being foo(Cents(4))? Commented Feb 7, 2015 at 18:32
  • Yeah I must admit that my example has little to do with operator overloading but rather it is about passing a class object to a function by reference. Yet I wouldn't edit the code and remove operator overloading stuff 'cause I cannot come up with an example that would make sense. But taking into account your wise remark I will change the question title. Commented Feb 7, 2015 at 19:39
  • @MaxGreen: Thanks; that's quite a lot better now. Commented Feb 7, 2015 at 20:04

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Now that you told me that Cents(4) creates an unnamed object I understand how function parameter "cCenst" can be a reference to it. Thanks)
To be more precise, Cents(4) is an expression that is an rvalue. Just as 4 is an rvalue.
@KubaOber: One level of abstraction up, it's an object. Two levels of abstraction down, it's a sequence of characters in a source code file. Being precise is great but context is everything.
I just thought that it's useful to bring the term into the discussion, as it's quite hard to talk about most of modern C++ without making the distinction. I'm not saying that somehow you erred greatly by not mentioning it :)
1

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>.

Comments

0

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.

3 Comments

No, constructors don't "return" anything.
Yes you right it wasn't the word that I would write. I changed it.
Constructors don't "create the object", either. At least, not in entirety. They initialise data members. Memory allocation and other stuff is handled separately by the language. That's why this is not "calling a constructor"; indeed, constructors do not even have names (12.1/1)!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.