0

I have in my book the following statement:

If we have given any constructor for a class whether it is

1. our own explcit default constructor ( i.e parameterless or with parameters having default values )
or
2. our own constructor with parameters
Then compiler will not create implicit default constructor.

BUT I have doublt about point 2 and I suspect my book id either incorrect or outdated because my following code does have a constructor with parameters but the compiler is generating internal constructor too.

#include <iostream>

class imminent{
    public:
    imminent(int x, int y){
    std::cout << "I am explicit constructor" << std::endl;
    }
};

int main(){
    imminent gilfray(); /* compiler creates internal default constructor 
                           that why this line is compiled without errors*/

    imminent jimmy(1, 2);
}

Moreover, how can I put my c++ code in real way here as this time I was forced to use JS in the code snippet, isn't there any c++ code sharing option ?

abelenky
  • 63,815
  • 23
  • 109
  • 159
Danish ALI
  • 65
  • 1
  • 9
  • 7
    Have a look at most vexing parse: http://en.wikipedia.org/wiki/Most_vexing_parse. `imminent gilfray();` is a function returning `imminent` –  Nov 24 '14 at 18:19

2 Answers2

0
imminent gilfray();

I believe you intend this line to declare a variable gilfray, with type imminent.

However, it does not.
Instead, it declares a function gilfray, with no parameters, and a return type of imminent.

No default constructor is used in this code.

You can verify that gilfray is not a variable by trying to actually use it.

gilfray.DoSomething(); // Likely a linker error: "Undefined function gilfray."

If you wanted to try to use a default-constructor, it'd look like:

imminent gilfray; /* Create variable gilfray, with default ctor */
abelenky
  • 63,815
  • 23
  • 109
  • 159
0

In C++03, in order to value-initialize an object, you need to use an equal sign:

imminent gilfray = imminent();

In C++11, imminent gilfray() still declares a function taking no parameters returning an imminent. However, you can use braces instead.

imminent gilfray{}; // compiler error