sharing same functions

This is a discussion on sharing same functions within the C++ Programming forums, part of the General Programming Boards category; i got 2 programmes that use the same function how can i make them use a another file to get ...

  1. #1
    Registered User devil@work's Avatar
    Join Date
    Mar 2003
    Posts
    33

    sharing same functions

    i got 2 programmes that use the same function how can i make them use a another file to get the function like in php you know with the include statement will that style work under c++

  2. #2
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    You can create your own header file...

    #include YouirFile.h
    Your header file would then have:
    #include YourFile.cpp


    or just #include YourFile.cpp without the .h file (bad practice).

    Make sure your file is in an "accessable" directory/folder, or give the full path & filename.

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,652
    Do you want dynamic or static sharing of code?

    "Dynamic sharing" of code would be like DLL's on Windows, or shared libraries on Linux.
    "Static sharing" of code would be linking in a library.

    gg

  4. #4
    Registered User devil@work's Avatar
    Join Date
    Mar 2003
    Posts
    33
    lets say first line #include myfile.cpp
    and inside it there is a function lets say

    Code:
    int myfunc()
    {
    return a*b;
    }
    do i have to write a prototype some where?

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,652
    Don't include .cpp files!

    Read this instead.

    gg

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    6
    I'm not completely sure that I've understood the question but...

    You could write the function in a textfile called "TextFile" like so...

    Code:
    void Add(int a, int b)
    {
    	int sum(0);
    	sum = a + b;
    	cout<<"The sum is "<<sum<<endl;
    }
    And then in the source files where functions are listed type...

    Code:
    #include "TextFile.txt";
    Of course if you do this you need a protoype in both source files where protoypes usually go for functions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 02:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21