declaration

Where to declare/define class scope constants in C++?

我只是一个虾纸丫 提交于 2019-12-02 14:49:29
I'm curious about the benefits/detriments of different constant declaration and definition options in C++. For the longest time, I've just been declaring them at the top of the header file before the class definition: //.h const int MyConst = 10; const string MyStrConst = "String"; class MyClass { ... }; While this pollutes the global namespace (which I know is a bad thing, but have never found a laundry list of reasons why it is bad), the constants will still be scoped to individual translation units, so files that don't include this header won't have access to these constants. But you can

What's the purpose of this [1] at the end of struct declaration?

北慕城南 提交于 2019-12-02 14:47:14
I was snooping through my MSP430 microcontroller's header files, and I ran into this in <setjmp.h> : /* r3 does not have to be saved */ typedef struct { uint32_t __j_pc; /* return address */ uint32_t __j_sp; /* r1 stack pointer */ uint32_t __j_sr; /* r2 status register */ uint32_t __j_r4; uint32_t __j_r5; uint32_t __j_r6; uint32_t __j_r7; uint32_t __j_r8; uint32_t __j_r9; uint32_t __j_r10; uint32_t __j_r11; } jmp_buf[1]; /* size = 20 bytes */ I understand that it declares an anonymous struct and typedef's it to jmp_buf , but I can't figure out what the [1] is for. I know it declares jmp_buf to

C double character pointer declaration and initialization

狂风中的少年 提交于 2019-12-02 14:42:17
I always though that declaring char *c = "line"; was the same as char c[] = "line"; and so I did char **choices = { "New Game", "Continue Game", "Exit" }; Which gives me an incompatible pointer type, where char *choices[] = { "New Game", "Continue Game", "Exit" }; doesn't. Any help on understanding this? Well, they're not the same. It's just easier for most people to think of them as being the same so everyone starts to think that way until they run into a problem like the above :-) I was going to write something long and winded, but then I figured... Someone else must have done this already.

Declaring UI Objects in Switch Cases

梦想的初衷 提交于 2019-12-02 13:53:43
问题 I've read about the scope of switch cases, being jump labels and all, but the suggested solutions here at SO seem to imply that adding curly braces would circumvent the issue. However, this still doesn't seem to work: switch (objectType) { case label: //label is an integer constant NSLog(@"statement before declaration"); UILabel *control = [[UILabel alloc] init]; //no error break; case button: //button is an integer constant { UIButton *control = [[UIButton alloc] init]; //no error } break;

Why a structure is allowed to have “pointer to its own type” as member but not “(an array of the) structure type” itself?

孤者浪人 提交于 2019-12-02 13:50:06
问题 when i try to declare the following function typedef struct TRIE_NODE { char* word; struct TRIE_NODE node[26]; }TRIE_NODE; I get the following error: definition of 'struct TRIE_NODE' is not complete until the closing '}' However, if i declare this function with a pointer to the 26 nodes, it compiles just fine. typedef struct TRIE_NODE { char* word; struct TRIE_NODE* node[26]; }TRIE_NODE; I imagine that, since this is not an instance, it's impossible for me to get a pointer to the first of

Declaring a function that return a 2D array in a header file?

自作多情 提交于 2019-12-02 13:32:07
问题 I am trying to declare, within my header file, a function that returns a 2D array. How can this be accomplished, given that we already know the size of the array? Below is what I'm currently doing. class Sample { public: char[x][y] getArr(); void blah(int x, int y); private: const static int x = 8; const static int y = 2; char arr[x][y]; }; 回答1: It turns-out my original answer was totally incorrect, but I can't delete it since it's been accepted. From two separate answers below, I was able to

How declaration of variables behave?

 ̄綄美尐妖づ 提交于 2019-12-02 11:11:50
#include<stdio.h> #include<conio.h> int main(){ char i; int c; scanf("%i",&c); scanf("%c",&i);// catch the new line or character introduced before x number printf("%i",i);// value of that character getch(); return(0); } The program will behave in the same way with the next variable declarations instead of the above variable declaration: this: int c; int *x; int i; or this: int *x; int c; int i; And only this way: c variable and a x pointer before the i variable. I know that those last declarations haven't sense, the int i instead of char i , and an added pointer that isn't even needed. But

How does linkage and name mangling work?

天涯浪子 提交于 2019-12-02 10:05:26
Lets take this code sample //header struct A { }; struct B { }; struct C { }; extern C c; //code A myfunc(B&b){ A a; return a; } void myfunc(B&b, C&c){} C c; Lets do this line by line starting from the code section. When the compiler sees the first myfunc method it does not care about A or B because its use is internal. Each c++ file will know what it takes in, what it returns. Although there needs to be a name for each of the two overload so how is that chosen and how does the linker know which means what? Next is C c; I once had a bug were the linker wouldnt reconize thus allow me access to

C - gcc: no compiler warning with different function-declaration/implementation

有些话、适合烂在心里 提交于 2019-12-02 09:47:33
问题 I try to figure out why my c-compiler gives me no warning/error with following (simplified) code. The function-declaration have no parameters while the function-implementation have parameters: some.h: void foo(); some.c: static uint32_t count = 0; void foo(uint32_t num) { count += num; print("Count: %u"); } main.c: foo(100); foo(); Output: Count: 100 Count: 100 Compiler for target build: gcc-arm-none-eabi-4_9-2015q1-20150306-win32 Linker for target build: gcc-arm-none-eabi-4_9-2015q1-20150306

how to declare a vector of thread

久未见 提交于 2019-12-02 08:45:10
i'm new in c++ programing and need some help to use a thread library with vector library... first I follow this tutorial but the compiler (visual studio 2013) show me errors and I don't know how correct it: first declaration of function void Fractal::calcIterThread(vector<vector<iterc>> &matriz, int desdePos, int hastaPos, int idThread){ ... } in main loop vector<vector<iterc>> res; res.resize(altoPantalla); for (int i = 0; i < altoPantalla; i++){ res[i].resize(anchoPantalla); } int numThreads = 10; vector<thread> workers(numThreads); for (int i = 0; i < numThreads; i++){ //here diferent try