0

I'm just beginning C++, having first done some Java and a lot of Python. In both of those languages there is exactly one way of creating a new variable of type MyClass, in Java it's MyClass obj = new MyClass() and in Python it's obj = MyClass().

In C++ I've seen all kinds of different syntaxes in example code, things like:

MyClass* obj = new Myclass();
MyClass obj();
MyClass obj = MyClass();
MyClass obj;

Where can I get an exhaustive list of all the different ways of instantiating a class in C++, and the differences between them? In case I'm using the terminology wrong, by "instantiating", I mean obtaining a variable name on which methods can be called with either dot or arrow notation.

Jack M
  • 4,769
  • 6
  • 43
  • 67
  • 6
    2nd line is not an object instantiation, see [this](https://stackoverflow.com/questions/47596523/why-doesnt-this-enum-convert-to-int/47596822). –  Apr 02 '18 at 12:38
  • #3 and #4 are essentially the same thing, but #3 requires the esistance of a copy constructor (even if it is not invoked). #1 is a different beast, as it allocates a new object on the heap and provides a pointer to it that you have to explicitly deallocate. #2 is actually interpreted as a declaration of a function taking nothing and returning a `MyClass` object. – Matteo Italia Apr 02 '18 at 12:39
  • The reason there are different ways is because C++ is not Java. Or Python. – Sam Varshavchik Apr 02 '18 at 12:42
  • 1
    @SamVarshavchik Okay? – Jack M Apr 02 '18 at 12:44
  • In C++ it sometimes helps to talk about [object lifetime](https://stackoverflow.com/questions/34400042/in-c-are-constructors-called-before-or-after-object-creation) and [initialization](http://en.cppreference.com/w/cpp/language/initialization) rather than instantiation. – wally Apr 02 '18 at 12:50
  • 1
    How come this already has two close votes? This is a very specific, concrete question about the syntax of a language. – Jack M Apr 02 '18 at 12:53
  • @JackM: This question is anything but specific. And it's not a question about *syntax* at all. You need to get a book on C++ and not try to translate Java or Python to C++. – Christian Hackl Apr 02 '18 at 12:56
  • @ChristianHackl What? There is some finite number of ways in which to instantiate objects in C++. That finite list is defined, explicitly or implicitly, by the C++ language spec. I'm asking what that list is. The comments about Java and Python were merely to give some background. – Jack M Apr 02 '18 at 13:01
  • Looking at the C++ standard it seems the word instantiation is used in connection with templates. So there is a terminology gap between the languages that a good answer would hopefully bridge. – wally Apr 02 '18 at 13:05
  • @JackM: Then you are asking for documentation, which is off topic. Such a list also won't help you much if you don't understand the basics of C++. Such as the fact that you often don't bind an object to a variable. – Christian Hackl Apr 02 '18 at 13:05
  • 1
    @JackM - I voted to close, since this question is not specific at all. C++ statements for instantiating objects have VERY different meaning from similar-looking statements in Java or Python, and you're incorrectly assuming that looking alike means they have similar meaning in all the languages. You'd be better trying to learn C++ as C++, rather than by analogy with Java or Python. (And, if someone was trying to learn Java, I'd recommend they learn Java directly rather than by analogy with C++ - the languages are just too different, even in cases where they look similar). – Peter Apr 02 '18 at 13:15
  • I think the answer to this question is to basically look at the [declaration syntax](http://en.cppreference.com/w/cpp/language/declarations) if you want the full syntax. Along with object lifetime and initialization the [storage class specifiers](http://en.cppreference.com/w/cpp/language/storage_duration) should be considered (part of the declaration syntax) to determine when the object will begin and end its lifetime. – wally Apr 02 '18 at 13:17
  • 1
    I think this is an interesting question and worthy of an answer, but I must point out that the phrase "Where can I get an exhaustive list" is the kiss of death for most questions. – wally Apr 02 '18 at 13:21
  • If you keep trying to learn C++ by drawing analogies to Java you will have a very frustrating time trying to wrap your brain around C++. Despite very similar syntax, C++ classes and objects are fundamentally different from what Java calls a class and an object. When you create an object in Java, you're really creating a pointer. Only the first example in your list you get a pointer. And, you missed three more variations: `MyClass* obj = new Myclass{};`, `MyClass obj{};` and `MyClass obj = MyClass{};` That's why this is too broad of a question. – Sam Varshavchik Apr 02 '18 at 13:34

1 Answers1

2

MyClass obj(); is actually a declaration of a function obj which returns MyClass object.

MyClass* obj = new Myclass(); creates MyClass object on the heap, you should free this memory later.

MyClass obj; creates MyClass object via default constructor.

MyClass obj = MyClass(); creates MyClass object via copy constrctor.

You could also use MyClass obj{}; which creates zero initilized MyClass object or call initializer-list constructor if there is one.

  • If `MyClass obj()` is a funciton declaration, could you explain the first line of the first code snippet on this page? Underneath where it says "Reading files directly". http://doc.qt.io/qt-5/qfile.html – Jack M Apr 02 '18 at 12:48
  • 1
    @JackM Those aren't the same thing. The first code snippet has stuff inside the `()` – NathanOliver Apr 02 '18 at 12:49
  • @NathanOliver So does that mean this answer is missing out one way of instantiating a class, namely `MyClass obj("an argument");` ? – Jack M Apr 02 '18 at 12:54
  • @JackM https://en.wikipedia.org/wiki/Most_vexing_parse – Edward Dankovsky Apr 02 '18 at 12:55
  • @JackM Maybe. It is valid if `MyClass` has a constructor that takes a string literal. – NathanOliver Apr 02 '18 at 12:55
  • @EdwardDankovsky But wait, if the trouble is that the line could be interpreted as either a func declaration *or* an instantiation of an object, then in particular that must mean that the line *is* a valid instantiation. So your comment that it's "actually" a function declaration doesn't seem to be correct? – Jack M Apr 02 '18 at 13:03
  • @JackM • "the line could be interpreted as either a func declaration or an instantiation of an object" is incorrect. The line can ONLY be interpreted as a func declaration. – Eljay Apr 02 '18 at 13:06
  • @JackM no. C++ standard says that that line is a function declaration – Edward Dankovsky Apr 02 '18 at 13:08
  • `MyClass obj;` does not necessarily create a local object. It could also have static storage duration. – Christian Hackl Apr 02 '18 at 13:11
  • @ChristianHackl thanks for remark. Fixed my anwser – Edward Dankovsky Apr 02 '18 at 13:17