0

I'm using C++ for implementing a project that requires to import a file once. All the data in this file needs to brought in memory as key value pairs, but only when the file is imported.

Since this is a one time operation, I don't feel like it requires a class. But I don't know where to put a function that does this file handling. Should I be defining a header file named helper_functions.h containing this and other such methods(saving the file once program is terminated)?

  • 2
    You could use a free function, but why? Adding the additional "class" syntax elements to a C++ file with one function is less text to type than writing your question above. – Doc Brown Mar 23 '16 at 9:04
3

Using a class for a feature that gets used only once in a program can be beneficial, if you look at it from a perspective of consistency and code organisation. So, how often an operation is performed shouldn't be the only consideration when deciding to create a class or not.

Regardless of whether you choose to create a class for this feature or not, the functions/class that form the interface of the feature should be in a header file named after the feature.
For classes, this is usually obvious because the class will also be named after the feature. For stand-alone functions this might be less obvious, but it is good to use the same logic in deciding on the header file to put the functions in.
The reasoning here is that it will be easier to find out which functions/classes are available in relation to feature X.

  • What if it is just 1 function? And I don't have anything relevant to it to put in the same header file. Don't you think this will increase the amount of header files way too much? – Ayush Gupta Mar 23 '16 at 8:00
  • 1
    @AyushGupta If the function itself is complex enough to deserve its own file along with its private helper functions/classes, then there's nothing wrong with having only a single function declaration inside the header. The header is only an epiphenomen of what's inside its respective implementation file. However, if the function is too simple/short to deserve it's own implementation file, just put the whole thing where you call the function. – cmaster Mar 23 '16 at 8:10
0

We don't know what is the file or data that you are "importing".

Perhaps you need some lexer and/or parser to make that import.

Perhaps you could use some existing format, e.g. JSON (for which there are good C++ libraries, like jsoncpp...)

The decision to make (or not) a class -and that is sometimes matter of taste and habits- is unrelated to file import. You could have a class as soon as you have data with member functions operating on it. But (contrarily to Java) not everything has to be class (or a hierarchy of them). Read also about abstract data types (which could be implemented as one or more classes). Also understand what is the relation between struct & class in C++.

Study some free software code related to your project. That could inspire you.

BTW, a C++ header file can, and often does, declare more than one class and it could also define some static inline functions and operator, and some enum or constexpr etc. Many small sized projects (e.g. of less than a hundred thousand lines) have a single header file (which might be precompiled and which would #include other system or library headers and #define some macros).

Notice also that standard C++ containers prefer to deal with classes, and you'll better be aware of the rule of five & RAII idiom. If you want a Redis like thing, you might be interested in std::map and/or std::unordered_map etc... and these containers really want classes for their contents (i.e. keys & values).

Understand that a C++ class is cheap. Its instance usually has a some vtable which generally costs only one hidden pointer per instance.

  • I'm trying to implement a minimal redis server in C++. It requires to load the Key:value pairs stored in the file to memory. Now to do this importing, I have to define either a class or a function and find a home file for it. Doing this for a class is trivial, give the file the name of the class. But I dont see how to do the same with the function. And I do understand the difference between class and struct, but I dont see how it is relevant here. – Ayush Gupta Mar 23 '16 at 8:12
  • I think you are focusing on unimportant details. Think more in abstract data types terms, and these are in C++ often implemented by one or several classes – Basile Starynkevitch Mar 23 '16 at 8:14
  • I'm a student still learning how to name and organize stuff in large projects. This is my first multi file project, if you know what I mean. In order for me to be able to manage a bigger project, I atleast need to know how to name files and organize things. – Ayush Gupta Mar 23 '16 at 8:20
  • No, it is your duty to name files and organize things (and you will be evaluated on how you do that). We won't help you. I gave some insights & advices, but you need to do your homework. – Basile Starynkevitch Mar 23 '16 at 8:22
  • Thanks for the insights, I'll keep it in mind the next time! – Ayush Gupta Mar 23 '16 at 8:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.