How to pass a parameter to a function that is called on an event?

C++ newbie: how can I store long member functions of a class in a separate file, but still access the class?

  • Hi everyone: Sorry, my question probably is not very elegantly worded! I have written a class in a .cpp file and a .h file. I want to make my code more readable for users. It is very long. So I have created .cpp and .h files for storage called "auxiliary.h/cpp" I have moved a lot of my code into the auxiliary files and I want to access it through functions in my class. For example, one function in my class is now called readDataFromExternalFile(). The code that reads data from an external file is now in auxiliary.h/cpp. The problem is (sorry, this is probably a silly question!), when I use the function, stuff (data members AND functions) I need in the class is now out of scope. My question is, is there a simple way of making the class see the auxiliary files? I know all about passing items by reference, but things get complicated (particularly with passing template functions) and the function parameter lists get very, very long when I pass all items that I need by reference. One thing I tried was passing an INSTANCE of the class as an argument/parameter in each function that gets stuff from the auxiliary files. But this results in "instanceName not declared in this scope" and I guess it does seem like a circular way of doing things. Any suggestions would be very, very much appreciated! Thanks.

  • Answer:

    i dont see any problem in that. Just make sure you Include you class file by #include <FileName.h> and you make an Object of class in main Program. You can access all member functions using that object in main program.

Antst at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

If you have one gigantic class that does everything, you're not writing object oriented code correctly. Break your object up into multiple classes that each handle different things, or you could use inheritance. Or you could write some global functions that your class could use. There are a million possibilities... but writing your implementation for one class in multiple .cpp files is a -bad- idea.

Bill C

include "yourfilename.cpp"

Andrew

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.