0

I am working through Scott Myers Effective C++. The following appears in the text.

enter image description here

I am having difficulty understanding what is going on with the valid syntactical statement doSomething(B(28));

How exactly is an object of class B created out of B(28), for doSomething() requires an argument of type B bObject.

Further, suppose the full definition of class B is as follows:

class B{
    int b;
public:
    explicit B(int x = 0, bool b = true);
}

Also, suppose doSomething() is defined thus:

void doSomething(B bObject){
    bObject.b = 1;
}

with doSomething(B(28));, how is the b member accessed? That is, of what object is member variable b accessed?

2
  • 2
    Did the book not go over that class_name(parameters) creates a temporary object of class_name with the specified parameters? Commented Oct 3, 2017 at 14:50
  • 1
    The temporary (B(28)) creates an object and assign it to bObject, for the duration of doSomething scope. after that, the object is destroyed Commented Oct 3, 2017 at 14:57

1 Answer 1

1

B is a class that has a valid constructor that takes an int parameter. That is to say that B knows how to construct itself when passed an int.

28 is a valid representation of an int.

B(28) Is a way to instantiate a 'B' class, using the constructor that takes an int.

To clarify a little further, the constructor for 'B' will take an int and a bool, but both parameters are "optional", which is indicated by the "= 0" for the int parameter, and the "= true" for the bool parameter.

Since the parameters are optional, the B can construct with no parameters specified, in which case it will construct with x=0 and b=true. It can also construct with the int parameter specified, as in your example. In your example case, x=28, and b=true. It can also be constructed with both parameters specified. e.g. B(8, false). It is not, however, possible to only specify the bool parameter. B(false), for example, will not work. There is no way to skip parameters when filling in optional parameters. B(,false) is not valid...

So, B(28) constructs a B object with x=28.

Now, the function DoSomething expects to be passed a 'B' object. Since B(28) is a valid construction of a 'B' object, it satisfies the parameter list for DoSomething.

When you call:

DoSomething(B(28));

A 'B' object is constructed, and passed to DoSomething as the argument.

Inside the function, B is referred to as 'bObject', because the function signature:

DoSomething(B bObject)...

This function signature wants something of type 'B' to be passed, and it will be referred to as bObject inside the function.

Thus,

bObject.b = 1;

Assigns the value of 1 to the 'b' member of the B class that was constructed and passed to the function.

Perhaps the reason this is somewhat confusing is that after DoSomething gets done setting the b member of the B class, and the function exits, the B class no longer exists, so there is no way after exiting the DoSomething function to prove that the b member got modified.

It is therefore a somewhat useless thing to do (to pass a temporary object to a function, and modify it, then let it go out of existence.) as far as I can tell.

What the author really seems to be trying to explain is the significance of 'explicit' in the constructor. The example helps to make his point on explicit constructors excluding implicit conversions on arguments, or something.

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

Comments

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.