Simple C++ question

Started by
4 comments, last by darkawakenings 20 years, 8 months ago
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?
Advertisement
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.
~~~~~Screaming Statue Software. | OpenGL FontLibWhy does Data talk to the computer? Surely he's Wi-Fi enabled... - phaseburn
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!}

--{You fight like a dairy farmer!}

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.
-------------"On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question."- Charles Babbage (1791-1871)
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
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.

This topic is closed to new replies.

Advertisement