-1

I have tried to research it.. but I think everything is an object in c++.... like (int, float) are scalar objects..etc.

But when we create class's instance, documentation refers the "initialize an object with constructor" in c++. What does it actually means.

sparsh goyal
  • 89
  • 1
  • 7
  • very related/may dupe: https://stackoverflow.com/questions/2218254/variable-initialization-in-c. I'm looking for more – NathanOliver Aug 05 '19 at 13:57
  • Also see: https://stackoverflow.com/questions/1613341/what-do-the-following-phrases-mean-in-c-zero-default-and-value-initializat – NathanOliver Aug 05 '19 at 13:59
  • The wikipedia article on initialization is probably worth a read: https://en.m.wikipedia.org/wiki/Initialization_(programming) – T_Bacon Aug 05 '19 at 14:00
  • initialize = set the initial value; object = memory containing an instance of a class – Phil1970 Aug 05 '19 at 14:01
  • 1
    int and float are built-in types. struct A {}; A is also a type, a user-defined type. It is not an object. An object is an instance of a class. A a; a is the object, A is the type. When you initialize an object, it means you instanciate it (by means of a class constructor). An object is initialized when you give it an initial value. Example: std::vector {1,2,3}; – mfnx Aug 05 '19 at 14:01
  • @MFnx you mean int and float are not objects? – sparsh goyal Aug 05 '19 at 14:13
  • @sparshgoyal int and float are types, and in particular, built-in types. An object is an instance of a class. int and float are not really classes. Let's assume int and float are classes, then in the declaration int i; or float f; i and f are objects, int and float are types. – mfnx Aug 05 '19 at 14:17
  • see this, https://stackoverflow.com/questions/14821936/what-is-a-scalar-object-in-c – sparsh goyal Aug 05 '19 at 14:19
  • To which "documentation" are you referring exactly? – Lightness Races in Orbit Aug 05 '19 at 14:49
  • 2
    @MFnx The way the C++ Standard uses "object", variables of scalar types like `int` or `float` are also considered objects, not just class types. Their initializations do not use constructors. – aschepler Aug 05 '19 at 15:11

3 Answers3

4

I think everything is an object in c++

By the letter of the standard, an "object" is "a region of storage". You can read the nitty-gritty details under [intro.object].

But in layman's terms yes you are right.

like (int, float) are scalar objects..etc.

Absolutely. An int is an object. A float is an object.

(Of course, int and float themselves are types.)

But when we create class's instance, documentation refers the "initialize an object with constructor" in c++.

There's nothing wrong with that. You can initialise an int, and you can initialise a float, and you can initialise an object of class type. For the latter case, one way to do that is using a constructor. That doesn't change anything.

What does it actually means.

Exactly what it says: performing the steps needed to give some object an initial value.

I'll caution you also that there is a lot of bad "documentation" (notably poor tutorials etc.) for C++ out there on the web, so it's also possible that you came across badly-worded or flat-out incorrect text. Notice that, even in the comments section under your question, some people got this wrong.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
1

everything is an object in c++.... like (int, float) are scalar objects

This is wrong. int and float are built-in types. The user can define it's own types, like for example:

struct A {};

Here, A is a user-defined type. It is not an object!

An object is an instance of a type:

A a;
int i;

Here, a and i are objects of type A and int. When you initialize an object of type A, it means you instanciate class A and initialize it by calling one of the class constructors.

Another example:

std::vector<int> v {1,2,3}

Here, the object v of type std::vector<int> is initialized with the values {1,2,3}, by calling the constructor of the class std::vector<int>.

mfnx
  • 2,894
  • 1
  • 12
  • 28
  • @LightnessRacesinOrbit Any reference for that statement? According to this, I think I'm correct. https://stackoverflow.com/questions/22206044/difference-between-object-and-instance-c – mfnx Aug 05 '19 at 14:50
  • @Caleth Yes exactly. x is an object and an instance. I think that is what I'm saying. – mfnx Aug 05 '19 at 14:54
  • @LightnessRacesinOrbit I think MFnx is saying that `int` is a *type*. Instances of `int` are objects – Caleth Aug 05 '19 at 14:55
  • Okay, this wording is much more clear. Before, it read like you were saying that _a_ `int` is not an object. On the other hand, I think it's clear that the OP meant instances-of, not the types themselves. – Lightness Races in Orbit Aug 05 '19 at 14:55
  • Exactly. How int can be an object? "An int", being, a object of type int, is an obj. But "int" is not an object as far as I know. I got downvoted hardly there lol due to "an int" vs "int" lol. – mfnx Aug 05 '19 at 14:56
  • I think you're just reading the question too pedantically :P The original version of your answer pretty heavily read as if a thing had to be of class type to be deemed an object. It's better now. +1 – Lightness Races in Orbit Aug 05 '19 at 14:57
  • Still, I've changed "An object is an instance of a class" to "An object is an instance of a type", to avoid confusion. – aschepler Aug 05 '19 at 15:13
  • @LightnessRacesinOrbit Pedantically maybe :) But if you talk with someone who says object, when he refers to a class, that's really confusing (happened to me more than once). – mfnx Aug 05 '19 at 15:25
  • @aschepler Are you sure? Instantiating a type sounds weird, or is it correct? – mfnx Aug 05 '19 at 15:29
  • 1
    Well "instantiate" means something else in C++, related to templates. "Instance" isn't a formal technical term, but I think it sounds fine to say `i` is an instance of the type `int`. More technically, we would say `i` is a variable of type `int`, which names an object of type `int`. – aschepler Aug 05 '19 at 15:31
  • `foo a;` is read as "a is an instance of object foo", foo is also user data type. So int is also an object. – seccpur Aug 05 '19 at 15:32
  • @seccpur I do not agree. – mfnx Aug 05 '19 at 15:33
-2

Creating an object in C++ has 2 steps:

1) find some memory to contain the object.

This can be some space in the stack frame of the function for local objects or the data section for global objects. In those cases the compiler deals with that. Or operator new is used to dynamically create an object and allocates some memory. Which is the case if you write "new Foo()" anywhere.

2) "initialize an object with constructor"

This simply means the constructor is called. The address from step 1 is passed as this to the constructor and any arguments you specified too. If you have no constructor then the default constructor is used when possible.

Goswin von Brederlow
  • 11,875
  • 2
  • 24
  • 42