I'm trying to understand the distinction between declarations and definitions in C++. I've read various answers here and consulted the C++ Standard, but I'm still confused about a few points:
Some explanations suggest that definitions allocate memory to the variable, implying that if memory isn't allocated, it's considered a declaration. Is this always the case?
Which phase of the compilation process—Compiler or Linker—is primarily associated with declarations and definitions?
Here's a summary of my understanding so far:
Declaration: This informs the compiler about the existence of a variable or function, including its name and type, but doesn't specify memory allocation.
Definition: This not only declares the existence of a variable or function but also provides the implementation and how much memory a variable needs but it doesn't allocate memory unless it is used.
Given the following code snippet similar to what we've discussed in lectures:
class Point { // declaration
public:
int x; // declaration
int y; // declaration
void setPoint(int x, int y); // declaration
};
int main() {
Point p1; // definition
};
My understanding is that everything in this code snippet should be considered a definition except for void setPoint(int x, int y);. However, I'm unsure about int x; because it seems that memory isn't allocated until an object is created. For instance, my IDE (Clion) considers int x; a declaration, which adds to my confusion.
Could someone please clarify how definitions are precisely associated with memory allocation? Additionally, I'd appreciate insights into the specific cases where memory is or isn't allocated for variables declared in classes or function bodies that haven't been invoked.
Declarations and definitions are about names, not necessarily about objects.
A somewhat simplified (hence, wrong) description of those terms is that a declaration tells the compiler that some name will be used in a particular way; a definition gives the compiler all the details of what that name means.
This is a declaration of the class
Point:After that declaration the name
Pointcan be used in ways that don't depend on its definition.Point*andPoint&(pointer toPointand reference toPoint, respectively) are probably the most common examples.This is the definition of the class
Point:Its two data members,
xandy, are part of the definition. Those are neither declarations nor definitions.void setPoint(int x, int y);is a declaration of the member functionPoint::setPoint. Presumably, there's a definition somewhere that hasn't been shown.This is a declaration of the object
p1:After that declaration the compiler knows that
p1names an object of typePoint, and the code can do any operations onp1that are valid for an object of typePoint.This is the definition of the object
p1:This tells the compiler "here it is".
Note that nothing in these descriptions mentions "memory". The definition of a class doesn't use memory; the definition of an object might mean "grab some memory for this thing", or it might mean "when the program gets here grab some memory for this thing".