Sign in to follow this  
icecubeflower

passing structs

Recommended Posts

Hey if you make a struct isn't it automatically passed by reference? Say you have this:
struct boner
{
   int inum;
   string strfun[75];
};

void wondermethod(boner funtimes);

int main()
{
   boner bigfun;
   int ix;

   bigfun.inum=80;
   bigfun.strfun[3]="stuff";
   wondermethod(bigfun);

   for(ix=0;ix<bigfun;ix++)
   {
      cout<<bigfun.strfun[3].c_str()<<'\n';
   }

   return 0;
}

void wondermethod(boner funtimes);
{
   funtimes.strfun[6]=" sucks!"
   funtimes.strfun[3]+=funtimes.strfun[6];
   funtimes.inum-=70;
}



Wouldn't that output this no matter what machine and what compiler you use?:
stuff sucks!
stuff sucks!
stuff sucks!
stuff sucks!
stuff sucks!
stuff sucks!
stuff sucks!
stuff sucks!
stuff sucks!
stuff sucks!


I think I remember that any structures you make are automatically passed by reference so saying either of these is the same thing, right?
void wondermethod(boner funtimes);
void wondermethod(boner &funtimes);


If I remember right the only way to make a struct pass by value is if you make a copy constructor for it. If I'm right please don't bother explaining what a copy constructor is, I don't need to know that right now. All I want to know is if I was right about everything or if I'm wrong and that struct above gets passed by value. In which case the output would look like this I believe:
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff


Share this post


Link to post
Share on other sites
Structs are not automatically passed by reference. You must do that explicitly. If not, the struct is copied member-wise onto the stack of the invoked function in your example.

Share this post


Link to post
Share on other sites
Quote:
Original post by icecubeflower
Hey if you make a struct isn't it automatically passed by reference?


No. Try it and see. And where did you get that idea from, anyway?

Quote:

Wouldn't that output this no matter what machine and what compiler you use?:


No.

Quote:
I think I remember that any structures you make are automatically passed by reference so saying either of these is the same thing, right?


No.

Quote:
If I remember right the only way to make a struct pass by value is if you make a copy constructor for it.


No. Anyway, a copy constructor is generated automatically for structs (and classes) which don't have one defined already.

C++ is based on value semantics. Copying things accidentally is easy; sharing them is hard. Python, for example, is based on reference semantics: sharing things accidentally is easy and copying them is "hard" (except that there is a library function that can copy almost anything).

Quote:
If I'm right please don't bother explaining what a copy constructor is, I don't need to know that right now.


Why do you imagine that you're a better judge of what you need to know than the people you're asking for help?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this