Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Cast a struct ...

Reply
Thread Tools

Cast a struct ...

 
 
Konrad Mühler
Guest
Posts: n/a
 
      05-20-2008
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
 
Reply With Quote
 
 
 
 
Konrad Mühler
Guest
Posts: n/a
 
      05-20-2008
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
error C2440: 'return' : cannot convert from 'const char *' to 'const unsigned short *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast Abhijit Bhadra C++ 2 12-01-2004 04:43 PM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM
malloc - to cast or not to cast, that is the question... EvilRix C Programming 8 02-14-2004 12:08 PM
to cast or not to cast malloc ? MSG C Programming 38 02-10-2004 03:13 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57