我不明白为什么以下代码编译.
public class House<T> {
static <T> void live(House<T> a) {}
static {
new House<Integer>() {
{
this.live(new House<String>());
}
};
}
}
Run Code Online (Sandbox Code Playgroud)
T在new House中输入静态代码是一个Integer,因此live函数的所需参数类型是House<Integer>,而它是编译的House<String>.
请解释.
你能解释一下为什么在下面的代码中:
#include <iostream>
void fun(char * const & x){(*x)++;}
int main(){
char txt[100]="kolokwium";
fun(txt);
std::cout << txt <<"\n";
}
Run Code Online (Sandbox Code Playgroud)
代码编译需要关键字const吗?
如果我将其删除,我得到:
invalid initialization of non-const reference of type ‘char*&’ from an rvalue of type ‘char*’
Run Code Online (Sandbox Code Playgroud)
谢谢!
在下面的代码中,由于name()是虚拟的,我希望将调用派生结构的方法.相反,什么是写出来的是"A".为什么?
#include <iostream>
using namespace std;
struct A {
virtual string name() { return "A"; }
};
struct B : A {
string name() { return "B"; }
};
int main (int argc, char *argv[]) {
B b;
cout << static_cast<A>(b).name() << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)