Archived

This topic is now archived and is closed to further replies.

darkawakenings

Simple C++ question

Recommended Posts

I have a function that accepts four instances of StructA as parameters. StructA is a simple struct that only holds there floats. I want to be able to do something like this: MyFunc(StructA={1.0, 2.4, 6.4), arg2, arg3, arg4); so that i can pass a StructA as arg 1 without actually making one. How would I do this?

Share this post


Link to post
Share on other sites
you can do this with a class, but I''m not sure how to do it with a struct.

class ClassA
{
public:
ClassA() {;}
ClassA(float x2, float y2, float z2) {x = x2; y=y2; z=z2;}
~ClassA() {;}
float x;
float y;
float z;
}

Then I''d use it like this.

MyFunc(ClassA(1.0, 2.4, 6.4), arg2, arg3, arg4);

Hopefully that will lead you to how to do it with a struct.

~~~~~
"That has to be the most impressive collection of blatently incorrect statements I''ve seen on these boards for a while." -benjamin bunny
Download and play Slime King I.

Share this post


Link to post
Share on other sites
I''m not sure I understand what you''re asking. If you want your function to take StructA as parameters just protype it like this:

void MyFunc(StructA arg1,StructA arg2,StructA arg3,StructA arg4);

So what exactly is the problem? And what do you mean so you can pass arg 1 without actually making one. You can''t pass a parameter to a function that doesn''t exist; it doesn''t make any sense But there is a feature called a default value that you can set in the function declaration. It''s the value the function will assume if an argument is omitted during the call. Is that what you''re looking for?





--{You fight like a dairy farmer!}

Share this post


Link to post
Share on other sites
quote:
Original post by darkawakenings
I have a function that accepts four instances of StructA as parameters. StructA is a simple struct that only holds there floats. I want to be able to do something like this:

MyFunc(StructA={1.0, 2.4, 6.4), arg2, arg3, arg4);

so that i can pass a StructA as arg 1 without actually making one. How would I do this?





Well... you can''t really. You could overload the function to accept three floats instead of a StructA, which will do (I think) pretty much what you want.

Share this post


Link to post
Share on other sites
I dont get why you would want to do this unless you were using templates... three floats would give the same exact results as passing a struct right? You''re basically trying to pass multiple parameters in, whats wrong with that XP

Share this post


Link to post
Share on other sites
quote:
Original post by dede
you can do this with a class, but I''m not sure how to do it with a struct.

class ClassA
{
public:
ClassA() {;}
ClassA(float x2, float y2, float z2) {x = x2; y=y2; z=z2;}
~ClassA() {;}
float x;
float y;
float z;
}

Then I''d use it like this.

MyFunc(ClassA(1.0, 2.4, 6.4), arg2, arg3, arg4);



Two things about that:

1, you can use the same syntax for a struct (minus the "public:" line)

2, that code will still make a new struct, it''ll just throw it away afterwords.

If you are really keen on passing a struct but not creating new ones, consider using a static variable.

Share this post


Link to post
Share on other sites