Solved

struct declaration and definition inside a parameter list

Posted on 2007-03-27
15
470 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
ID: 18803092
>>       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
ID: 18803180
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
ID: 18803204
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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 53

Expert Comment

by:Infinity08
ID: 18803221
>>       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
ID: 18803226
>      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
ID: 18803256
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
ID: 18803299
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
 
LVL 1

Author Comment

by:runxctry
ID: 18803314
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
ID: 18803366
>> 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
ID: 18803379
>> 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
ID: 18803539
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
ID: 18803590
>> " 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
ID: 18803628
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
ID: 18803663
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
ID: 18808168
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Why is compiler in oracle server ? 9 66
Why isn't object file created? 6 58
c++, dynamic object by json 1 42
NEED HELP WITH VISUAL STUDIO 2017 (beginner) 6 39
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

772 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