470,855 Members | 1,248 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,855 developers. It's quick & easy.

Cast a struct ...

Hi,

I've a struct:

struct POINT {
int x,y;
};

And I want to commit a variable of type POINT in a function

myFunction(POINT myVar) { ... }

If I call the function, I want to write both values for x and y directly
in the function call like :

myFunction(POINT(1,2));

This doesn't work and I want to know, how I can do this. Or do I have to
define the struct as an independent class?

Thanks
Konrad
Jun 27 '08 #1
2 1886
Konrad Mühler wrote:
I've a struct:

struct POINT {
int x,y;
};

And I want to commit a variable of type POINT in a function

myFunction(POINT myVar) { ... }

If I call the function, I want to write both values for x and y directly
in the function call like :

myFunction(POINT(1,2));

This doesn't work and I want to know, how I can do this. Or do I have to
define the struct as an independent class?
You can either define a constructor in your struct or define a
stand-alone function that would return a struct made from two arguments
(sort of out-of-the-class constructor):

struct POINT {
int x,y;
POINT(int x_, int y_) : x(x_), y(y_) {} // constructor
};
...
myFunction(POINT(1,2));

or
POINT makePOINT(int x, int y) {
POINT p = { x, y };
return p;
}
...
myFunction(makePOINT(1,2));

Beware, that according to the current Standard, adding a user-defined
constructor prevents your class from being a POD. I think it's fixed in
the upcoming Standard. It actually may not mean anything different to
your program, but I just thought I'd mention it.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #2
You can either define a constructor in your struct or define a
stand-alone function that would return a struct made from two arguments
(sort of out-of-the-class constructor):

struct POINT {
int x,y;
POINT(int x_, int y_) : x(x_), y(y_) {} // constructor
};
...
myFunction(POINT(1,2));

or
POINT makePOINT(int x, int y) {
POINT p = { x, y };
return p;
}
...
myFunction(makePOINT(1,2));

Beware, that according to the current Standard, adding a user-defined
constructor prevents your class from being a POD. I think it's fixed in
the upcoming Standard. It actually may not mean anything different to
your program, but I just thought I'd mention it.
Thank you very much. It helped a lot :-)
Konrad
Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

31 posts views Thread by Jamie Burns | last post: by
4 posts views Thread by William Payne | last post: by
7 posts views Thread by James Mcguire | last post: by
11 posts views Thread by Alberto Giménez | last post: by
3 posts views Thread by Pablo Gutierrez | last post: by
28 posts views Thread by Tamir Khason | last post: by
6 posts views Thread by Jack | last post: by
2 posts views Thread by lovecreatesbea... | last post: by
17 posts views Thread by arnuld | last post: by
7 posts views Thread by * Tong * | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.