Jump to content

Creating a class solely for the storage of methods. A good or bad practice?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
Casey

Casey

    Newbie

  • Members
  • Pip
  • 5 posts
I'm relatively new to programming, and I'm making some classes to simplify file handling and read input from an ini file to bypass recompiling every time I want to change settings.

I thought it would be convenient to create a search class which could gather data from either text or binary files and load said data into variables it was given.

Its use would look something like this:


#include "search.h"


double a;

long int b;

__int8 c;


search seeker;


seeker.string(search term, array to search through, &a <-- where to store retrieved data);


seeker.binary(search term2, array2 to search through, &b <--will overload to correct method, # of bytes to retrieve);


seeker.bbinary(search term3, array2 to search through, &c, # of bytes to retrieve ); <--Handles big endian.


Now, I realize I could simply create the above functions and their complementary overloads and place them in a header, however, being able to go


seeker.method(parameters)


gives, I feel, the code an organizational boost.

So, my questions are:

1. Is it a bad practice to use classes solely for the storage of methods?

2. Would my code be made less efficient, were I to declare a class full of methods and use only one of them instead of putting them as functions into a header and just using the one I need?

Example of 2.


#include "search.h"


search seeker;


seeker.binary(parameters);



vs.


#include "search.h"


binary(parameters);



#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
It's somewhat a matter of taste. In Java, you're required to do that. In C++, I'd consider just using a namespace for the functions, rather than a class, unless there actually is some data (such as the file being operated on) that it would make sense to encapsulate.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Casey

Casey

    Newbie

  • Members
  • Pip
  • 5 posts
I haven't worked with namespaces yet.

Would that end up looking, approximately, like:

search::binary(parameters)
?

That would look relatively organized.


I'll read up on namespaces then.


I'm still curious if there are detrimental effects when declaring an instance of a class if a large part of what is contained in that class goes unused.

Say declaring an instance of a class with 10,000 methods of which only 5 will be used, as opposed to creating a namespace with 10,000 functions of which only five will be used.

What should I be looking into to find information on these kinds of questions? Questions, I guess, that deal with how code is processed and handled after I press the compile/execute button.

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
A lot of it is going to depend on the compiler. Personally, I would use a namespace instead of a class, but you can do either. Ultimately, I'd look into doing a few small tests to see which seems to work best.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog