dev_nut
dev_nut

Reputation: 2542

if I define a function in the implementation (.cpp) file without defining it in the header file what happens?

I'm a C++ noob. I have a foo.cpp file which defines a function,

void sort() {
   // details of sort algorithm
}

I have not defined this in the corresponding header file, but I get no compilation error. I noticed this is taken as a static method. Is that the case? What exactly have I done here?

EDIT:

I've noticed that inside this method I can't use this, (ex: this->bar), hence my assumption that this is a static function. This actually happened when I wanted a 'private' method, but forgot to declare it in the header file. Is there an actual use of such a function?

Upvotes: 11

Views: 13952

Answers (8)

stdout
stdout

Reputation: 2651

And also it might be very bad if the same function defined inside some other cpp file with the same signature.

Upvotes: 1

ma11hew28
ma11hew28

Reputation: 126487

Even though it's only in the implementation file, it's not static unless you specify static.

Static functions, in addition to restricting access, may also allow for compiler optimizations for better performance.

See static function in C, notably @Stephen_Canon's answer.

Upvotes: 1

nurettin
nurettin

Reputation: 11746

It is not invisible outside of that compilation unit, it is not bound to a single compilation unit, it is just a definition that needs a declaration in order to be called. Like any other definition.

wtf.cpp:

#include <iostream>
void f(){ std::cout<< "f();"; }

omg.cpp:

void f(); // I can declare wherever I use it.
int main(){ f(); }

$ g++ wtf.cpp omg.cpp && ./a.out

Output: f();

Upvotes: 8

lucasg
lucasg

Reputation: 11012

You've just have defined a free function .

You can use in your implementation file given that the declaration is above the calls (the parser "reads" the source file from top to bottom) :

void fun(){
   mysort(); // Compile Error

void mysort(){}

void anotherFun(){
   mysort(); // Works here
   }

To get rid of this boundary effect, you can add a prototype of the function at the beginning of the file. I often use this "free function" for small conversion macros.

Example :

char* BigEndianToLitteEndian( char word[], int size); // prototype

void routine()
{
  ....
  le = BigEndianToLittleEndian( be, SIZE );
  ....

}

char* BigEndianToLitteEndian( char word[], int size){ // implementation
   ....
}

Upvotes: 1

Serve Laurijssen
Serve Laurijssen

Reputation: 9763

the function is like the others said available in that translation unit. But you can still access it from anywhere as it will just have external linkage. so just do this:

void foo()
{
    extern void sort();

    sort();
}

Upvotes: 3

Mario
Mario

Reputation: 36537

Nothing happens. The function will be there and essentially available, but without a prototype/forward declaration other translation units/parts of the code won't know it's actually there (and based on optimization level it might be excluded from the resulting executable code).

Upvotes: 3

Axel
Axel

Reputation: 14169

No, it is not taken as a static function. It is just not visible to the other transllation unnits, and so you cannot use it.

The difference in regard to a static method is, that the linker sees the function. So if you define a function with the same name in another translation unit, you will probably either get a linker error (best case) or the wrong method called at times.

PS: I talk about a functions rather than methods here, because in C++ a method typically is part of a class, and you cannot define a method for a class if that method isn't declared in that class' declaration.

Upvotes: 4

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799210

You've created a function that is only visible to that compilation unit (and others that include the source itself).

Upvotes: 2

Related Questions