Question

i got the following code:

header file:

  #define html(...) anyHtml(__VA_ARGS__, NULL);
  const char* Html::anyHtml(const char* arg, ...);
  #define body(cssClass, ...) anyBody(cssClass, __VA_ARGS__, NULL);
  const char* Html::anyBody(const char* cssClass, const char* arg, ...);

cpp file:

const char* Html::anyHtml(const char* arg, ...) {
string temp = initOpen + tagToString.at(HTML) + end + lf + lf;
va_list arguments;
for (va_start(arguments, arg); arg != NULL; arg = va_arg(arguments, const char*)) {
temp += arg + lf;
}
va_end(arguments);
temp += deOpen + tagToString.at(HTML) + end + lf;
string *returnThis = new string(temp);
return (*returnThis).c_str();
}

const char* Html::body(const char* cssClass, const char* arg, ...) {
string temp = initOpen + tagToString.at(BODY) + " class=\"" + cssClass + "\"" + end + lf + lf;
va_list arguments;
for (va_start(arguments, arg); arg != NULL; arg = va_arg(arguments, const char*)) {
temp += arg + lf +lf;
}
va_end(arguments);
temp += deOpen + tagToString.at(BODY) + end + lf;
string *returnThis = new string(temp);
return (*returnThis).c_str();
}

main file:

Html *html = new Html();

cout << html -> html("test","test","test");
cout << html -> body("thisClass","test","test");

works fine!!

BUT THIS:

Html *html = new Html();

cout << html -> html(
    "hello",
    html -> body("thisClass","content")
    );

dont work.. if i outcomment the macro for anyBody in the header file

//#define body(cssClass, ...) anyBody(cssClass, __VA_ARGS__, NULL);

it is possible like this:

Html *html = new Html();

cout << html -> html(
    "hello",
    html -> body("thisClass","content",NULL)
    );

now my questions:

is it possible to nest some macros like

#define body(cssClass, ...) anyBody(cssClass, __VA_ARGS__, NULL);

among themselves ?

i´ve read that the whole "variable argument count" thing is still not really designed in cpp today?

if you take a look at the functions anyHtml() and anyBody(). is this a "good" solution for this issue or is it some kind of a dirty newbe hack? i´m still new to cpp coming along with perl and java mostly.. for this i want to get some feedback to get further with it..

now.. enough :-)

your welcome to tell me what u want to say.. :-)

---- EDIT

ok i´m sorry..

Html *html = new Html();

cout << html -> html(
    "hello",
    html -> body("thisClass","content")
    );

the output should be

<html>
hello
<body class="thisClass">
content
</body>
</html>

but it gives

C2143 Syntax Error missing ')' before ';'
C2143 Syntax Error missing ')' before ';'
C2059 Syntax Error: ')'

if i use both

#define html(...) anyHtml(__VA_ARGS__, NULL);
#define body(cssClass, ...) anyBody(cssClass, __VA_ARGS__, NULL);

if i make

cout << html -> html("hello","test");
cout << html -> body("thisClass","content");

these errors not appearing..

without this macros i have to type this:

 cout << html -> html(
    "hello",
    html -> body("thisClass","content",NULL)
    );

very special i know.. :-(

--- EDIT

thanks for fixing my issue!

the semicolon in the macro definition solved the nesting problem...

what about the functions anyHtml() and body()...

is it correct with using the

string temp = ...

and casting it after the string operations?

or is it dirty?

Was it helpful?

Solution

You have extraneous semicolon's in your macros. Remove them:

#define html(...) anyHtml(__VA_ARGS__, NULL)
#define body(cssClass, ...) anyBody(cssClass, __VA_ARGS__, NULL)

If you don't remove them, you end up with syntax errors, because the expansion turns into something like this:

cout << html -> anyHtml(
    "hello",
    html -> anyBody("thisClass", "content", NULL);, NULL);;

Edit: You asked:

Is it correct with using the string temp = ... and casting it after the string operations? or is it dirty?

You are not casting temp, but creating a copy of it in dynamically allocated memory. Although this allows you to pass back a valid C string, it is creating a memory leak, since you will not be able to call delete on the object you created.

You can just change your routines to return std::string, and then you can return temp directly without without needing dynamic allocation.

OTHER TIPS

You've got semicolons following your macro definitions, so you end up with a semicolon in your argument list for

cout << html -> html(
  "hello",
  html -> body("thisClass","content")
  );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top