Jump to content
  • Advertisement
Sign in to follow this  
ndwork

Explicit Initialization

This topic is 3455 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

If one has an object with a constructor that accepts two parameters (let's say). And then one constructs those objects with two parameters and explicit initialization (see below for clarity), what happens?
int a=0, b=0;
myObj( a, b ) = 3;

Share this post


Link to post
Share on other sites
Advertisement
The only way you could get that code to even compile is to either provide an operator int&() or have the myObj assignment operator return a non-const reference.

Assuming "myObj" is a class name, not a variable name, you are constructing a temporary of type myObj and attemting to assign the integer value 3 to that object.

Share this post


Link to post
Share on other sites
Depends entirely on what myObj is.

If myObj is a type, then myObj( a, b ) constructs a temporary unnamed variable using the two-parameter constructor, and then the assignment operator is called on that temporary object, passing the value 3 to it. This requires that you have provided an overloaded assignment operator that takes an integer, or another assignment operator (including the default compiler generated assignment operator) and that integers can be implicitely converted to the parameter type of that assignment operator.

If myObj is an object, then that line will call operator()(int, int) and call the assignment operator on the return value. This assumes the function call operator is properly overloaded, and that myObj is actually initlaized already, so asking about futher initializations makes no sense.

There are many assumptions needed to be made to answer your question. So instead, how about you actually provide a working example that demonstrates your question.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!