什么是int
C#?它是一个关键字,还是从system.ValueTypes派生的类?如果它是关键字,那么以下行如何编译
int i = new int(); // If int is not a class then how does it have a default constructor
Console.WriteLine(i.ToString()); // If int is not a class then how does it have member functions
Run Code Online (Sandbox Code Playgroud)
如果int
是一个类,为什么没有必要总是用它初始化它new
?以下行如何编译?
int i = 8;
Run Code Online (Sandbox Code Playgroud) 在下面的代码中,我只希望两个线程中的一个进入该halt()
函数,然后暂停程序.但似乎两个线程都进入了synchronized
halt()函数.为什么会发生这种情况?
package practice;
class NewThread implements Runnable {
String name; // name of thread
Thread t;
boolean suspendFlag;
NewThread(String threadname) {
name = threadname;
t = new Thread(this, name);
System.out.println("New thread: " + t);
suspendFlag = false;
t.start(); // Start the thread
}
// This is the entry point for thread.
public void run() {
try {
for(int i = 15; i > 0; i--) {
System.out.println(name + ": " + i);
Thread.sleep(200);
Runtime r = …
Run Code Online (Sandbox Code Playgroud) 为什么上一个预期输出与以下代码中的实际输出不同?
#include<iostream>
#include<fstream>
#include<istream>
#include<sstream>
#include<vector>
using namespace std;
int main()
{
vector<int> v;
for(int ii = 0; ii < 4; ii++){
v.push_back(0);
}
vector<vector<int>> twoDv;
for(int ii = 0; ii < 5; ii++){
twoDv.push_back(v);
}
cout<<"Expected Output : " << &twoDv[0][0] <<'\t'<< (&twoDv[0][0] + 3) <<'\t'<< (&twoDv[0][3] + 1)<<'\n';
cout<<"Actual Output : " << &twoDv[0][0] <<'\t'<< &twoDv[0][3] <<'\t'<< &twoDv[1][0] << '\n';
}
Run Code Online (Sandbox Code Playgroud) 在下面的代码中,我们返回一个const对象并将其收集在非const对象中,并且它仍然可以编译而不会出现错误或警告.
class foo
{
int i;
public:
const foo& call() const
{
return *this;
}
};
int main()
{
foo aa, bb;
bb = aa.call();
}
Run Code Online (Sandbox Code Playgroud) 以下代码有什么问题?我们如何使print()函数作为printf工作?
#include <stdio.h>
#include<stdarg.h>
void print(char *format,...)
{
va_list args;
va_start(args,format);
printf(format,args);
}
int main() {
print("%d %s",5,"le");
}
Run Code Online (Sandbox Code Playgroud)