我正在学习 C++,并了解到 int 类型只是预制类。所以我想也许我应该尝试创建一个。
我想做的基本上是一个普通的 int 类
int x;
x=7;
cout << x;
Run Code Online (Sandbox Code Playgroud)
// 屏幕上的输出为 7。
如此相似...
abc x;
x=7;
cout << x;
Run Code Online (Sandbox Code Playgroud)
我会放什么
class abc{
\\ HERE!!!!!!
};
Run Code Online (Sandbox Code Playgroud)
所以我可以这样做
class SomeClass {
public:
int x;
SomeClass(int x) {
this->x = x;
}
};
int main(int argc, char *argv[]) {
SomeClass s = 5;
cout << s.x << "\n"; // 5
s = 17;
cout << s.x << "\n"; // 17
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是正如您所看到的,我必须使用 sx 来打印值 - 我只想使用 's'。我是在做一个实验,我不想听到这个方法是好是坏,毫无意义或革命性,或者无法完成。我记得我做过一次。但只能通过复制和粘贴我不完全理解甚至忘记的代码。
我需要一些有关 Cuda GLOBAL 内存的帮助。在我的项目中,我必须声明全局数组以避免在每次内核调用时发送这个数组。
编辑:
我的应用程序可以调用内核超过 1,000 次,每次调用时我都会向他发送一个大小超过 [1000 X 1000] 的数组,所以我认为这需要更多时间,这就是我的应用程序运行缓慢的原因。所以我需要为 GPU 声明全局数组,所以我的问题是
1如何声明全局数组
2如何在内核调用之前从 CPU 初始化全局数组
提前致谢
我可以以某种方式将数组值声明为字符串、int 等而不向其添加值吗?
例如:
public $arr = array(string);
Run Code Online (Sandbox Code Playgroud) 我来自 java,在那里我们可以做这样的事情:
动作.java:
public interface Action {
public void performAction();
}
Run Code Online (Sandbox Code Playgroud)
主类.java:
public class MainClass {
public static void main(String[] args) { //program entry point
Action action = new Action() {
public void performAction() {
// custom implementation of the performAction method
}
};
action.performAction(); //will execute the implemented method
}
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我没有创建一个实现 的类Action,而是直接在声明时实现了接口。
这样的事情甚至可以用 PHP 实现吗?
我试过的:
动作.php:
<?php
interface Action {
public function performAction();
}
?>
Run Code Online (Sandbox Code Playgroud)
myactions.php:
include "action.php";
$action = new Action() {
public function …Run Code Online (Sandbox Code Playgroud) 我看到一个令我困惑的声明。(这里的语法)
static var dateFormatter: NSDateFormatter = {
var formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
return formatter
}()
Run Code Online (Sandbox Code Playgroud)
要声明一个变量,它看起来像是使用一个函数(初始化程序)来创建一个。由于我不熟悉 Swift 中的闭包,我找到了一些有用的教程。但是,上面的示例似乎不适合其中任何一个。参考:http : //fuckingclosuresyntax.com/ 任何建议、参考或教程将不胜感激。
我开始学习Generics了Java,我看几个教程,但我有点迷茫,不知道如何泛型方法声明。
当我使用泛型类型时,定义方法的正确顺序是什么?我找到了这个示例,什么时候需要使用尖括号,什么时候不需要?
public class Box<A> {
private A a;
...
public void setA(A a) {
this.a = a;
}
public <A> List<A> transform(List<A> in) {
return null;
}
public static <A> A getFirstElement(List<A> list) {
return null;
}
public A getA() {
return a;
}
Run Code Online (Sandbox Code Playgroud) 有人可以帮我弄清楚这个声明吗?
int *x()(int)
void (*signal(int, void (*fp)(int)))(int)
Run Code Online (Sandbox Code Playgroud)
我似乎无法使用此处给出的方法解析这些。我知道有什么用这种手段cdecl.org并且也是第一个1是非法的,但我想弄清楚如何分析这些?
我正在尝试数组指针和函数指针的一些随机声明。我被打中了。
#include<bits/stdc++.h>
int add(int a,int b){
return 1729; }
int main(){
int (abc)(int,int);
abc = add;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
发出的错误是:
#include<bits/stdc++.h>
int add(int a,int b){
return 1729; }
int main(){
int (abc)(int,int);
abc = add;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但随着这两个abc和add是同一类型的,它为什么要转变?还是可以将函数分配给新变量?如果是这样,该怎么做?
关于C(以及 C++ 的 C 子集)中指针和数组声明的细节,SO 上有很多问题。
我对为什么更感兴趣。当我们连续声明多个指针/数组时,
为什么必须将*,[]放在每个变量的前面?
int *a, *b;
int c[1], d[1];
Run Code Online (Sandbox Code Playgroud)
为什么我们必须在函数指针中的变量名之后/周围键入内容?
void (*foo_ptr)(int, int);
Run Code Online (Sandbox Code Playgroud)
为什么我们有这个让很多新手感到困惑的功能,甚至编译器也会识别并报告这些东西作为类型的一部分?前任:function foo accepts int** but it was given int*
我想我正在寻找它背后的直觉,导致它以这种方式被创建,以便我可以将它应用于我对语言的理解。现在我只是看不到它......
我是 Haskell 的新手,我在创建和返回变量方面有一点问题。
实际上我的问题是在函数范围内创建一个变量并返回它。现在我在函数声明中声明了一个空变量,函数将返回它。
例如我想返回 nb 的平方根 i。因为我不知道如何在作用域中创建变量i,所以我用好的nb 和未来的平方根i 调用mySqr 为空。
mySqr :: Int -> Int -> Int
mySqr nb i
| i * i == nb = i
| otherwise = mySqr nb (i + 1)
Run Code Online (Sandbox Code Playgroud)
调用:mySqr 25 0
它有效,但我希望它更好。
谢谢你的帮助。