Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. check how many variable arguments are passed?

check how many variable arguments are passed?

Scheduled Pinned Locked Moved C / C++ / MFC
question
15 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H hansipet

    Is it possible to check how many variable arguments are passed to a function? Best regards Hansjörg

    R Offline
    R Offline
    Roger Stoltz
    wrote on last edited by
    #3

    Have a look here[^].


    "It's supposed to be hard, otherwise anybody could do it!" - selfquote
    "High speed never compensates for wrong direction!" - unknown

    H 1 Reply Last reply
    0
    • R Roger Stoltz

      Have a look here[^].


      "It's supposed to be hard, otherwise anybody could do it!" - selfquote
      "High speed never compensates for wrong direction!" - unknown

      H Offline
      H Offline
      hansipet
      wrote on last edited by
      #4

      May be that I have missed something. but here I don't can find something about the number of arguments...

      CPalliniC 1 Reply Last reply
      0
      • H hansipet

        May be that I have missed something. but here I don't can find something about the number of arguments...

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #5

        The sample shows how you can implement functions with a variable number of arguments using C. :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

        In testa che avete, signor di Ceprano?

        H 1 Reply Last reply
        0
        • CPalliniC CPallini

          The sample shows how you can implement functions with a variable number of arguments using C. :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

          H Offline
          H Offline
          hansipet
          wrote on last edited by
          #6

          HOw this works I know. But I need a way to know the number of variable arguments. Because if no one are used, I want to optimize the function...it's a lot faster. Best regards Hansjörg

          CPalliniC 1 Reply Last reply
          0
          • H hansipet

            HOw this works I know. But I need a way to know the number of variable arguments. Because if no one are used, I want to optimize the function...it's a lot faster. Best regards Hansjörg

            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #7

            if optimization is your concern, then avoid to use variable number of arguments (or use a mechanism such as the main one, i.e. an array of arguments and its length passed to your function). BTW, referring to the MSDN sample (link given by Roger Stoltz) [^], nothing prevents you to use the marker to indicate the total number of arguments: does this fit your needs? :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

            In testa che avete, signor di Ceprano?

            H 1 Reply Last reply
            0
            • CPalliniC CPallini

              if optimization is your concern, then avoid to use variable number of arguments (or use a mechanism such as the main one, i.e. an array of arguments and its length passed to your function). BTW, referring to the MSDN sample (link given by Roger Stoltz) [^], nothing prevents you to use the marker to indicate the total number of arguments: does this fit your needs? :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

              H Offline
              H Offline
              hansipet
              wrote on last edited by
              #8

              The marker is not so a good was i think... The problem is that it is not possible to define 2 methods in a class (same name). One with variable length parameter and one without...This would be the best solution for me too... I want to make a logger class in this way... class Logger{ void Log(LPCSTR format,...); void Log(LPCSTR message); } something like that. It is easier for the user to have allways the same name...Do you know any solution for that? arrays are also not so good..it's not easy to use... if not I have to give the function another name.... :( Best regards hansjörg

              CPalliniC R 2 Replies Last reply
              0
              • H hansipet

                The marker is not so a good was i think... The problem is that it is not possible to define 2 methods in a class (same name). One with variable length parameter and one without...This would be the best solution for me too... I want to make a logger class in this way... class Logger{ void Log(LPCSTR format,...); void Log(LPCSTR message); } something like that. It is easier for the user to have allways the same name...Do you know any solution for that? arrays are also not so good..it's not easy to use... if not I have to give the function another name.... :( Best regards hansjörg

                CPalliniC Offline
                CPalliniC Offline
                CPallini
                wrote on last edited by
                #9

                hansipet wrote:

                Do you know any solution for that?

                No. BTW, why don't you emulate the behaviour of cout instead of the one of printf? :)

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

                In testa che avete, signor di Ceprano?

                H 1 Reply Last reply
                0
                • CPalliniC CPallini

                  hansipet wrote:

                  Do you know any solution for that?

                  No. BTW, why don't you emulate the behaviour of cout instead of the one of printf? :)

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

                  H Offline
                  H Offline
                  hansipet
                  wrote on last edited by
                  #10

                  This would be a great solution...but how I can do this?

                  CPalliniC 1 Reply Last reply
                  0
                  • H hansipet

                    This would be a great solution...but how I can do this?

                    CPalliniC Offline
                    CPalliniC Offline
                    CPallini
                    wrote on last edited by
                    #11

                    The tricky part is customizing the >> operator. Have a look, for instance, at this article http://www.codeproject.com/cpp/Encoder.asp[^]. :)

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

                    In testa che avete, signor di Ceprano?

                    H 1 Reply Last reply
                    0
                    • CPalliniC CPallini

                      The tricky part is customizing the >> operator. Have a look, for instance, at this article http://www.codeproject.com/cpp/Encoder.asp[^]. :)

                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

                      H Offline
                      H Offline
                      hansipet
                      wrote on last edited by
                      #12

                      Thank you...I will check this..

                      1 Reply Last reply
                      0
                      • H hansipet

                        The marker is not so a good was i think... The problem is that it is not possible to define 2 methods in a class (same name). One with variable length parameter and one without...This would be the best solution for me too... I want to make a logger class in this way... class Logger{ void Log(LPCSTR format,...); void Log(LPCSTR message); } something like that. It is easier for the user to have allways the same name...Do you know any solution for that? arrays are also not so good..it's not easy to use... if not I have to give the function another name.... :( Best regards hansjörg

                        R Offline
                        R Offline
                        Roger Stoltz
                        wrote on last edited by
                        #13

                        If you're using this for logging purposes, why don't use CString::FormatV()? Like this:

                        void <YourClassName>::Log( LPTSTR pText, ... )
                        {
                        CString LogText;
                        va_list args;
                        va_start( args, pText );
                        LogText.FormatV( pText, args );

                        // Do whatever you want with the LogText...
                        

                        }

                        This would make the function call similar to printf().


                        "It's supposed to be hard, otherwise anybody could do it!" - selfquote
                        "High speed never compensates for wrong direction!" - unknown

                        H 1 Reply Last reply
                        0
                        • R Roger Stoltz

                          If you're using this for logging purposes, why don't use CString::FormatV()? Like this:

                          void <YourClassName>::Log( LPTSTR pText, ... )
                          {
                          CString LogText;
                          va_list args;
                          va_start( args, pText );
                          LogText.FormatV( pText, args );

                          // Do whatever you want with the LogText...
                          

                          }

                          This would make the function call similar to printf().


                          "It's supposed to be hard, otherwise anybody could do it!" - selfquote
                          "High speed never compensates for wrong direction!" - unknown

                          H Offline
                          H Offline
                          hansipet
                          wrote on last edited by
                          #14

                          I think this is in the MFC library and I don't can use it...

                          R 1 Reply Last reply
                          0
                          • H hansipet

                            I think this is in the MFC library and I don't can use it...

                            R Offline
                            R Offline
                            Roger Stoltz
                            wrote on last edited by
                            #15

                            hansipet wrote:

                            I think this is in the MFC library and I don't can use it...

                            Ok, then use vsprintf(). See here[^]. It works the same as the CString alternative, but in this case you have to provide the output buffer.


                            "It's supposed to be hard, otherwise anybody could do it!" - selfquote
                            "High speed never compensates for wrong direction!" - unknown

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups