How to invoke a function from another. cpp source file

Source: Internet
Author: User

Senario:

There are two source files a. cpp and B. cpp both in a project. A. cpp between des a function called viod show (). Now I wanna invoke that function in B. cpp.

As I know, there are two ways we can use to invoke a function from another. cpp source file:

1. In B. cpp, use "extern void show ()" statement to declare an extern function.

2. Add a new header file (we call a. h for example), in that header file, we declare that function we want other source files to see and use by adding these codes:

# Ifndef a_h <br/> # definea_h <br/> void show (); <br/> # endif 

Then, we need to add "include". H "" statement at the beginning of B. CPP, after "include <iostream>" statement. after that, we can invoke show () in B. CPP now.

PS: there is no restriction about the name of the header file, I use "B. H "as its name here is just to say that the function has ded in it is defined in. CPP file.

PPS: Does anyone know the differences between these two methods?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.