Link to home
Start Free TrialLog in
Avatar of runxctry
runxctry

asked on

struct declaration and definition inside a parameter list

This is a piece of code I wrote to demonstrate passing structs as parameters.  The catch is that they are declared WITHIN the parameter list.  Which are not valid, and why they are not valid?  

Context - two compilers that I'm using can't seem to agree what is proper syntax.  One throws syntax errors.  My guess is that the one of the compilers have a bug in it.

CODE:

#include <iostream.h>

struct three_integer_struct
{
      int a;
      int b;
      int c;

      three_integer_struct
      (
            int z_a,
            int z_b,
            int z_c
      ) :
      a( z_a ),
      b( z_b ),
      c( z_c ) {};
};

void display ( three_integer_struct & data )
{
      cout << data.a <<endl <<data.b <<endl <<data.c <<endl;
}

void main()
{
      display ( (three_integer_struct) {1,2,3} );                  // Is this valid?
      display ( three_integer_struct {4,5,6} );                  // Is this valid?
      display ( three_integer_struct hello = {7,8,9} );      // Is this valid?
      display ( three_integer_struct hello {10,11,12} );      // Is this valid?
      display ( three_integer_struct (13,14,15) );            // This is valid.
}
Avatar of Infinity08
Infinity08
Flag of Belgium image

>>       display ( (three_integer_struct) {1,2,3} );                  // Is this valid?

No. You can't initialize something without telling the compiler what it is, and then try to cast it.

>>       display ( three_integer_struct {4,5,6} );                  // Is this valid?

No. This is syntactically incorrect.

>>       display ( three_integer_struct hello = {7,8,9} );      // Is this valid?

No. You can't initialize with {} when you provide a constructor. Plus you can't create the variable like that inside the function call parameters.

>>       display ( three_integer_struct hello {10,11,12} );      // Is this valid?

No. This is syntactically incorrect.

>>       display ( three_integer_struct (13,14,15) );            // This is valid.

No. You can't pass a reference to a temporary value.


Here's a correct way to do what you want :

      three_integer_struct hello(13,14,15);
      display(hello);
ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This will also work:

void display ( three_integer_struct * data )
{
      cout << data->a <<endl <<data->b <<endl <<data->c <<endl;
}

void main()
{
      display ( new three_integer_struct (13,14,15) );            // This is valid.
}
Best Regards,
DeepuAbrahamK
>>       display ( new three_integer_struct (13,14,15) );            // This is valid.

Nooooooo !!

Memory leak alert :)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of runxctry
runxctry

ASKER

Thanks, Infinity08 --

==
>>       display ( three_integer_struct hello = {7,8,9} );      // Is this valid?

No. You can't initialize with {} when you provide a constructor. Plus you can't create the variable like that inside the function call parameters.
==
You're right about the constructor thing.  Let's say I didn't provide one.

==
>>       display ( three_integer_struct (13,14,15) );            // This is valid.

No. You can't pass a reference to a temporary value.
==
In this case, I'm actually passing a temporary value to a reference.  This seems valid, especially since the display(...) function will consider the data as local scope.  Only once the flow of control passes this line (in main) will the temporary go out of scope.

Thanks again for your help.
Thanks, rstavely.

Is there anything syntactically or logically incorrect about passing the DATA non-const, in your example (not the reference)?  It seems like it should work either way to me.

Infinity - my goal is to keep it a temporary to the scope of that one line, not the whole function.  Thanks for the memory leak comment - I seriously wouldn't have caught that.
Upping the point value...  Will increase more as discussion increases and no answers (yep - subjective, sorry) present themselves.
>> You're right about the constructor thing.  Let's say I didn't provide one.
Then you could do something like this :

      #include <iostream>

      using namespace std;

      struct three_integer_struct
      {
            int a;
            int b;
            int c;
      };

      void display ( three_integer_struct data )
      {
            cout << data.a <<endl <<data.b <<endl <<data.c <<endl;
      }

      int main(void)
      {
            display ((three_integer_struct) {7,8,9});
            return 0;
      }

Note that I used <iostream> instead of <iostream.h>. The latter should NOT be used.


>> In this case, I'm actually passing a temporary value to a reference.
No, you're taking the reference of a temporary value, and pass that as parameter to the function. If you want to use references, make it a const reference like rstaveley suggested.

>> Only once the flow of control passes this line (in main) will the temporary go out of scope.
That doesn't matter. It's not allowed by the C++ standard.
>> my goal is to keep it a temporary to the scope of that one line, not the whole function.

Then either use rstaveley's const reference solution, or my pass-by-value solution.
Infinity08 and rstavely --

===
No, you're taking the reference of a temporary value, and pass that as parameter to the function. If you want to use references, make it a const reference like rstaveley suggested.
===
Yes, you are right.  Thank you.  From Stroustrup, Section 5.5, "References" --
" References to variables and references to constants are distinguished because introducing a temporary for a variable would have been highly error-prone; an assignment to the variable would become an assignment to the - soon to disappear - temporary.  No such problem exists for references to constants, and references to constants are often important as function arguments."

Infinity08  - regarding your code  (thanks for that by the way) I have your own words to reply to you.  
" You can't initialize something without telling the compiler what it is, and then try to cast it. "
>> " You can't initialize something without telling the compiler what it is, and then try to cast it. "
That was when you added a constructor to the struct. Without a constructor, the compiler is intelligent enough to understand what you want to do.
My apologies for the confusion.
Infinity and rstavely -- thank you both very much for your help.  I appreciate your time, and this was a great thing I learned today.  I now feel that I haven't given either of you enough points for your time and I would be pleased to give more, except I don't know how to do that.

About me - I'm a fairly junior engineer, still learning the ropes, so your wisdom is infinitely appreciated.
No need for more points, runxctry. I'm just glad I could be of assistance :)

Good luck (and hopefully also a lot of fun) in learning the inner workings of C++ - it will help you a lot in your further career !

If you have any questions and/or problems, feel free to ask !
That was a vibrant Q&A. I'm sorry it caught me at the end of my shift. I can't improve on the wisdom of Stroustrup. Good luck, runxctry, and many thanks.