0

Is it possible to call function defined in a.cpp to use in b.cpp without declaring the function in a.cpp in any header file.

3
  • 1
    Yes, you can just put a prototype for the function in b.cpp. But why? Commented Feb 26, 2014 at 7:24
  • 1
    Sorry, I meant you can declare the function in b.cpp, before use. The obvious question would be "what problem are you trying to solve?" Commented Feb 26, 2014 at 7:30
  • thanks all with prototype defining I can do this, @JeremyWest It was suggested me to reuse rather than crating seprate static one, Just curiosity is it possible without prototype. Commented Feb 26, 2014 at 7:32

1 Answer 1

2

Yes, although it's not recommendable.

What the inclusion of headers actually does is effectively putting the content of the header into the source code at the exact location where the preprocessor finds the #include directive. So, instead of using a include directive, the code can manually be written at that location, the program will be the same:

With headers:

//a.h

void foo();

//a.cpp
#include "a.h"

void foo() {
  //do something
}

//b.cpp
#include "a.h"
void bar() {
  foo();
}

After preprocessing it's the same as:

//a.cpp
void foo();

void foo() {
  //do something
}

//b.cpp
void foo();

void bar() {
  foo();
}

So you can leave out the header and declare the function manually everywhere you need to call it. The header however ensures that the declarations are the same all over the project. E.g. if you change foo to take a parameter:

//a.h
void foo(int);

Now in b.cpp the compiler will tell you that the call foo() does not match the declaration. If you leave out the headers and manually declare it instead and if you forget to change the declaration in b.cpp, the compiler will assume there are two versions of foo, because you told him so:

//a.cpp
void foo(int); //version 1
void foo(int i) {
  //do something
}

//b.cpp
void foo(); //oops. forgot to change that. compiler assumes a second version

void bar() {
  foo(); //ok, try to call version 2...
}

This will compile, however the linker will tell you something about an undefined reference for void foo(), called in b.obj.

Sign up to request clarification or add additional context in comments.

4 Comments

Still for certain reason I have to use 2nd approach, Do I strictly need to define the prototype in b.cpp or without prototype in b.cpp is it possible?
@SagarKotecha The "prototype" is not defined. It's a declaration. The definition is what you find in a.hpp, i.e. the function with its body. I am not meaning to be picky, but if you use the proper terminology people will understand better what you are talking about. And yes, you have to declare a function before you can use it. However, if you tell us why you think you have to do it, i.e. tell us what you are trying to achieve (instead of how), we probably can show you how to make the better use of headers.
Sorry for my English, I was meaning declaration for "define the prototype" (which was wrong).
No problem at all :-) I made a mistake in my comment above: the definition is found in a.cpp, of course.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.