473,785 Members | 2,309 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(POIN T myVar) { ... }

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

myFunction(POIN T(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 2067
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(POIN T myVar) { ... }

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

myFunction(POIN T(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(POIN T(1,2));

or
POINT makePOINT(int x, int y) {
POINT p = { x, y };
return p;
}
...
myFunction(make POINT(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(POIN T(1,2));

or
POINT makePOINT(int x, int y) {
POINT p = { x, y };
return p;
}
...
myFunction(make POINT(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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
3346
by: Jamie Burns | last post by:
Hello, I am writing a client / server application. There is 1 server, and many clients. The server processes requests from each client, and typically creates and manipulates C++ objects on their behalf. Now, when a client requests for an object to be created, I pass back a pointer to the object (from server memory address scope) as a "long integer". To the client, this is just an ID for the object they wish to access. From then on,...
4
9313
by: William Payne | last post by:
Hello! If a cast from a void pointer to a pointer to a struct fails at runtime, is NULL the result? I.E, is this code meaningful: struct my_struct_t foo* = (struct my_struct_t*)void_pointer; // void_pointer is of type void* if(foo == NULL) { printf("Error!\n"); }
7
2538
by: James Mcguire | last post by:
Hi, I frequently do non-initialisation type structure assignment via casting: e.g. struct s{int i,j,k;} mys; .... mys=(struct s){3,4,5};
11
2053
by: Alberto Giménez | last post by:
Hi, I've seen some object oriented programming bits out there and i'm not sure if they're legal. For example: struct Object { int field1; int field2; }; struct SubObject { int field1; /* the same as Object */
3
14508
by: Pablo Gutierrez | last post by:
I have a C# method that reads Binary data (BLOB type) from a database and returns the data an array of bytes (i.e byte outbyte = new byte;). The BLOB column is saved into the database by a C program (UNIX) as an array of "struct point" where struct point //C structure { int Time; //32 bits
28
3068
by: Tamir Khason | last post by:
Follwing the struct: public struct TpSomeMsgRep { public uint SomeId;
6
527
by: Jack | last post by:
Is it possible to cast a 4-byte data type (e.g. an unsigned int) to a 4-byte struct? Sometimes the HIWORD and LOWORD of a 4-byte value contain information independent of each other. Is there a direct way to perform this cast? If not, is there way in C++ to do it? For example, I would like my function test() to work the way it is coded below. It produces a compiler error though: #include <pshpack2.h// 16-bit padding
2
7787
by: lovecreatesbea... | last post by:
I read `Advanced Programming in the UNIX Environment, 2nd ed.'. In section 16.3.2, the book says that variables type struct sockaddr_in; and/or struct sockaddr_in6; will be cast explicitly to variables type struct sockaddr; Is this safe? Or will these casts between different structs result in object slices? see casts at line 6 & 9 for example: struct sockaddr add1, *add2; /*see struct declarations below*/ struct sockaddr_in ...
17
2528
by: arnuld | last post by:
I have checked the FAQ: http://c-faq.com/malloc/mallocnocast.html FAQ discusses a special case when programmer has forgotten to do #include <stdlib.h>. I am including this header and I am not doing any explicit cast: #include <stdlib.h>
7
3951
by: * Tong * | last post by:
Hi, I couldn't figure out how to properly type cast in this case: $ cat -n type_cast.c 1 #include <stdio.h> 2 3 typedef unsigned char Byte; 4 typedef signed char Small_Int; 5
0
9489
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10357
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10162
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10101
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8988
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7509
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4063
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2893
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.