开发者

C++ accessing a function from in a class, receiving functions as a parameter

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. als开发者_高级运维o 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


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

}


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.


(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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