0

What is the difference between definition and instantiation?

Sub-question: Are "variable definition" and "variable instantiation" the same?

int x;

The above code can be reffered to as both a variable definition as well as a variable instantiation, right? If so, my question is if these two terms are synonyms? (or is there a different relation between them?)

Riko
  • 257
  • 4
  • 14
  • 4
    I'd say these are completely orthogonal things. – πάντα ῥεῖ Oct 31 '16 at 15:35
  • 2
    seems like you have some confusion about the terminology. You should show examples that are referred to as instantiation or definition or both in whatever documentation you read – 463035818_is_not_a_number Oct 31 '16 at 15:39
  • 2
    Might I suggest a [read](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)? – UKMonkey Oct 31 '16 at 15:40
  • Ambiguity detected :( When I first read your title, I first thought you meant "definition and [template] instantiation?". Then when I read the comments it appears they understand it to mean "definition and class object instantiation" and then when I read your text I'm still not sure whether or not it refers to C++14 variable template instantiation. Please clarify – Johannes Schaub - litb Oct 31 '16 at 15:45
  • maybe you meant something like `Foo f = Foo();` where you declare and define the variable `f` and there is also an instantiation taking place. Nevertheless, the three terms refer to three different things: declaration, definition, instantiation. – 463035818_is_not_a_number Oct 31 '16 at 15:45
  • 1
    As for the question: When people say "instantiation", they usually mean to go from a skeleton to something derived from that skeleton. Meaning, from a template to a class/function "instantiated" from it. Or from a class to an object "instantiated" from it. Or more generally, "instantiating" a type to a value of that type. The template vs class/function meaning is standardized for C++. The class vs variable meaning is not standardized for C++. So you may want to change the tag from [c++] to [language-agnostic]. Otherwise it may quickly be closed as opinion-based. – Johannes Schaub - litb Oct 31 '16 at 15:48
  • @tobi303 Clarification: int x; can be reffered to as a variable definition as well as variable instantiation, right? If so, my question is if these two terms are synonyms, or there are some cases, in which these two terms mean two different things. – Riko Oct 31 '16 at 15:49
  • @Riko You should mention this context in your question. – πάντα ῥεῖ Oct 31 '16 at 15:50
  • When someone would ask me "what is a variable instantiation", I would probably tell them "my opinion is that it's a lvalue-to-rvalue conversion on an lvalue that refers to a variable", or something like that (on full moon with less standardese than on other days). – Johannes Schaub - litb Oct 31 '16 at 15:51
  • I just found on [learncpp.com](http://www.learncpp.com/cpp-tutorial/17-forward-declarations/) the following: "A definition actually implements or instantiates (causes memory to be allocated for) the identifier." Is then the following interpretation correct?: "A definition refers to either an implementation (of a function for instance) or an instantiation, which is actually allocating some memory for an identifier." – Riko Oct 31 '16 at 16:14
  • see http://stackoverflow.com/questions/1410563/what-is-the-difference-between-a-definition-and-a-declaration for a good description of the definition vs declaration – DaveB Nov 01 '16 at 09:24
  • @DaveB definition vs declaration as well as definition vs initialization vs assignment are clear to me, thanks – Riko Nov 01 '16 at 10:11

3 Answers3

2

After quite some edits and also a correction made by Johannes Schaub:

  1. A definition of a variable of a certain type creates a variable of that type. As far as Stroustrup is concerned, this also holds for the definition of objects of a certain class, since a class is nothing more than a (non-native) type. (This makes much sense, although it isn't general OO terminology.)
  2. General object oriented termininology: Instantiation of a class creates an object of that class. Specialized C++ terminology: Instantiation of a template creates a "perfectly ordinary" (Stroustrup) class.
  3. A class is a type, defined in code, rather than as part of the language.
  4. An implementation is a concrete class that realizes the functionality specified in an abstract class from which it derives.
  5. A declaration is a specification of a variable or function, without allocating memory for it or generating code for it.

So @Riko, the definition on learncpp.com specifying that a definition implements an indentifier is not very accurate. Interfaces can be implemented, not types or classes. But one part of the definition is valuable: Definition in general goes hand in hand with memory allocation. You can declare a function or variable as often as you want (e.g. a declaration in a header file), but you an define it only once. If you declare a function, you give the signature (the name, return type and params, but not the body).

If you declare a variable, you used to put the word extern in front of it in a header file, but that isn't done often anymore, since object orientation and classes took over. Defining a variable in a header file, on the other hand, may lead to multiple instances of the variable, since the same header is read during compilation of distinct source files. Since C++ uses independent compilation and just textually includes the header, the variable is defined in multiple files, so there are several variables under the same name. Linkers don't like such ambiguity and will complain.

While the term instantiation in general means "creating an object of a class", Stroustrup (maker of C++) uses it in a special sense: A class is an instance of a template with all its parameters resolved. Nevertheless in many texts on C++ the word instantiation is used in the general Object Oriented sense, which is confusing.

@Jonannes Schaub. Although I am not too happy with C++ terminology deviating from general OO terminology, I think it's right to follow Stroustrup here, since after all, he created the language.

Jacques de Hooge
  • 6,204
  • 2
  • 19
  • 37
  • https://en.wikipedia.org/wiki/Instance_(computer_science) Initalization is giving a variable or object a start value. And indeed the word Instantiation is also used for templates and generic types. But long before templates were introduced in C++, instantiation was already used for creating objects of a certain class. – Jacques de Hooge Oct 31 '16 at 16:00
  • Cool. Thanks for the info – NathanOliver Oct 31 '16 at 16:01
  • 2
    @JacquesdeHooge that does not apply to C++ but to object oriented programming in general (as the article says). As an analogous example, "object" in C++ has a different meaning than in object oriented programming in general. – Johannes Schaub - litb Oct 31 '16 at 16:02
  • @JacquesdeHooge Thanks for your answer. However, my question is more related to the C++ terminology in general (not necessarily in OOP). Could you please have a look at my last comment under the question and let me know if that interpretation is correct or not? – Riko Oct 31 '16 at 16:31
  • @Johannes Schaub. Indeed Stroustrup chooses to use the word Instantiation in a special sense: Template instantiation. I'll add that to my answer. – Jacques de Hooge Oct 31 '16 at 17:09
1

There are:

1) variable definitions,

2) variable/object instantiations, and

3) template instantiations.

