It is incorrect statement. As I said fundamentals types have no constructor. You should read the C++ Standard instead of various books of some authors including Stroustrup. It is the C++ Standard that determines the C++ language. I did not find any reference in the C++ Standard that fundamental types have a constructor.
I probably read it when I was reading about strings and thought it applied to all of the datatypes. Don't ask me why, that's just what I thought. Sorry for the mistake.
void f(double d)
{
int j = int(); // default int value
complex z = complex(); // default complex value
// ...
}
Section 6.2.8 Constructors
The value of an explicit use of the constructor for a built-in type is 0 converted to that type. Thus, int() is another way of writing 0. For a user-defined type T, T() is defined by the default constructor, if any.
Although it doesn't seem like int has an actual constructor.
Unfortunately, for a built-in type T, T(e) is equivalent to (T)e
The value of an explicit use of the constructor for a built-in type is 0 converted to that type. Thus, int() is another way of writing 0. For a user-defined type T, T() is defined by the default constructor, if any.
Please, do not cite wrong statements. Firstly the C++ Standard has no such section as 6.2.8. Secondly you shall refer to the C++ Standard not some books about C++.
I never said I was referring to the standard, but to be fair I didn't say I was referring to the book mentioned above either, the standard is a mess of technicalities that I can't decypher, and I'll quote whatever the hell I want, if you don't want to accept my quote that's fine with me, I'm just throwing it into the discussion.
I also said it doesn't seem like int has an actual constructor.
The value of an explicit use of the constructor for a built-in type is 0 converted to that type. Thus, int() is another way of writing 0. For a user-defined type T, T() is defined by the default constructor, if any.
Yes.
> Although it doesn't seem like int has an actual constructor
No, it does not. Even so, a value initialized int is deemed to be constructed.
An object whose initializer is an empty set of parentheses, i.e., (), shall be value-initialized. - IS
An object that is value-initialized is deemed to be constructed and thus subject to provisions of this International Standard applying to 'constructed' objects, objects 'for which the constructor has completed,' etc., even if no constructor is invoked for the object’s initialization. - IS
Both are valid forms initialisation. The first form is called functional notation. The second is called plain initialisation. In my opinion, functional notation is the C++ way of initialisation. Since functional notation was not part of the C syntax, it leads me to believe it was added into C++ to mimic the syntax of a constructor call.
xhtmlx wrote:
"I thought they both invoked the constructor. Do they not?"
Integrated types do not have a constructor. Constructors are special member functions, which only classes can have. Because the basic integrated types are not classes, they cannot have constructors, nor can they have destructors.
Is that not function declarations? Even tho it can be used with typedef, I would not use typedef for other than function prototype declaration.
I read somewhere that the use of typedefs can make code easier to maintain, you only need to change one statement in your source code instead of everywhere it appears...