0

i'm trying to make a difference between different types of initializing objects. But I've read here(middle to end of page) that if you try to call Class object(); this will not instantiate an object, rather will define a function? I tried and it is so, but I don't understand why.

Circle c; // default constructor called

Circle c(); // function declaration (default constructor NOT called)

Circle c{}; // default constructor called


Here is how i tried to reproduce:

class Circle
{
    float r;
public:
    Circle()
    {
        cout << "default constructor" << endl;
    }
    Circle(float r)
    {
        this->r = r;
    }
};

In main() I'm trying to instantiate an object like this Circle c() and it does not call the parameter less constructor. I was expecting seeing in console the message "default constructor". What does the syntax Circle c() does if it is not calling the default constructor? How is that a valid function declaration?

Community
  • 1
  • 1
Cătălina Sîrbu
  • 1,253
  • 9
  • 30

0 Answers0