AndreaM16 AndreaM16 - 2 months ago 7
C++ Question

Define multiple private functions with same return type and parameters type in one line

I'm totally new to

C++
and I was wondering if there's a way to define n functions which have same return type and parameters type in a single line in order to maintain a DRY code.

I tried looking for a solution online and on SO but I wasn't able to find a proper answer.

myHeader.h

class MyClass{

public:
. . .

private:
. . .
std::vector<Dcel::Vertex*> addVertices(std::vector <Dcel::Vertex*>);
std::vector<Dcel::Vertex*> randomPointsGetter(std::vector<Dcel::Vertex*>);
};


For instance, they both return
std::vector<Dcel::Vertex*>
type and take
std::vector <Dcel::Vertex*>
as parameters input.

Is it possible to define both of them, or n functions, in the same line? If yes, How?

Thanks in advance.

Answer

When the number of functions grows, a macro seems appropriate:

class MyClass {

#define MY_PREFIX_DECL_FN(name) \
    std::vector<Dcel::Vertex*> name(std::vector<Dcel::Vertex*>)

    MY_PREFIX_DECL_FN(addVertices);
    MY_PREFIX_DECL_FN(randomPointsGetter);
    MY_PREFIX_DECL_FN(...);

#undef MY_PREFIX_DECL_FN

};
Comments