小编hun*_*y91的帖子

java中的通用参数类型匹配

我不明白为什么以下代码编译.

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>.

请解释.

java generics

1
推荐指数
1
解决办法
259
查看次数

函数中的Const关键字带有*&参数.

你能解释一下为什么在下面的代码中:

    #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)

谢谢!

c++ const pass-by-reference

1
推荐指数
1
解决办法
60
查看次数

c ++中的Static_cast和虚方法

在下面的代码中,由于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)

c++ virtual static-cast

1
推荐指数
1
解决办法
231
查看次数

-2
推荐指数
1
解决办法
292
查看次数