在 Java 中,根据声明的数据成员和方法在类文件中列出的顺序来组织类,通常最受接受的方式是什么,请记住以下内容以及您可以为每个类想到的任何其他内容:
我想根据计算初始化一个数组,但是当我尝试这样做时编译器给我一个错误(我使用的是 GCC 版本 6.3.0):
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;
char textgrid[SCREEN_HEIGHT/16][SCREEN_WIDTH/16];
Run Code Online (Sandbox Code Playgroud)
编译器错误如下:
error: variably modified 'textgrid' at file scope
Run Code Online (Sandbox Code Playgroud)
有没有办法在文件范围内执行此操作?
看来我不能使用计算作为 #define 语句的一部分来完成此操作,因为以下给出了相同的错误:
#define TEXTGRID_WIDTH (SCREEN_WIDTH / 16)
#define TEXTGRID_HEIGHT (SCREEN_HEIGHT / 16)
char textgrid[TEXTGRID_HEIGHT][TEXTGRID_WIDTH];
Run Code Online (Sandbox Code Playgroud) 我对这两个符号表示什么感到非常困惑。我知道 的优先级()大于[],这是否意味着它char(*name)[10]是一个指针并且char *name[10]是一个数组?
声明指向 void 函数的指针的正确方法是什么?
void reverseArray(int arraySize, int a[]){
}
Run Code Online (Sandbox Code Playgroud)
我认为有些正确的许多尝试之一
void (*reversePtr)(int, int) = &reverseArray;
Run Code Online (Sandbox Code Playgroud) 我想做以下事情:
const char errorMsg [64] ( useApple ? "Error Msg Apple\n" : "Error Msg Bee\n" );
MyMethod ( errorMsg );
Run Code Online (Sandbox Code Playgroud)
对于带有签名的方法:
MyMethod(const char* errorMessageInput );
Run Code Online (Sandbox Code Playgroud)
我有一个使用 const char* 的方法,我想在发送它之前创建一个局部变量。我无法分配动态内存,但我可以使用比需要更大的数组(在这种情况下,我将其设为 64)。我将如何编译此代码?
package P is
type T is private;
private
package NP is
type T is null record;
end NP;
end P;
Run Code Online (Sandbox Code Playgroud)
可以使用 NP.T 作为 PT 的完整声明吗?
我知道 JS 中的 let 不能声明变量两次。但是当我在控制台中尝试以下代码时:
a = 1;
let a = 2;
let a = 3;
.
.
.
let a = 100;
Run Code Online (Sandbox Code Playgroud)
注意:它们是逐行运行的(如下面的屏幕截图所示)。版本是谷歌浏览器91.0.4472.114
它总是有效,没有错误。这真的让我很困惑,为什么它工作得很好?我知道控制台中发生的情况并不代表脚本中发生的情况。但我的问题是为什么它存在于控制台中?这有什么原因吗,或者可能是一个错误?
因为我假设let和const具有相同的声明行为,如果我使用const而不是let,这是毫无疑问的。
b = 1;
const b = 2;
const b = 3; //Uncaught SyntaxError: Identifier 'b' has already been declared
Run Code Online (Sandbox Code Playgroud)
代码1:
#include<iostream>
class Singleton
{
private:
static Singleton instance; //declaration of static variable
public:
static Singleton &GetInstance()
{
return instance;
}
void Hello()
{
std::cout << "Hello!";
}
};
Singleton Singleton::instance; //definition of static variable
int main()
{
Singleton::GetInstance().Hello();
}
Run Code Online (Sandbox Code Playgroud)
代码2:
#include <iostream>
class Singleton
{
public:
static Singleton &GetInstance()
{
static Singleton instance; //declaration of static variable
return instance;
}
void Hello()
{
std::cout << "Hello!";
}
};
int main()
{
Singleton::GetInstance().Hello();
}
Run Code Online (Sandbox Code Playgroud)
在代码1中,我们需要定义静态变量,但在代码2中,我们只是在函数Singleton::GetInstance&()中声明了静态变量,然后将其返回。代码 2 中的声明和定义是否出现在同一行?为什么?
假设我想声明一个字符串供整个班级使用,但该字符串有一部分可能会在以后更改。是否可以只声明一次字符串,然后“刷新”它?
例子:
substring = ""
my_long_string = f"This is a long string using another {substring} in it."
def printMyString(newString):
substring = newString
print(my_long_string)
printMyString(newString="newer, better substring")
Run Code Online (Sandbox Code Playgroud)
我可以调整此代码,以便最终打印出来This is a long string using another newer, better substring in it.,而无需稍后再次声明长字符串吗?
我唯一的猜测是这样的:
substring = ""
def regenerate_string(substring):
return f"This is a long string using another {substring} in it."
def printMyString(newString):
print(regenerate_string(newString))
printMyString(newString="newer, better substring")
Run Code Online (Sandbox Code Playgroud)
但我想问一下 Python 中是否可能有一个内置函数来做到这一点?
auto queue = [](string str) {
istringstream ss(str);
//std::copy(std::istream_iterator<string>(ss),
// std::istream_iterator<string>(),
// std::ostream_iterator<string>(std::cout, " "));
//deque<string> q(std::istream_iterator<string>(ss), std::istream_iterator<string>{});
deque<string> q(std::istream_iterator<string>(ss), std::istream_iterator<string>());
return q;
};
Run Code Online (Sandbox Code Playgroud)
为什么编译器会抱怨
括号作为函数声明已消除歧义 [-Wveshing-parse]
如果我用 构建一个容器istream_iterator<string>()。
和容器构造函数中的参数有什么区别吗std::copy?