0
class Point
{
private:
int x, y;
public:
Point()
{
}
// Parameterized Constructor
Point(int x1)
{
 x=x1;
}
Point(int x1, int y1)
{
    x = x1;
    y = y1;
}
int getX()
{
    return x;
}
int getY()
{
    return y;
}
};
int main()
{
   Point p(10);
   p = Point(50,100);
}

does Point(50,100) returns an object??Can Anyone explain the details about the execution in main function. for assigning p with Point(50,100) it should give an object.

4
  • It does returns an instance of Point, which in turn is assigned to the p using implicitly-defined copy-assignment operator (memberwise copies all the data members, x and y in this case). Commented Jul 9, 2021 at 11:06
  • Why do you call it "parameterized constructor"? It is a regular constructor. Commented Jul 9, 2021 at 11:08
  • 1
    @IgorR i suspect some tutorial, for example this is a particularly poor one that uses the term and spreads more misinformation geeksforgeeks.org/constructors-c Commented Jul 9, 2021 at 11:13
  • fwiw, the distinction between a default constructor and a "parametrized constructor" as it is made eg in the link above is wrong and misleading. Default constructors can take parameters too, what distinguishes them is that they can be called without parameters. "parametrized constructor" is not a common term and neither is it very useful Commented Jul 9, 2021 at 11:14

1 Answer 1

1

Here Point(50,100) creates an object then calls the assignment operator of the class Point which is generated as default by the compiler and basically copies memory of righthand operand into left hand operand. After copying, it also calls the destructor for the Point(50,100).

You can check this by adding a destructor to your function.

~Point()
{
    std::cout<<"destructor"<<x<<std::endl;
}
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.