1 & 3 are specific C++ terminology. 2 is more general terminology that might be used with C++. It is not an "officially" defined term for C++.

I understand that your question is about 1 and 2, but not 3. 3 is different than 2, though related in meaning. I won't address 3 further as I don't believe it is part of your question.

Instantiation is the creation of an object instance. It is more usual to use the term in reference to a class object than something like an int or double.

A C++ variable definition does cause an object of the type being defined to be instantiated. It is, however, possible in C++ to instantiate an object other than via a variable definition.

Example 1:

std::string name;

The variable name, a std::string, is defined and (at run-time) instantiated.

Example 2:

std::string *namePointer;

The variable namePointer, a pointer, is defined and might be said (at run-time) to be instantiated (though not initialized). There is no std::string variable and no std::string is instantiated.

//simple example, not what one should usually write in real code
namePointer = new std::string("Some Text");

No additional variable is defined. A std::string object is instantiated (at run-time) and the separate and pre-existing namePointer variable also has its value set.

Avi Berger
  • 1,536
  • 11
  • 11
0

Definition and Declaration are compile time concerns. Declaration and definition of identifiers happens while your program is being compiled.

Declaration: A declaration is telling the compiler about the type of an identifier that is defined somewhere else but may be referenced here.

Definition: There can be only one definition of an identifier. This is where the thing is actually defined. All the declarations refer to this definition.

Mostly this is only a distinction we make with classes because built in types are already defined (the compiler already knows what an int is). The only exception I can think of is when we declare a variable to be extern.

Instantiation, this happens at run time.

  • An object is an instance of a class.
  • Instantiation is the act of creating a new object.

Instantiation of an object happens while your program is being run. Instantiation is when a new instance of the class is created (an object).

In C++ when an class is instantiated memory is allocated for the object and the classes constructor is run. In C++ we can instantiate objects in two ways, on the stack as a variable declaration, or on the heap with the new keyword. So for the class A both of the following create an instance of the class (instantiate it)

struct A {
    int a;
};

A inst1;
A* inst2 = new A();
  • inst1 is a local variable that refers to an instance of the class A that was just created on the stack.
  • inst2 is a local variable that holds a pointer to an instance of class A that was just created on the heap.

There may be some confusion because the first is not possible in Java or C#, only the second. In C++ both instantiate (create a new runtime instance of) the class A. The only difference beteween the two is scope and where the memory was allocated.

DaveB
  • 332
  • 1
  • 9