0

In the below two codes why the first code's object of class student "s2" is not created whereas object "s2" of code 2 is created. Why are just the "()" creating such difference?

    ///////////////CODE 1/////////////////
    #include <iostream>
    using namespace std;

    class Student
    {
        int marks;
    public:
        Student()
        {
            cout << "In" << endl;;
        }
        Student(int x)
        {
            cout << "Out" << endl;
            marks = x;
        }
    };

    int main()
    {
        Student s1(100);
        Student s2();
        Student s3 = 100;
        return 0;
    }
    //////////////////////////////////////

This is another code in which the "()" are absent i

///////////////CODE 2/////////////////
    #include <iostream>
    using namespace std;

    class Student
    {
        int marks;
    public:
        Student()
        {
            cout << "In" << endl;;
        }
        Student(int x)
        {
            cout << "Out" << endl;
            marks = x;
        }
    };

    int main()
    {
        Student s1(100);
        Student s2;
        Student s3 = 100;
        return 0;
    }
    //////////////////////////////////////
user3386109
  • 34,287
  • 7
  • 49
  • 68
Ankur Paul
  • 9
  • 1
  • 5
  • C and C++ are different languages. Please use only the correct tag. – kaylum Feb 26 '20 at 02:38
  • 1
    Does this answer your question? [Most vexing parse](https://stackoverflow.com/questions/5926103/most-vexing-parse) – ruakh Feb 26 '20 at 02:43

1 Answers1

0

This is similar to the issue of the most vexing parse. In essence, the line Student s2(); is not a variable declaration: you're declaring a function named s2 that takes no arguments.

Either use Student s2; or Student s2{};.

N. Shead
  • 3,828
  • 1
  • 16
  • 22
  • I think "most vexing parse" refers to specifically when there is an argument that looks like `Student s2(T())`. – jtbandes Feb 26 '20 at 02:42
  • @jtbandes Yes, you're right. I'll modify my answer to make that clearer. I don't know of any other searchable name for this issue, though. – N. Shead Feb 26 '20 at 02:44
  • 1
    afaik `Student s2()` is a classical example of mvp – bolov Feb 26 '20 at 03:20
  • @bolov looking around suggests that `Student s2();` is a vexing parse, but not the "most vexing parse" as defined by Meyers. I don't think it really makes a difference, though... – N. Shead Feb 26 '20 at 03:28