因为它与Delphi有关...
当变量声明某种类型时,它是否初始化为该类型的OBJECT?或者必须为变量分配一个表达式,该表达式返回该类型的对象?
我来自强大的Java背景.我的意思是这个...在Java中,假设您声明一个名为Orange的用户定义类型的实例变量.看起来像这样:
private Orange _fruit;
Run Code Online (Sandbox Code Playgroud)
变量_fruit仍然保持对null的引用,直到实际分配了一个Orange类的实例,如下所示:
_fruit = new Orange();
Run Code Online (Sandbox Code Playgroud)
在Delphi中如果我声明一个类型为TForm的变量,如下所示:
var
Form : TForm;
Run Code Online (Sandbox Code Playgroud)
Form是否已初始化为TForm对象?或者它仍然是零?
我问,因为我在尝试编译一小段代码时遇到错误,如下所示:
这是主要单位:
unit Main;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils,
System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Second;
type
TForm1 = class(TForm)
ShowForm2: TButton;
procedure ShowForm2Click(Sender: TObject);
end;
var
Form1: TForm1;
SecondForm : TSecondForm;
implementation
{$R *.dfm}
procedure TForm1.ShowForm2Click(Sender: TObject);
begin
SecondForm.ShowModal;
end;
end.
Run Code Online (Sandbox Code Playgroud)
这是第二单元:
unit Second;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
Vcl.StdCtrls;
type
TSecondForm = class(TForm)
Label1: …Run Code Online (Sandbox Code Playgroud) 查看Java和C#等语言使用未初始化的局部变量是编译时错误.那么为什么C&C++允许未初始化的局部变量呢?这些语言允许这种情况的原因是什么?我认为,如果这两种语言迫使程序员强制初始化包括指针在内的局部变量,那么许多不好的问题就不会出现或者可以防止,这也会使语言更加安全.不是吗?
在这个switch语句中(我惊讶地编译并执行而没有错误),在case 2中没有声明变量,而case 1从不执行.这有效吗?如何在不声明的情况下使用变量?
switch(2){
case 1:
string something = "whatever";
break;
case 2:
something = "where??";
break;
}
Run Code Online (Sandbox Code Playgroud) 我对以下代码非常困惑:
class Tree {
protected:
struct Node {
Node* leftSibling;
Node* rightSibling;
int value;
};
private:
Node* root;
int value;
.....
public:
void addElement(int number) {
if (root == NULL) {
printf("This is the value of the pointer %lld\n",(long long)root);
printf("This is the value of the int %d\n",value);
...
return;
}
printf("NOT NULL\n");
}
};
int main() {
Tree curTree;
srand(time(0));
for(int i = 0;i < 40; ++i) {
curTree.addElement(rand() % 1000);
}
}
Run Code Online (Sandbox Code Playgroud)
该curTree变量是main函数的本地变量,因此我预计它不会将其成员初始化为0,但它们都已初始化.
我有以下三个类定义:
class String
{
public:
String() {}
String(const char *) {}
};
class ClassA
{
public:
ClassA(const String &) {}
};
class ClassB
{
public:
ClassB(const ClassA &, const String & = String()) {}
void method() {}
};
Run Code Online (Sandbox Code Playgroud)
现在假设我要创建一个实例ClassB:
String name("test");
ClassA item(ClassB(name));
Run Code Online (Sandbox Code Playgroud)
这不起作用:
error: request for member 'method' in 'item', which is of non-class type 'ClassA ()(ClassB)'
这个错误是什么意思?ClassA ()(ClassB)编译器一直指的是什么奇怪的类型?
我编写了一个简单的C程序,我希望它在编译时会失败但不幸的是它在C中编译并运行良好,但在C++编译时失败了.考虑以下计划:
#include <stdio.h>
int main()
{
char *c=333;
int *i=333;
long *l=333;
float *f=333;
double *d=333;
printf("c = %u, c+1 = %u",c,c+1);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
访问此链接:http://ideone.com/vnKZnx
我认为由于C++的强类型检查,这个程序肯定无法在C++中编译.为什么这个程序用C编译?事实上,编译器也会显示警告.我正在使用Orwell Dev C++ IDE(gcc 4.8.1编译器).我也在其他编译器(Borland Turbo C++ 4.5)上尝试了相同的程序,通过扩展名.c保存它,并且在这个编译器上它无法编译.
在下面的代码中:
var verticesCount: Int // to read a vertices count for graph
// Reading until we get a valid vertices count.
while (!Assertions.checkEnoughVertices(
verticesCount = consoleReader.readInt(null, Localization.getLocStr("type_int_vertices_count"))))
// The case when we don't have enough vertices.
println(String.format(Localization.getLocStr("no_enough_vertices_in_graph"),
Assertions.CONFIG_MIN_VERTICES_COUNT))
val resultGraph = Graph(verticesCount)
Run Code Online (Sandbox Code Playgroud)
我们在最后一行收到下一个错误:
Error:(31, 33) Kotlin: Variable 'verticesCount' must be initialized
Run Code Online (Sandbox Code Playgroud)
Assertions.checkEnoughVertices接受一个安全类型变量作为参数 (verticesCount: Int),因此这里 verticesCount 不可能未初始化或为空(并且我们在这些行上没有得到相应的错误)。
当已经初始化的变量再次未初始化时,最后一行发生了什么?
以下代码将返回错误,
$ perl -E'sub foo { my $bar if 0; $bar++ }'
This use of my() in false conditional is no longer allowed at -e line 1.
Run Code Online (Sandbox Code Playgroud)
但是这段代码
$ perl -E'sub foo { my $bar = undef if 0; $bar++ }'
Run Code Online (Sandbox Code Playgroud)
不返回错误。这两种形式有什么区别吗?
我是 C++ 新手,正在尝试理解一些东西。我的 main.cpp 中有这段代码:
Radio r = Radio("PSR", 100.8);
Run Code Online (Sandbox Code Playgroud)
或该代码:
Radio r("PSR", 100.8);
Run Code Online (Sandbox Code Playgroud)
两者似乎都有效并且做同样的事情。那么有什么区别呢?
c++ constructor initialization variable-initialization visual-studio
我使用Swift 3,我想基于布尔值初始化一个String let变量.我知道如何使用带有var变量但不是单行表达式的标准if语句来完成它.
使用Java我会做:
String str = myBool ? "John" : "Peter";
Run Code Online (Sandbox Code Playgroud)
是否有与Swift 3等效的不使用var并以单行方式?
c++ ×3
c ×2
c# ×1
class ×1
constructor ×1
delphi ×1
immutability ×1
kotlin ×1
perl ×1
pointers ×1
swift ×1
syntax ×1
while-loop ×1