struct

What's the difference between struct and class in .Net?

I'm looking for a clear, concise and accurate answer. Ideally as the actual answer, although links to good explanations welcome. ...

C-like structures in Python

Is there a way to conveniently define a C-like structure in Python? I'm tired of writing stuff like: class MyStruct(): def __init__(self, field1, field2, field3) self.field1 = field1 self.field2 = field2 self.field3 = field3 (Interesting, it appears I can't type underscores following non-whitespace in pre-f...

Struct like objects in Java

Is it completely against the Java way to create struct like objects? class SomeData1 { public int x; public int y; } I can see a class with accessors and mutators being more Java like. class SomeData2 { int getX(); void setX(int x); int getY(); void setY(int y); private int x; private int y; } The ...

Changing the value of an element in a list of structs

I have a list of structs and I want to change one element. For example : MyList.Add(new MyStruct("john"); MyList.Add(new MyStruct("peter"); Now I want to change one element: MyList[1].Name = "bob" However, whenever I try and do this I get the following error: Cannot modify the return value of System.Collections.Generic.List.this[i...

When should you use a class vs a struct in C++?

In what scenarios is it better to use a struct vs a class in C++? ...

Is it safe for structs to implement interfaces?

I seem to remember reading something about how it is bad for structs to implement interfaces in CLR via C#, but I can't seem to find anything about it. Is it bad? Are there unintended consequences of doing so? public interface Foo { Bar GetBar(); } public struct Fubar : Foo { public Bar GetBar() { return new Bar(); } } ...

Can you have a Class in a Struct

Is it possible in C# to have a Struct with a member variable which is a Class type? If so, where does the information get stored, on the Stack, the Heap, or both? ...

What are the differences between struct and class in C++

This question was already asked in the context of C#/.Net. Now I'd like to learn the differences between a struct and a class in (unmanaged) C++. Please discuss the technical differences as well as reasons for choosing one or the other in OO design. I'll start with an obvious difference: If you don't specify public: or private:, memb...

Why doesn't GCC optimize structs?

Systems demand that certain primitives be aligned to certain points within the memory (ints to bits that are multiples of 4, shorts to bits that are multiples of 2, etc.). Of course, these can be optimized to waste the least space in padding. my question is why doesn't GCC do this automatically? Is the more obvious heuristic (order va...

How do I implement IEqualityComparer on an immutable generic Pair struct?

Currently I have this (edited after reading advice): struct Pair<T, K> : IEqualityComparer<Pair<T, K>> { readonly private T _first; readonly private K _second; public Pair(T first, K second) { _first = first; _second = second; } public T First { get { return _first; } } public K Second { ge...

How do you compare structs for equality in C?

How do you compare two instances of structs for equality in standard C? ...

How do I find the size of a struct?

struct a { char *c; char b; }; What is sizeof(a)? ...

Does ANSI C support signed / unsigned bit fields?

Does is make sense to qualify bit fields as signed / unsigned? ...

Why does a bit field of type "unsigned short" pack into a struct differently to an "unsigned int:16"

I have a C++ program representing a TCP header as a struct: #include "stdafx.h" /* TCP HEADER 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Source Port ...

Why is there no RAII in .NET?

Being primarily a C++ developer the absence of RAII (Resource Acquisition Is Initialization) in Java and .NET has always bothered me. The fact that the onus of cleaning up is moved from the class writer to its consumer (by means of try finally or .NET's using construct) seems to be markedly inferior. I see why in Java there is no suppor...

Dereferencing Variable Size Arrays in Structs

Structs seem like a useful way to parse a binary blob of data (ie a file or network packet). This is fine and dandy until you have variable size arrays in the blob. For instance: struct nodeheader{ int flags; int data_size; char data[]; }; This allows me to find the last data character: nodeheader b; cout <<...

Marshal C++ struct array into C#

I have the following struct in C++: #define MAXCHARS 15 typedef struct { char data[MAXCHARS]; int prob[MAXCHARS]; } LPRData; And a function that I'm p/invoking into to get an array of 3 of these structures: void GetData(LPRData *data); In C++ I would just do something like this: LPRData *Results; Results = (LPRData *)mal...

N-ary trees in C

Which would be a neat implemenation of a N-ary tree in C language? Particulary, I want to implement an n-ary tree, not self-ballancing, with an unbound number of children in each node, in which each node holds an already defined struct, like this for example: struct task { char command[MAX_LENGTH]; int required_time; }; ...

[C] How can I initialize an array of pointers to structs?

Is it possible to initialize an array of pointers to structs? Something like: struct country_t *countries[] = { {"United States of America", "America"}, {"England", "Europe"}, {"Ethiopia", "Africa"} } I want to do that in order to get the entities in not-contiguous memory, and the pointers to them in contiguous mem...

C# : How does this work : Unit myUnit = 5;

I just noticed that you can do this in C#: Unit myUnit = 5; instead of having to do this: Unit myUnit = new Unit(5); Does anyone know how I can achieve this with my own structs? I had a look at the Unit struct with reflector and noticed the TypeConverter attribute was being used, but after I created a custom TypeConverter for my st...