Connecting Tech Pros Worldwide Help | Site Map

Cast a struct ...

 
LinkBack Thread Tools Search this Thread
  #1  
Old June 27th, 2008, 04:43 PM
=?ISO-8859-1?Q?Konrad_M=FChler?=
Guest
 
Posts: n/a
Default 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

  #2  
Old June 27th, 2008, 04:43 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: Cast a struct ...

Konrad Mühler wrote:
Quote:
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
  #3  
Old June 27th, 2008, 04:43 PM
=?ISO-8859-1?Q?Konrad_M=FChler?=
Guest
 
Posts: n/a
Default Re: Cast a struct ...

You can either define a constructor in your struct or define a
Quote:
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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.