declaration

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

柔情痞子 提交于 2019-12-03 01:27:03
问题 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

Comma omitted in variadic function declaration in C++

丶灬走出姿态 提交于 2019-12-03 00:57:06
I am used to declaring variadic functions like this: int f(int n, ...); When reading The C++ Programming Language I found that the declarations in the book omit the comma: int f(int n...); // the comma has been omitted It seems like this syntax is C++ specific as I get this error when I try to compile it using a C compiler: test.c:1:12: error: expected ‘;’, ‘,’ or ‘)’ before ‘...’ token int f(int n...); Is there any difference between writing int f(int n, ...) and int f(int n... )? Why was this syntax added C++? kfsone According to § 8.3.5.4 of the C++ standard ( current draft ): Where

how to declare a vector of thread

£可爱£侵袭症+ 提交于 2019-12-02 21:17:31
问题 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

namespaces, classes and free functions - when do you need fully qualified names

穿精又带淫゛_ 提交于 2019-12-02 21:12:07
In my example below, why do I have to fully qualify the name of the free function in the cpp to avoid linker errors and why does it work for the class function without? Can you explain the difference? ctest.h: namespace Test { int FreeFunction(); class CTest { public: CTest(); ~CTest(); }; } ctest.cpp: #include "ctest.h" using namespace Test; // int FreeFunction() -> undefined reference error int Test::FreeFunction() -> works just fine { return 0; } CTest::CTest() -> no need to fully qualify name, i.e. Test::CTest {} CTest::~CTest() {} Thanks for your time & help. int FreeFunction(void); is

Definition of def, cdef and cpdef in cython

社会主义新天地 提交于 2019-12-02 20:38:15
I'd like to know the difference between def , cdef and cpdef when I declare a function. The difference between def and the others it's more or less clear. And I've also seen that sometimes it's added the return type in the declaration ( cdef void/double/int... name ) and sometimes not. I'd also like to know how to declare a string variable in cython, as I didn't know it, I declared it as object. def declares a function in Python. Since Cython is based on C runtime, it allows you to use cdef and cpdef . cdef declares function in the layer of C language. As you know (or not?) in C language you

Syntax error on token(s) “product1. ”VariableDeclaratorId expected after this token

风格不统一 提交于 2019-12-02 20:15:25
问题 I met this type of error when creating a new class. Below was my code: package com.example.opener.test; import java.util.ArrayList; import Product.Product; public class ProductMgrStubs { /* Attribute */ byte[] image1 = new byte[]{6,7,5,6,5}; Product product1 = new Product("Product 1", image1, "Product 1", 0); ArrayList<Product> productList = new ArrayList<Product>(); productList.add(product1); /* The error message popped out from here */ } Could anyone help me figure out what actually

Template class pointer c++ declaration

橙三吉。 提交于 2019-12-02 18:11:49
template <typename T> class Node {...}; int main { Node* ptr; ptr = new Node<int>; } Will fail to compile I have to to declare the pointer as Node<int>* ptr; Why do I have to specify the type when declaring a pointer I haven't created the class yet, why does the compiler have to know what type it will be pointing to. And is it not possible to create a generic pointer and decide afterwards what type I want to assign it. progician Templating resolves types at compile-time. When you assign the new Node<int> object to it, the pointer must know at compile-time what type exactly it is. Node<int> and

C++ global variable initialization order

痴心易碎 提交于 2019-12-02 17:55:41
I don't understand what the following code example does and how it does it: #include <stdio.h> int f(); int a = f(); // a exists just to call f int x = 22; int f() { ++x; return 123; // unimportant arbitrary number } int main() { printf("%d\n", x); } When this is ran it prints 23 , which is the intuitive answer. However in C++, global variables are supposed to be initialized in order of definition. That would mean that a should be initialized before x , because it is defined before x . If that was the case, then the function f would have to be called before x was initialized, because the call

How does linkage and name mangling work?

邮差的信 提交于 2019-12-02 17:11:23
问题 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

Function parameter types and =>

女生的网名这么多〃 提交于 2019-12-02 15:39:58
What exactly that declaration of method parameter means: def myFunc(param: => Int) = param What is meaning of => in upper definition? Tomasz Nurkiewicz This is so-called pass-by-name . It means you are passing a function that should return Int but is mostly used to implement lazy evaluation of parameters. It is somewhat similar to: def myFunc(param: () => Int) = param Here is an example. Consider an answer function returning some Int value: def answer = { println("answer"); 40 } And two functions, one taking Int and one taking Int by-name : def eagerEval(x: Int) = { println("eager"); x; } def