struct

How to initialize an array of struct in C++?

倾然丶 夕夏残阳落幕 提交于 2019-12-28 04:00:49
问题 I have the following struct in my C++ code (I am using Visual Studio 2010): struct mydata { string scientist; double value; }; What I would like to do is to be able to initialize them in a quick way, similar to array initialization in C99 or class initialization in C#, something á la : mydata data[] = { { scientist = "Archimedes", value = 2.12 }, { scientist = "Vitruvius", value = 4.49 } } ; If this is not possible in C++ for an array of structs, can I do it for an array of objects? In other

Access struct members as if they are a single array?

风流意气都作罢 提交于 2019-12-28 02:11:07
问题 I have two structures, with values that should compute a pondered average, like this simplified version: typedef struct { int v_move, v_read, v_suck, v_flush, v_nop, v_call; } values; typedef struct { int qtt_move, qtt_read, qtt_suck, qtd_flush, qtd_nop, qtt_call; } quantities; And then I use them to calculate: average = v_move*qtt_move + v_read*qtt_read + v_suck*qtt_suck + v_flush*qtd_flush + v_nop*qtd_nop + v_call*qtt_call; Every now and them I need to include another variable. Now, for

Mixing class and struct

耗尽温柔 提交于 2019-12-28 01:20:10
问题 I'm well aware of the difference between class and struct, however I'm struggling to authoritatively say if this is well defined: // declare foo (struct) struct foo; // define foo (class) class foo { }; // instance of foo, claiming to be a struct again! Well defined? struct foo bar; // mixing class and struct like this upsets at least one compiler (names are mangled differently) const foo& test() { return bar; } int main() { test(); return 0; } If this is undefined behaviour can someone point

How to set and get fields in struct's method

北城以北 提交于 2019-12-27 17:03:52
问题 After creating a struct like this: type Foo struct { name string } func (f Foo) SetName(name string){ f.name=name } func (f Foo) GetName string (){ return f.name } How do I create a new instance of Foo and set and get the name? I tried the following: p:=new(Foo) p.SetName("Abc") name:=p.GetName() fmt.Println(name) Nothing gets printed, because name is empty. So how do I set and get a field inside a struct? 回答1: Commentary (and working) example: package main import "fmt" type Foo struct { name

How to set and get fields in struct's method

折月煮酒 提交于 2019-12-27 17:03:28
问题 After creating a struct like this: type Foo struct { name string } func (f Foo) SetName(name string){ f.name=name } func (f Foo) GetName string (){ return f.name } How do I create a new instance of Foo and set and get the name? I tried the following: p:=new(Foo) p.SetName("Abc") name:=p.GetName() fmt.Println(name) Nothing gets printed, because name is empty. So how do I set and get a field inside a struct? 回答1: Commentary (and working) example: package main import "fmt" type Foo struct { name

Go embedded struct call child method instead parent method

自作多情 提交于 2019-12-27 12:06:29
问题 Here a sample of Go code with an Interface, a Parent Struct and 2 Children Structs package main import ( "fmt" "math" ) // Shape Interface : defines methods type ShapeInterface interface { Area() float64 GetName() string PrintArea() } // Shape Struct : standard shape with an area equal to 0.0 type Shape struct { name string } func (s *Shape) Area() float64 { return 0.0 } func (s *Shape) GetName() string { return s.name } func (s *Shape) PrintArea() { fmt.Printf("%s : Area %v\r\n", s.name, s

Opaque C structs: how should they be declared?

吃可爱长大的小学妹 提交于 2019-12-27 11:42:26
问题 I've seen both of the following two styles of declaring opaque types in C APIs. Is there any clear advantage to using one style over the other? Option 1 // foo.h typedef struct foo * fooRef; void doStuff(fooRef f); // foo.c struct foo { int x; int y; }; Option 2 // foo.h typedef struct _foo foo; void doStuff(foo *f); // foo.c struct _foo { int x; int y; }; 回答1: My vote is for the third option that mouviciel posted then deleted: I have seen a third way: // foo.h struct foo; void doStuff(struct

C# Marshalling a C++ struct with wchar_t* member occasionally leaves the heap corrupted

一世执手 提交于 2019-12-26 06:04:22
问题 I have declared a struct as follows: // C++ struct TestStruct { wchar_t* TestString; }; and the corresponding managed representation // C# [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct TestStruct { [MarshalAs(UnmanagedType.LPWStr)] public string TestString; } As well as this function: // C++ __declspec(dllexport) void __stdcall FillMultipleStructs(TestStruct* testStructures, const short arrayLength) { for(int i = 0; i < arrayLength; i++) { const wchar_t

C# Marshalling a C++ struct with wchar_t* member occasionally leaves the heap corrupted

六月ゝ 毕业季﹏ 提交于 2019-12-26 06:04:01
问题 I have declared a struct as follows: // C++ struct TestStruct { wchar_t* TestString; }; and the corresponding managed representation // C# [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct TestStruct { [MarshalAs(UnmanagedType.LPWStr)] public string TestString; } As well as this function: // C++ __declspec(dllexport) void __stdcall FillMultipleStructs(TestStruct* testStructures, const short arrayLength) { for(int i = 0; i < arrayLength; i++) { const wchar_t

C# Marshalling a C++ struct with wchar_t* member occasionally leaves the heap corrupted

落爺英雄遲暮 提交于 2019-12-26 06:02:07
问题 I have declared a struct as follows: // C++ struct TestStruct { wchar_t* TestString; }; and the corresponding managed representation // C# [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct TestStruct { [MarshalAs(UnmanagedType.LPWStr)] public string TestString; } As well as this function: // C++ __declspec(dllexport) void __stdcall FillMultipleStructs(TestStruct* testStructures, const short arrayLength) { for(int i = 0; i < arrayLength; i++) { const wchar_t