我知道可以在多个文件中进行类实现(是的,我知道这是一个坏主意),但我想知道是否可以在单独的文件中编写类定义而不会出现重新定义错误(也许有一些技巧,要不然...)
有没有办法在该类之外声明新的静态变量,即使它没有在类中设置?
// Using this class as a static object.
Class someclass {
// There is no definition for static variables.
}
// This can be initialized
Class classA {
public function __construct() {
// Some codes goes here
}
}
/* Declaration */
// Notice that there is no static declaration for $classA in someclass
$class = 'classA'
someclass::$$class = new $class();
Run Code Online (Sandbox Code Playgroud)
如何做呢?
谢谢您的建议。
我是 Swift 语言的新手,试图在编码时养成健康的编程习惯。
在声明时显式声明变量类型是最佳实践吗?例如:
var str:String = "likeThis"
Run Code Online (Sandbox Code Playgroud)
或者这是可以接受的:
var str= "likeThis"
Run Code Online (Sandbox Code Playgroud) 以下程序是否符合c ++标准?
namespace X { class A; }
namespace Y { using X::A; class A {}; }
int main() {}
Run Code Online (Sandbox Code Playgroud)
我用不同的编译器得到了不同的结果:
我没有在我的程序违反的c ++标准中找到任何规则.
如果程序格式正确,为什么visual studio会出错?
如果程序格式不正确,那么c ++标准中的规则是否会违反,为什么gcc不会出错?
我不是想让我的程序编译.我只是想根据c ++标准找出它是否格式正确,以及为什么我测试的两个编译器表现不同.
我想知道,为什么不允许这样写:
struct foo {
void bar(); // declaration
void bar(){std::cout << "moo" << std::endl;} // declaration + definition
};
Run Code Online (Sandbox Code Playgroud)
该函数被声明两次(我认为这是可以的)并定义一次.但是,我的编译器抱怨:
decldef.cxx:7:10: error: 'void foo::bar()' cannot be overloaded
Run Code Online (Sandbox Code Playgroud)
为什么不允许?
为什么我的编译器(g ++ 4.7.2)将此解释为重载?
PS:我知道怎么写"正确的方式",但我只是想知道,为什么上面的错误.
#include<stdio.h>
void main()
{
int a = 4;
switch (a)
{
case 4:
int res = 1;
printf("%d",res);
break;
}
}
Run Code Online (Sandbox Code Playgroud)
当我用 gcc 编译这段代码时,我得到了错误
root@ubuntu:/home/ubuntu# gcc test.c -o t
test.c: In function ‘main’:
test.c:9:4: error: a label can only be part of a statement and a declaration is not a statement
int res = 1;
Run Code Online (Sandbox Code Playgroud)
但是当我添加时;,case 4:;我可以编译我的代码。
有什么问题,为什么要;解决这个问题?
int[] array = new int[]{1,2,3};
Run Code Online (Sandbox Code Playgroud)
int[] array = {1,2,3};
Run Code Online (Sandbox Code Playgroud)
int[] array;
array = new int[]{1,2,3};
Run Code Online (Sandbox Code Playgroud)
int[] array;
array = {1,2,3};
Run Code Online (Sandbox Code Playgroud)
有人可以解释为什么最后一个是错误的以及我们可以做 #2 的原因吗?
在 C 中,您可以声明一个指向数组的变量,如下所示:
int int_arr[4] = {1,2,3,4};
int (*ptr_to_arr)[4] = &int_arr;
Run Code Online (Sandbox Code Playgroud)
尽管实际上它与声明一个指向 int 的指针相同:
int *ptr_to_arr2 = int_arr;
Run Code Online (Sandbox Code Playgroud)
但从语法上来说,它是不同的。
现在,一个返回这样一个指向数组(例如 int )的指针的函数会是什么样子?
为什么这段代码:
\nextern void x;\nRun Code Online (Sandbox Code Playgroud)\n导致:
\n$ cl t555.c /std:c11 /Za\nt555.c(1): error C2182: 'x': illegal use of type 'void'\nRun Code Online (Sandbox Code Playgroud)\n这里什么是非法的?
\nUPD。使用案例:
\n$ cat t555a.c t555.p.S\n#include <stdio.h>\n\nextern void x;\n\nint main(void)\n{\n printf("%p\\n", &x);\n return 0;\n}\n\n .globl x\nx:\n .space 4\n\n$ gcc t555a.c -std=c11 -pedantic -Wall -Wextra -c && as t555.p.S -o t555.p.o && gcc t555a.o t555.p.o && ./a.exe\nt555a.c: In function \xe2\x80\x98main\xe2\x80\x99:\nt555a.c:7:20: warning: taking address of expression of type \xe2\x80\x98void\xe2\x80\x99\n 7 | printf("%p\\n", &x);\n | ^\n0x1004010c0\n\n$ clang t555a.c -std=c11 …Run Code Online (Sandbox Code Playgroud) 在作者 Brian W. Kernighan 和 Dennis M. Ritchie 的《C 编程语言》第二版中,他们讨论了声明符和直接声明符。讨论从本书的第 14 页开始。122 与dcl和direct-dcl的 您能以易于理解的方式解释声明符和直接声明符之间的区别吗?是什么让它变得直接?
另外,在第 225
其中直接声明符是标识符或带括号的标识符。特别是,它不能通过 typedef 实现函数类型。
在我看来,声明符是变量或函数的声明。在“TD”中,T部分指定其说明符和类型,D部分指定标识,表示变量或函数的唯一标识名称。这和语言的语法有一定的关系。
声明符是否是间接的,因为它们没有像direct-declarator中那样指定为直接的?
c syntax programming-languages declaration variable-declaration
declaration ×10
c ×4
c++ ×3
arrays ×2
syntax ×2
class ×1
definition ×1
java ×1
label ×1
oop ×1
overloading ×1
php ×1
pointers ×1
swift ×1
variables ×1
visual-c++ ×1
void ×1