Reputation:
When defining a function in the class body, are you always declaring it or how would you say? Here is an example:
class ss
{
ss() {}
};
Are we then declaring the constructor, defining the constructor, or declaring AND defining the constructor. What could you say about this?
Upvotes: 2
Views: 100
Reputation: 12751
It is both definition and declaration.
The below information is lifted from C++ standards document: (n4659)
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/n4659.pdf
for example consider the below function
int max(int a, int b, int c)
{
int m = (a > b) ? a : b;
return (m > c) ? m : c;
}
Upvotes: 1
Reputation: 36483
You're both declaring and defining the constructor.
A declaration only would look like:
class ss
{
ss();
}
You can seperate the definition and declaration outside the class to show the obvious difference between the two:
class ss
{
ss(); //declaration
}
ss::ss() {} // definition
A definition is by definition a declaration too.
A declaration is only the signature of a function, a definition includes the actual function body.
Upvotes: 5
Reputation: 171127
This is not specific to functions, but is a general rule in C++: A definition is one possible type of declaration. (This follows from the C++ grammar). Which means every definition is also a declaration.
So in your case, the constructor is both declared and defined by the declaration ss() {}
.
Upvotes: 3