initializing

Embedded C++ static initialization of struct arrays

房东的猫 提交于 2020-01-25 06:48:10
问题 While migrating to C++ I require a certain function that seems to have been deprecated. sorry, unimplemented: non-trivial designated initializers not supported What is the correct way to implement the following data storage system in C++ for memory constraint systems? typedef union union_t { float f; int i; } arg; typedef struct type_t { int a; arg b; int d; } element; const element list[] = { { .a = 1, .b = { .f = 3.141519f }, .d = 6 }, { .a = 3, .b = { .i = 1 }, } }; Often the use of std

initializing structs using user-input information

ε祈祈猫儿з 提交于 2020-01-04 04:05:21
问题 I am trying to make a program that works with poker (texas holdem) starting hands; each hand has a value from 1 to 169, and i want to be able to input each card and whether they are suited or not, and have those values correspond to a series of structs. Here is the code so far, i cant seem to get it to work (im a beginning programmer). oh and im using visual studio 2005 by the way #include "stdafx.h" #include <iostream> int main() { using namespace std; struct FirstCard { struct SecondCard {

Dynamically allocating and setting to zero an array of floats

风格不统一 提交于 2019-12-21 03:22:17
问题 How do I automatically set a dynamically allocated array of floats to zero(0.0) during allocation Is this OK float* delay_line = new float[filter_len]; //THIS memset(delay_line, 0.0, filter_len); //can I do this for a float?? //OR THIS for (int i = 0; i < filter_len; i++) delay_line[i] = 0.0; Which is the most efficient way Thanks 回答1: Use sizeof(float) * filter_len unless you are working in some odd implementation where sizeof(float) == sizeof(char) . memset(delay_line, 0, sizeof(float) *

Pointer initialization concept in C

旧街凉风 提交于 2019-12-13 08:34:59
问题 Why is this erroneous? char *p; *p='a'; The book only says -use of uninitialized pointer. Please can any one explain how that is? 回答1: char *c; //a pointer variable is being declared *c='a'; you used the dereferencing operator to access the value of the variable to which c points to but your pointer variable c is not pointing to any variable thats why you are having runtime issues. char *c; //declaration of the pointer variable char var; c=&var; //now the pointer variable c points to variable

list initialization for a matrix class

那年仲夏 提交于 2019-12-12 03:57:57
问题 I am trying to write a matrix class for linear algebra calculations. I have almost finished writing what I wanted. but I have a little trouble in creating a constructor that uses list initialization to create a matrix. this is my class data members: template <typename T> class Matx { private: // data members //rows and columns of matrix int rows, cols; //pointer to pointer to type T T** matrix; and this is my code for initialization: template <typename T> Matx<T>::Matx(T* ptM[], int m, int n)

can somebody explain how this works?

妖精的绣舞 提交于 2019-12-10 15:13:49
问题 I have this line of code. class ButtonPanel extends JPanel implements ActionListener { public ButtonPanel() { yellowButton = new JButton("Yellow"); and It works, I thought Java needs to know the type of yellowButton before creating an instance of a jButton like this? JButton yellowButton = new JButton("Yellow"); can somebody explain how this works? 回答1: If it really does work, then that means yellowButton is probably a class field that you didn't notice. Check the class again. What you

What gets called when you initialize a class without a constructor? [duplicate]

淺唱寂寞╮ 提交于 2019-12-07 06:10:42
问题 This question already has answers here : Can a class have no constructor? (6 answers) Closed 4 years ago . So when a class has a private constructor you can't initialize it, but when it doesn't have a constructor you can. So what is called when you initialize a class without a constructor? As example, what is called here (new b())?? public class a { public static void main(String args[]) { b classB = new b(); } } public class b { public void aMethod() { } } 回答1: There's no such thing as a

What gets called when you initialize a class without a constructor? [duplicate]

落花浮王杯 提交于 2019-12-05 11:40:59
This question already has answers here : Can a class have no constructor? (6 answers) Closed 4 years ago . So when a class has a private constructor you can't initialize it, but when it doesn't have a constructor you can. So what is called when you initialize a class without a constructor? As example, what is called here (new b())?? public class a { public static void main(String args[]) { b classB = new b(); } } public class b { public void aMethod() { } } Ian Roberts There's no such thing as a "class without a constructor" in Java - if there's no explicit constructor in the source code the

Java defining or initializing attributes of a class

耗尽温柔 提交于 2019-12-03 11:10:47
问题 Is there a difference between defining class attributes and initializing them? Are there cases where you want to do one over the other? Example: The following code snippets should point out the difference that I mean. I'm using a primitive and an object there: import Java.util.Random; public class Something extends Activity { int integer; Random random = null; Something(){ integer = 0; random = new Random(); .... vs. import Java.util.Random; public class Something extends Activity { int

Dynamically allocating and setting to zero an array of floats

落花浮王杯 提交于 2019-12-03 10:57:22
How do I automatically set a dynamically allocated array of floats to zero(0.0) during allocation Is this OK float* delay_line = new float[filter_len]; //THIS memset(delay_line, 0.0, filter_len); //can I do this for a float?? //OR THIS for (int i = 0; i < filter_len; i++) delay_line[i] = 0.0; Which is the most efficient way Thanks Thomas L Holaday Use sizeof(float) * filter_len unless you are working in some odd implementation where sizeof(float) == sizeof(char) . memset(delay_line, 0, sizeof(float) * filter_len); Edit: As Stephan202 points out in the comments, 0.0 is a particularly easy