Solved

struct declaration and definition inside a parameter list

Posted on 2007-03-27
15
449 Views
Last Modified: 2010-04-01
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.
}
0
Comment
Question by:runxctry
  • 7
  • 5
  • 2
  • +1
15 Comments
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
>>       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);
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 90 total points
Comment Utility
Of course, if you modify the display function like this (ie. do not pass by reference but by value) :

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

then you can call it like this :

      display(three_integer_struct(13,14,15));




Or you create the function like this (ie. pass by pointer) :

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

then you can call it like this :

      three_integer_struct hello(13,14,15);
      display(&hello);
0
 
LVL 11

Expert Comment

by:DeepuAbrahamK
Comment Utility
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
0
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
>>       display ( new three_integer_struct (13,14,15) );            // This is valid.

Nooooooo !!

Memory leak alert :)
0
 
LVL 17

Assisted Solution

by:rstaveley
rstaveley earned 85 total points
Comment Utility
>      display ( three_integer_struct (13,14,15) );

That would only be OK, if you made display take a const reference as its parameter.

i.e.
--------8<--------
void display (const three_integer_struct & data )
{
      cout << data.a <<endl <<data.b <<endl <<data.c <<endl;
}
--------8<--------

You can pass a reference to a const temporary object.
0
 
LVL 1

Author Comment

by:runxctry
Comment Utility
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.
0
 
LVL 1

Author Comment

by:runxctry
Comment Utility
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.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 1

Author Comment

by:runxctry
Comment Utility
Upping the point value...  Will increase more as discussion increases and no answers (yep - subjective, sorry) present themselves.
0
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
>> 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.
0
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
>> 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.
0
 
LVL 1

Author Comment

by:runxctry
Comment Utility
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. "
0
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
>> " 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.
0
 
LVL 1

Author Comment

by:runxctry
Comment Utility
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.
0
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
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 !
0
 
LVL 17

Expert Comment

by:rstaveley
Comment Utility
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.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now