string and function parameter

This is a discussion on string and function parameter within the C++ Programming forums, part of the General Programming Boards category; Hello, I've been trying to use std:: string with the function's ... functionality. I know how to do it with ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    132

    string and function parameter

    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.

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Are you wanting to know how to use a string as a function parameter? If so:
    Code:
    void myFunction(string Para1)
    {
        someOperation(Para1);
    }

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    132
    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.

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    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.

  5. #5
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Code:
    void Error (string format, ...) {
       va_list ap;
       va_start, va_end, etc... im confused a bit here.
    }
    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)

  6. #6
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    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

  7. #7
    Registered User
    Join Date
    Jun 2002
    Posts
    132
    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.

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Try:
    Code:
    void Error (string format, ...) {
       string* pErr = &format + sizeof(string);
       string& errString = *pErr;
    }
    
    ...
    
    std::string err = "Some error";
    Error("some format", err);
    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** 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.

  9. #9
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    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);
    }
    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.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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

  11. #11
    Registered User
    Join Date
    Dec 2004
    Posts
    95
    Mostly compilers don't check types variable parameter lists (IIRC), so non-POD objects wouldn't be able to copy correctly anyway I suppose.

  12. #12
    Registered User
    Join Date
    Jun 2002
    Posts
    132
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21