0

i have two questions that are fairly small and related so i will put them both in the same question.

i have been experimenting with classes and i was attempting to access a class in another file that wasn't in a class so for example.

//class 1 .cpp
void Class1::function1()//another error 
{
    function()
}


//main.cpp

void function()
{
//stuff happens
}

is there a way to-do this? or would i need to add this function to a class to get it to work. also how would you go about creating a function that receives a function as it parimetre? for example function(function2())

i am simply trying to access a function from a class as it would make my code easier to use later if the function that i am using doesn't get added to a class. with regards to the seconds question i which to create a function that receives a time and a function as an argument. it will wait for the specified time then execute the program

deepsnore
  • 976
  • 5
  • 13
I Phantasm I
  • 1,651
  • 5
  • 22
  • 28
  • Can you please ask this question in the context of an issue you are attempting to overcome? – Vinnyq12 Jun 04 '11 at 02:26
  • For your second question, there are other questions on StackOverflow about how to pass function *pointers* in C++. Here is one example: http://stackoverflow.com/questions/402992/passing-function-pointers-in-c – johnsyweb Jun 04 '11 at 02:57

3 Answers3

2

How to access a function in another file?
Depends on the type of function, there can be to cases:

1. Accessing class member functions in another file(Translation Unit):
Obviously, you need to include the header file, which has the class declaration in your caller translation unit.

Example code:

//MyClass.h

class MyClass
{
    //Note that access specifier
    public:
        void doSomething()
        {
             //Do something meaningful here
        }

};

#include"MyClass.h"    //Include the header here
//Your another cpp file
int main()
{
    MyClass obj;
    obj.doSomething();
    return 0;
}

2. Accessing free functions in another file(Translation Unit):

You do not need to include the function in any class, just include the header file which declares the function and then use it in your translation unit.

Example Code:

//YourInclude.h

inline void doSomething() //See why inline in @Ben Voight's comments
{
    //Something that is interesting hopefully
}

//Your another file

#include"YourInclude.h"

int main()
{
    doSomething()
    return 0;
}

Another case as pointed out by @Ben in comments can be:
A declaration in the header file, followed by a definition in just one translation unit

Example Code:

//Yourinclude File
void doSomething();  //declares the function

//Your another file
include"Yourinclude"
void doSomething()   //Defines the function
{
    //Something interesting
}

int main()
{
    doSomething();
    return 0;
}

Alternately, a messy way to do this can be to just mark the function as extern in your another file and use the function.Not recommended but a possibility so here is how:

Example Code:

extern void doSomething();

int main()
{
    doSomething();
    return 0;
}

How would you go about creating a function that receives a function as it parameter?
By using function pointers In a nutshell Function pointers are nothing but pointers but ones which hold address of functions.

Example Code:

int someFunction(int i)
{
    //some functionality
}


int (*myfunc)(int) = &someFunction;


void doSomething(myfunc *ptr)
{
    (*ptr)(10); //Calls the pointed function

}
Alok Save
  • 202,538
  • 53
  • 430
  • 533
1

You need a prototype for the function you want to call. A class body contains prototypes for all its member functions, but standalone functions can also have prototypes. Typically you organize these in a header file, included from both the file which contains the function implementation (so the compiler can check the signature) and in any files which wish to call the function.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • THANK YOU... i keep forgetting about function prototypes :D it works now...do u have any ideas how i would acomplish the second part of the question? – I Phantasm I Jun 04 '11 at 02:51
  • @Phantasm: For the second part, you need to tell us what platform. The standard C/C++ libraries have some system clock functions but no wait functions, you'll need to use something OS-specific or an external library such as `boost::asio` which provides timers. – Ben Voigt Jun 04 '11 at 04:29
  • no you miss understood the question i already have a fully functional timer i wish to no how i can have a function as argument for another question – I Phantasm I Jun 07 '11 at 07:58
1
(1) How can the `class` function be accessible ?

You need to declare the class body in a header file and #include that wherever needed. For example,

//class.h
class Class1 {
  public: void function1 (); // define this function in class.cpp
};

Now #include this into main.cpp

#include"class.h"

You can use function1 inside main.cpp.

(2) How to pass a function of class as parameter to another function ?

You can use pointer to class member functions.

iammilind
  • 68,093
  • 33
  • 169
  • 336
  • Thats incorrect! `function1()` has a private access specifier in `Class1` here which makes it pretty much useless to be called in another translation unit! – Alok Save Jun 04 '11 at 04:44
  • @Als: access specifiers have nothing to do with translation units. – Ben Voigt Jun 04 '11 at 04:53
  • @Ben Voight: Probably I was not clear enough the point is If `function1()` is private, How can it be called from outside the class? – Alok Save Jun 04 '11 at 05:01
  • @Als: You didn't say "outside the class", you said "another translation unit". Classes and translation units are orthogonal concepts. – Ben Voigt Jun 04 '11 at 05:02
  • @Ben Voigt: My wordings were a bit mumbled on that, still dizzy i guess! It wont work even in the same translation unit, So Yes, it has nothing to do with translation unit but nevertheless it is incorrect! – Alok Save Jun 04 '11 at 05:04
  • @Als, I had copy pasted the code from the OP so missed the `public`. Have edited it. – iammilind Jun 05 '11 at 02:23