declaration

Definition of def, cdef and cpdef in cython

非 Y 不嫁゛ 提交于 2019-12-03 05:54:34
问题 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. 回答1: def declares a function in Python. Since Cython is based on C runtime, it allows you to use

Is there a difference between int *x and int* x in C++? [duplicate]

十年热恋 提交于 2019-12-03 05:53:22
This question already has an answer here : Difference between char* var; and char *var;? [duplicate] (1 answer) I'm getting back into my C++ studies, and really trying to understand the basics. Pointers have always given me trouble, and I want to make sure I really get it before I carry on and get confused down the road. Sadly, I've been stymied at the very outset by an inconsistency in the tutorials I'm reading. Some do pointer declarations this way: int *x and some do it this way: int* x Now the former of the two seems to be by far the most common. Which is upsetting, because the second

C++ global variable initialization order

淺唱寂寞╮ 提交于 2019-12-03 05:33:57
问题 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

Why did C++ never allow functions to be used before they're declared? [closed]

感情迁移 提交于 2019-12-03 05:28:30
Closed . This question is opinion-based. It is not currently accepting answers. Learn more . Want to improve this question? Update the question so it can be answered with facts and citations by editing this post . OK, I know this looks like a duplicate of Why do functions need to be declared before they are used? but it doesn't seem like existing answers fully address all the details. I know that C++ was originally designed in the 80's so it could be translated in a single pass, because computers were slow. OK. But the most recent standard was published in 2011, so I don't see why C++

Why doesn't C# let you declare multiple variables using var?

走远了吗. 提交于 2019-12-03 05:13:54
Given the following: // not a problem int i = 2, j = 3; so it surprises me that this: // compiler error: Implicitly-typed local variables cannot have multiple declarators var i = 2, j = 3; doesn't compile. Maybe there is something I don't understand about this (which is why I'm asking this)? But why wouldn't the compiler realize that I meant: var i = 2; var j = 3; which WOULD compile. James Gaunt It's just another point of possible confusion for the programmer and the compiler. For example this is fine: double i = 2, j = 3.4; but what does this mean? var i = 2, j = 3.4; With syntactic sugar

Declare an array of objects using a for loop c++

♀尐吖头ヾ 提交于 2019-12-03 05:11:57
Okay. So I have declared an array of objects, and I have manually defined them using this code: Object* objects[] = { new Object(/*constructor parameters*/), new Object(/*constructor parameters*/) }; Is there anyway to use some kind of a loop (preferably a for loop) to declare these? Something like: Object* objects[] = { for(int i=0; i<20; /*number of objects*/ i++) { new Object(/*constructor parameters*/); } }; But with proper syntax? I strongly suggest using a standard library container instead of arrays and pointers: #include <vector> std::vector<Object> objects; // ... void inside_some

Template class pointer c++ declaration

假如想象 提交于 2019-12-03 03:51:35
问题 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. 回答1: Templating resolves types at compile-time. When you assign the new

Equivalent C declarations

有些话、适合烂在心里 提交于 2019-12-03 03:48:33
Are int (*x)[10]; and int x[10]; equivalent? According to the "Clockwise Spiral" rule, they parse to different C declarations. For the click-weary: The ``Clockwise/Spiral Rule'' By David Anderson There is a technique known as the ``Clockwise/Spiral Rule'' which enables any C programmer to parse in their head any C declaration! There are three simple steps to follow: 1. Starting with the unknown element, move in a spiral/clockwise direction; when ecountering the following elements replace them with the corresponding english statements: [X] or [] => Array X size of... or Array undefined size of.

Forward declare typedef within C++ class

我怕爱的太早我们不能终老 提交于 2019-12-03 02:08:08
What's the best solution to forward declare a typedef within a class. Here's an example of what I need to solve: class A; class B; class A { typedef boost::shared_ptr<A> Ptr; B::Ptr foo(); }; class B { typedef boost::shared_ptr<B> Ptr; A::Ptr bar(); }; I suppose I could just do the following: boost::shared_ptr<B> foo(); But is there a more elegant solution? There is no such thing as forward declaring a typedef unfortunately. However, there's a trick using late template instantiation: template <typename T> class BImpl; template <typename T> class AImpl { public: typedef boost::shared_ptr<AImpl>

Function parameter types and =>

大城市里の小女人 提交于 2019-12-03 01:43:13
问题 What exactly that declaration of method parameter means: def myFunc(param: => Int) = param What is meaning of => in upper definition? 回答1: 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