jeudi 18 juin 2020

How to wrap user functions?

I have three functions that will be used in different classes in a project. The functions use (only read) common variables, but manipulate the input data in a different way than each other.

I am not sure how to wrap those functions. I practically don't want common read-only variables to be available to the user. I came up with two solutions:

  1. C-Style header only approach. This solution lets user to see var0 and var1. Which I don't want. (Namespace approach also can't hide read-only variables AFAIK.)
#ifndef FUNC_H
#define FUNC_H
const int var0 = 30;
const int var1 = 75;

void func0(float input0)
{//manipulate input0//}

void func1(float input1)
{//manipulate input1//}

void func2(float input2)
{//manipulate input//}
#endif //FUNC_H
  1. Class approach
#ifndef FUNC_H
#define FUNC_H

class Func
{
    public:
        static void func0(float input0);
        static void func1(float input1);
        static void func2(float input2);

    private:
        Func();
        static const int var0 = 30;
        static const int var1 = 75;
};

void Func::func0(float input0){}
void Func::func1(float input1){}
void Func::func2(float input2){}

#endif //FUNC_H

This works but I am not sure if this is the right way of accomplishing what I want. Basically, I want to wrap them similar to STL libraries. What do you recommend?

Aucun commentaire:

Enregistrer un commentaire