Hello,
I've been trying to use std:: string with the function's ... functionality. I know how to do it with char*. Anyone can help?
Code:void Error (string format, ...) { va_list ap; va_start, va_end, etc... im confused a bit here. }
Hello,
I've been trying to use std:: string with the function's ... functionality. I know how to do it with char*. Anyone can help?
Code:void Error (string format, ...) { va_list ap; va_start, va_end, etc... im confused a bit here. }
Y3K Network http://www.y3knetwork.com
Bringing the software of the future on the net.
Are you wanting to know how to use a string as a function parameter? If so:
Code:void myFunction(string Para1) { someOperation(Para1); }
What?
I was asking about the ... parameter.Read it again.
Y3K Network http://www.y3knetwork.com
Bringing the software of the future on the net.
What do you mean by "the ... parameter"? You send a string object as a parameter correctly as the first paramater. I'm not the only one who has no idea what you're asking.
Are you talking about the ... parameter? I've never used it before. Do you know sean? (now that we're all on the same ball lol)Code:void Error (string format, ...) { va_list ap; va_start, va_end, etc... im confused a bit here. }
I think we need to see the entire function, or at least tell us what the function does. I don't understand what you want to do, but I don't think variadic functions (functions with a variable arguments list) are a good solution usually ...
"Think not but that I know these things; or think
I know them not: not therefore am I short
Of knowing what I ought."
-John Milton, Paradise Regained (1671)
"Work hard and it might happen."
-XSquared
Variadic functions, that one. Didn't know the term. Now, what didn't I explain well? I want to create a string from the variable argument list in the function parameters..
Y3K Network http://www.y3knetwork.com
Bringing the software of the future on the net.
Try:
I'm not sure if copy constructors etc. get called in variadic functions though, so if that doesn't work then:Code:void Error (string format, ...) { string* pErr = &format + sizeof(string); string& errString = *pErr; } ... std::string err = "Some error"; Error("some format", err);
Code:void Error (string format, ...) { string** ppErr = &format + sizeof(string); string* pErrString = *ppErr; string& errString = *pErrString; } ... std::string err = "Some error"; Error("some format", &err);
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.
I'm guessing you're trying to do something along those lines? I haven't tested it and I don't recall exactly how everything works, but that should be close enough for it to work with just a little tinkering.Code:void Error(const string& format, ...) { unsigned char* byteIndex = &format + sizeof(format); for(std::string::const_iterator it = format.begin(); it != format.end(); ++it) { switch(*it) { case 's': //Argument is a string* string& err = **(string**)byteIndex; byteIndex += sizeof(string*); cout << err << std::endl; break; case 'i': //Argument is an int int& err = *(int*)byteIndex; byteIndex += sizeof(int); cout << err << std::endl; break; etc.. } } } int main() { ... string err = "Error"; int errCode = 5; Error("is", errCode, &err); }
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.
If I remember correctly, the standard doesn't permit passing non-POD objects as variable parameters.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Mostly compilers don't check types variable parameter lists (IIRC), so non-POD objects wouldn't be able to copy correctly anyway I suppose.
Cool I will try all and post the reuslts! thanks
Y3K Network http://www.y3knetwork.com
Bringing the software of the future on the net.