Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is an int() Called?

It's been rehashed over and over that primitive types don't have constructors. For example this _bar is not initialized to 0 when I call Foo():

class Foo{
    int _bar;
};

So obviously int() is not a constructor. But what is it's name?

In this example I would say i is: (constructed? initialized? fooed?)

for(int i{}; i < 13; ++i)

Loki Astari mentions here that the technique has some sort of name.

EDIT in response to Mike Seymour:

#include <iostream>

using namespace std;

class Foo{
    int _bar;
public:
    void printBar(){ cout << _bar << endl; }
};

int main()
{
    Foo foo;

    foo.printBar();

    Foo().printBar();

    return 0;
}

Running this code on Visual Studio 2013 yields:

3382592
3382592

Interestingly on gcc 4.8.1 yields:

134514651
0

like image 444
Jonathan Mee Avatar asked Dec 12 '14 12:12

Jonathan Mee


People also ask

Is int [] and int * Same?

int means a variable whose datatype is integer. sizeof(int) returns the number of bytes used to store an integer. int* means a pointer to a variable whose datatype is integer.

What does int * mean in C?

There are no pointer types in C! So, "int*" means nothing. The asterisk is always bound to the element written right of it, it belongs to the element right to it.

What is the problem in declaring int main ()?

int main() doesn't include any list. So gcc is complaining about an old-style (K&R) function definition...that would allow main() to take any number of arguments. That's not one of the standard-sanctioned forms.

What does int mean in programming?

int , short for integer in many programming languages.


1 Answers

It's been rehashed over and over that primitive types don't have constructors.

That's right.

For example this bar is not initialized to 0 when I call Foo()

Yes it is. Foo() specifies value-initialisation which, for class like this with no user-provided constructor, means it's zero-initialised before initialising its members. So _bar ends up with the value zero. (Although, as noted in the comments, one popular compiler doesn't correctly value-initialise such classes.)

It would not be initialised if you were to use default-initialisation instead. You can't do that with a temporary; but a declared variable Foo f; or an object by new F will be default-initialised. Default-initialisation of primitive types does nothing, leaving them with an indeterminate value.

It would also not be initialised if the class had a user-provided default constructor, and that constructor didn't specifically initialise _bar. Again, it would be default-initialised, with no effect.

So obviously int() is not a constructor. But what is it's name?

As an expression, it's a value-initialised temporary of type int.

Syntactically, it's a special case of an "explicit type conversion (functional notation)"; but it would be rather confusing to use that term for anything other than a type conversion.

In this example I would say i is: (constructed? initialized? fooed?)

Initialised. List-initialised (with an empty list), value-initialised, or zero-initialised, if you want to be more specific.

like image 112
Mike Seymour Avatar answered Sep 23 '22 20:09

Mike Seymour