user7982333
user7982333

Reputation:

When defining a function, are you also declaring it?

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

Answers (3)

Pavan Chandaka
Pavan Chandaka

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; 
 } 
  1. int (return type of the function) is the declaration specifier
  2. max(int a, int b, int c) is the declarator;
  3. { /* ... */ } is the function-body.

Upvotes: 1

Hatted Rooster
Hatted Rooster

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

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

Related Questions