?
Solved

struct declaration and definition inside a parameter list

Posted on 2007-03-27
15
Medium Priority
?
494 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 360 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 340 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

752 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