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
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
Related Q & A:
- How can I change a member's name?Best solution by Yahoo! Answers
- How can i become a member of sweetsingles?Best solution by Yahoo! Answers
- How can I burn a .rmvb file onto a DVD for viewing on a regular DVD player?Best solution by Yahoo! Answers
- How can I find a member's profile on Yahoo?Best solution by Yahoo! Answers
- How can I transfer all of my Yahoo email files to a document file in my documents?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.