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. How to pass pointers and process them using ellipsis ?

How to pass pointers and process them using ellipsis ?

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialc++question
7 Posts 2 Posters 5 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    I understand the concept of using ellipsis to pass variable number of parameters to a function. However, the tutorial examples pass a simple values to demonstrate the workings of "list" . I like to pass a object / widget (pointer) parameter and process the passed parameter. For example equivalent to this: object -> add text (text ) Can that be done and if so can you post actual C++ code ? Am I actually trying to do similar as passing a function as a parameter ? My code call looks like this ellipsisFunction( 2, listWidget_1, lisTWidget_2) /* va_start example */ #include /* printf */ #include /* va_list, va_start, va_arg, va_end */ void PrintFloats (int n, ...) { int i; double val; printf ("Printing floats:"); va_list vl; va_start(vl,n); for (i=0;i

    L 1 Reply Last reply
    0
    • L Lost User

      I understand the concept of using ellipsis to pass variable number of parameters to a function. However, the tutorial examples pass a simple values to demonstrate the workings of "list" . I like to pass a object / widget (pointer) parameter and process the passed parameter. For example equivalent to this: object -> add text (text ) Can that be done and if so can you post actual C++ code ? Am I actually trying to do similar as passing a function as a parameter ? My code call looks like this ellipsisFunction( 2, listWidget_1, lisTWidget_2) /* va_start example */ #include /* printf */ #include /* va_list, va_start, va_arg, va_end */ void PrintFloats (int n, ...) { int i; double val; printf ("Printing floats:"); va_list vl; va_start(vl,n); for (i=0;i

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      If you pass a list of random types (numbers, strings, objects), then your function needs some method of determining what each reference is. That is why printf (and its derivatives) requires a format string to identify the type of each parameter.

      L 1 Reply Last reply
      0
      • L Lost User

        If you pass a list of random types (numbers, strings, objects), then your function needs some method of determining what each reference is. That is why printf (and its derivatives) requires a format string to identify the type of each parameter.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        I am aware that compiler by itself cannot determine the type of variable passed. What I am missing is - how iterating thru the list works .

        K L 2 Replies Last reply
        0
        • L Lost User

          I am aware that compiler by itself cannot determine the type of variable passed. What I am missing is - how iterating thru the list works .

          K Offline
          K Offline
          k5054
          wrote on last edited by
          #4

          I'm not sure exactly where you're stuck. But lets start with the example you gave in your original post:

          void PrintFloats (int n, ...)
          {
          int i;
          double val;
          printf ("Printing floats:");
          va_list vl;
          va_start(vl,n);
          for (i=0;i

          Assuming that you're stuck here, then you might complete this something like

          for(i = 0; i < n; ++i) {
              val = va\_arg(vl, float);
              printf("%f ", val);
          }
          va\_end(vl);
          putchar('\\n');
          

          In this case I'm assuming that the int n parameter tells the function how many float values to expect. Other options to tell a variadic function how many arguments to expect are to use some sort of format string, like printf() does, or you can use some sort of sentinel value for your function e.g.

          PrintFloats(1.1, 2.2, -15.2, nan(""));

          In this case we have assumed that a NaN won't be part of the input string, so is a fair choice for a sentinel.

          Keep Calm and Carry On

          L 1 Reply Last reply
          0
          • L Lost User

            I am aware that compiler by itself cannot determine the type of variable passed. What I am missing is - how iterating thru the list works .

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            As I said, you need some control to identify hoe many parameters are being passed, and what type they are. There is no other way of knowing when you get to the end of the list.

            1 Reply Last reply
            0
            • K k5054

              I'm not sure exactly where you're stuck. But lets start with the example you gave in your original post:

              void PrintFloats (int n, ...)
              {
              int i;
              double val;
              printf ("Printing floats:");
              va_list vl;
              va_start(vl,n);
              for (i=0;i

              Assuming that you're stuck here, then you might complete this something like

              for(i = 0; i < n; ++i) {
                  val = va\_arg(vl, float);
                  printf("%f ", val);
              }
              va\_end(vl);
              putchar('\\n');
              

              In this case I'm assuming that the int n parameter tells the function how many float values to expect. Other options to tell a variadic function how many arguments to expect are to use some sort of format string, like printf() does, or you can use some sort of sentinel value for your function e.g.

              PrintFloats(1.1, 2.2, -15.2, nan(""));

              In this case we have assumed that a NaN won't be part of the input string, so is a fair choice for a sentinel.

              Keep Calm and Carry On

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              OK, let's move on. The variadic function contains the passed parameters count. The va_arg gets the parameter TYPE and according to the doc it has to match some specification. ( I gather the type can be also passed...but that is way over my head for now ) If I desire to pass type which does not meet this spec - all bets are off. I am trying to pass a pointer to an object. Does that sounds reasonable ? The example I used has type float for(i = 0; i < n; ++i) { val = va_arg(vl, float); replacing float with an object printf("%f ", val); } va_end(vl); putchar('\n');

              K 1 Reply Last reply
              0
              • L Lost User

                OK, let's move on. The variadic function contains the passed parameters count. The va_arg gets the parameter TYPE and according to the doc it has to match some specification. ( I gather the type can be also passed...but that is way over my head for now ) If I desire to pass type which does not meet this spec - all bets are off. I am trying to pass a pointer to an object. Does that sounds reasonable ? The example I used has type float for(i = 0; i < n; ++i) { val = va_arg(vl, float); replacing float with an object printf("%f ", val); } va_end(vl); putchar('\n');

                K Offline
                K Offline
                k5054
                wrote on last edited by
                #7

                It's pretty straight forward, really. When you use the va_arg macro, the second argument is the received objects type. So, to receive a pointer to a type, you use va_arg(args, _Obj_*) where Obj is some object type - e.g. class, int, double, struct, etc. Here's a short working example:

                #include
                #include

                struct S {
                int data;
                S(int d) : data(d) {}
                };

                void f(size_t n, ...)
                {
                va_list args;
                va_start(args, n);
                for(size_t i = 0; i < n; ++i) {
                S* ptr = va_arg(args, S*);
                std::cout << ptr->data << '\n';
                }
                }

                int main()
                {
                S item1(1);
                S item2(2);
                S item3(3);

                f(3, &item1, &item2, &item3);
                

                }

                Keep Calm and Carry On

                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