I have a class called parser
with a member function madd_arguments(const argument args, ...)
. argument
is a separate class and has a member variable arg_name
to differentiate one from another. I want the function madd_arguments
to accept any number of argument
objects. To achieve this, I have the following code:
class argument{
private:
std::string arg_name;
public:
argument(const std::string& ARG_NAME) : arg_name(ARG_NAME){}
friend class parser;
};
#define add_arguments(...) madd_arguments(__VA_ARGS__, argument("END_HERE"))
#include "arg.h"
class parser {
private:
std::vector<argument> known_arguments; //holds all arguments
public:
void madd_arguments(const argument args, ...);
};
void parser::madd_arguments(const argument args, ...) {
va_list vargs;
for (va_start(vargs, args); args.arg_name != "END_HERE"; args = va_arg(vargs, const argument)){
known_arguments.push_back(args);
}
va_end(vargs);
}
and my main:
int main() {
argument a("name");
a.set_flags("-v", "-verbose", "bobby", "jones");
argument b("string argument");
parser p;
p.add_arguments(a,b);
}
but I get the following errors: error: passing ‘const argument’ as ‘this’ argument discards qualifiers [-fpermissive] _start(vargs, args); args.arg_name != "END_HERE"; args = va_arg(vargs, const argument)){
and error: passing ‘const std::vector<argument>’ as ‘this’ argument discards qualifiers [-fpermissive]known_arguments.push_back(args);
After looking at other posts on here I figured that I would have to make the member function madd_arguments
a const function but I can't because I am going to make changes to the variable known_arguments
which is a member of the parser
object.
I used macro and the va_list
based on this post: How do I write functions that accept unlimited arguments?. Anybody know how I can get around this or should I just go back to using variadic templates instead?
Aucun commentaire:
Enregistrer un commentaire