Discussion: Correct C++

Pages: 12
As the title gives away this is a discussion topic, not a question.

Wich is more correct C++:

int x(13);

or

int x = 13;
The first one is initialization, the second is assignment. There isn't much difference when dealing with an int.
AFAIK initialization is defined as a variable being given its first value, so aren't both initialization?

int x = 13; is more natural, to me anyway.
The both are equally correct in C++. The difference is that the first one is not allowed in C.
I thought they both invoked the constructor. Do they not?

@xhtmlx
I thought they both invoked the constructor. Do they not?


Fundamental types have no constructor.
Last edited on
Fundamental types have no constructor.

They actually do, or at least, you can consider them to.

Built-in types also have default constructors. ($6.2.8)

@Dash
Built-in types also have default constructors. ($6.2.8)


What is $6.2.8?
Section 6.2.8, The C++ Programming Language by Bjarne Stroustrup.
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.
Last edited on
Is there nothing said in the ISO standard?
http://www-d0.fnal.gov/~dladams/cxx_standard.pdf

I do not know how to find it, but it must be there?

@Krusing

I do not know how to find it, but it must be there?


No there is no such statement in the C++ standard.
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.
Last edited on
1
2
3
4
5
6
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
Last edited on
@Zephilinox

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.


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++.
Last edited on
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.
Last edited on
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


closed account (zb0S216C)
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.

Wazzak
Last edited on
@Framework
Because the basic integrated types are not classes, they cannot have constructors, nor can they have destructors.


Though it can be written as

1
2
3
4
5
typedef int I;

I x;

x.~I();
@vlad from moscow

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...

Edit: But that's another story... =)
Last edited on
Pages: 12