473,614 Members | 2,089 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

one line struct initialization

Is it possible, as in C? I don't think it is. Just checking.

Zytan

Mar 7 '07 #1
21 10563
>Is it possible, as in C? I don't think it is. Just checking.
If you provide a constructor that will initialize all members.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Mar 7 '07 #2
On 6 Mar 2007 20:03:39 -0800, "Zytan" <zy**********@y ahoo.comwrote:
>Is it possible, as in C? I don't think it is. Just checking.
Why wouldn't it be possible? What exactly are you referring to?
--
http://www.kynosarges.de
Mar 7 '07 #3
Is it possible, as in C? I don't think it is. Just checking.
>
Why wouldn't it be possible? What exactly are you referring to?
I mean like so:

int x = 4; // declared and initialized in one line

struct abc { ... };
abc = ...; // declared and initialized in one line. Is this possible?

Zytan

Mar 7 '07 #4
Is it possible, as in C? I don't think it is. Just checking.
>
If you provide a constructor that will initialize all members.
I wasn't looking to use a constructor. But, maybe I need one.
Does "int x = 4;" invoke a constructor for 'int'? Is that how
declarations and initializations are done on the same line?

Zytan

Mar 7 '07 #5
struct abc { ... };
abc = ...; // declared and initialized in one line. Is this possible?
Oops, I mean:

struct abc { ... };
abc y = ...; // declared and initialized in one line. Is this
possible?

Zytan

Mar 7 '07 #6
>I wasn't looking to use a constructor. But, maybe I need one.
Well maybe you can get by with an implicit conversion operator
instead. It depends on what kind of expression you want on the right
hand side of the assignment.

One way or another the RHS expression must be turned into an instance
of your struct.

>Does "int x = 4;" invoke a constructor for 'int'?
No, but int in special since you can write literals for it in C# and
there are special IL opcodes for it.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Mar 7 '07 #7
Zytan <zy**********@y ahoo.comwrote:
>Is it possible, as in C? I don't think it is. Just checking.
If you provide a constructor that will initialize all members.

I wasn't looking to use a constructor. But, maybe I need one.
Does "int x = 4;" invoke a constructor for 'int'? Is that how
declarations and initializations are done on the same line?
Well, the primitive types are a special case - but yes, if you want to
initialize a struct, you generally need to call a constructor. If it's
a mutable struct (generally a bad idea) you *could* just do:

MyStruct x;
x.SomeProperty = ...;

but it's generally better to have a constructor which fully constructs
the struct, and call that.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 7 '07 #8
Well maybe you can get by with an implicit conversion operator
instead. It depends on what kind of expression you want on the right
hand side of the assignment.

One way or another the RHS expression must be turned into an instance
of your struct.
Yes, it sounds like using a constructor is the proper way, then.
Does "int x = 4;" invoke a constructor for 'int'?

No, but int in special since you can write literals for it in C# and
there are special IL opcodes for it.
Right, that's what I thought.

Zytan

Mar 7 '07 #9
Well, the primitive types are a special case - but yes, if you want to
initialize a struct, you generally need to call a constructor.
I guess I was just looking for something as simple as what you can do
with initializing an array:

int[] x = new int[] { 4, 5, 6, 7 };
If it's
a mutable struct (generally a bad idea) you *could* just do:

MyStruct x;
x.SomeProperty = ...;

but it's generally better to have a constructor which fully constructs
the struct, and call that.
Yes, I think I need a constructor. I did notice that C# complains if
you only initialize a portion of the fields in the struct (if you miss
one) and then attempt to use it. So, it is safe (perhaps not so in
earlier versions?). But, a c'tor seems more safe.

A mutable struct is a bad idea? What is a mutable struct? I know
mutable means it can change. But, unless all fields are private,
isn't the struct mutable? Or is this precisely what you mean, that
struct fields should all be private (almost like a class)?

Zytan

Mar 7 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
4727
by: Jayw710 | last post by:
hi all, this should be basic, but I can't seem to get the right syntax. i have a struct struct PTStruct { int x; int y;
4
11442
by: Greg B | last post by:
Well since getopt() doesn't seem to be compatible with Windows, and the free implementation of it for Windows that I found still had some annoying restrictions, I thought I'd whip up a simple parser myself. Just wanted to see if anyone could provide me with some constructive criticism :) any feedback would be greatly appreciated ----------------------------------------------------------------------------- #include "stdio.h" #include...
3
7646
by: sathyashrayan | last post by:
The standard confirms that the following initialization of a struct struct node { --- --- } struct node var = {NULL};
10
8637
by: emma middlebrook | last post by:
Hi I discovered that if you declare a structure (and not 'new()' it) you can then separately initialize its members and the compiler counts those separate statements as a full initialization. That struck me as a bit odd really as I would have thought it would have only bothered about 'tracking' an 'atomic'/'complete' (via 'new()') initialization not doing it 'bit by it'!!! Any comments? Seems a bit of an overhead tracking memberwise...
3
1902
by: Karl M | last post by:
Hi everyone, I just notice some strange behaviors on the MS C++ compiler regarding struct default constructor, see the example bellow: struct MyStruct { int a; }; class MyClass { public:
4
5058
by: hobbes992 | last post by:
Howdy folks, I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or deleted, however it must be initialized by a function during run-time to contain so many users which each contain so many directories of which each contain so many files. I've completed the program and have it running flawlessly without implementing...
6
3036
by: arnuld | last post by:
this one was much easier and works fine. as usual, i put code here for any further comments/views/advice: --------- PROGRAMME ------------ /* Stroustrup: 5.9 exercise 7 STATEMENTS: Define a table of the name sof months o fyear and the number of days in each month. write out that table. Do this twice:
18
9107
by: Ehud Shapira | last post by:
Is it possible to have a declaration of a struct pointer initialized to an unnamed struct? (I'm only concerned with static/global variables, if it matters.) I'm trying to do something like: struct st_a { int i, j; };
5
11659
by: ssylee | last post by:
I'm not sure if I can initialize members of a struct the lazy way. For example, let's say I have a struct defined as below: typedef struct _ABC { BOOL A; BOOL B; BOOL C; } ABC;
0
8627
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...
1
8279
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
8433
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7093
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...
0
5540
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();...
0
4127
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2568
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
1
1747
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1425
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.