我有一些模板代码,我宁愿存储在CPP文件中而不是标题中的内联.我知道只要您知道将使用哪些模板类型,就可以完成此操作.例如:
.h文件
class foo
{
public:
template <typename T>
void do(const T& t);
};
Run Code Online (Sandbox Code Playgroud)
.cpp文件
template <typename T>
void foo::do(const T& t)
{
// Do something with t
}
template void foo::do<int>(const int&);
template void foo::do<std::string>(const std::string&);
Run Code Online (Sandbox Code Playgroud)
注意最后两行--foo :: do模板函数仅用于int和std :: strings,因此这些定义意味着应用程序将链接.
我的问题是 - 这是一个讨厌的黑客还是会与其他编译器/链接器一起使用?我目前只在VS2008上使用此代码,但是想要移植到其他环境.
我主要使用Java和泛型相对较新.我一直在阅读Java做出错误决定或者.NET有更好的实现等.
那么,泛型中C++,C#,Java之间的主要区别是什么?每个人的利弊?
Class<? extends Something>
这是我的解释,它是班级模板但课程?表示类的名称未确定,它扩展了Something类.
如果我的解释有问题,请告诉我.
我有以下不编译的代码.
class Base {
public:
virtual ~Base() { };
};
class Derived : public Base { };
class NotDerived { };
template <typename T>
class Group { };
int main() {
Group<Base> *g = NULL;
g = new Group<Base>(); // Works
g = new Group<Derived>(); // Error, but I want it to work
g = new Group<NotDerived>(); // Error, as expected
}
Run Code Online (Sandbox Code Playgroud)
我知道这不会编译,因为它g是一种不同的类型Group<Derived>.为了在Java中工作,我会做一些事情Group<? extends Base> g,但据我所知,C++没有那个关键字.可以做些什么?
编辑:我想澄清,我不希望它可以设置不从派生类型Base的g.我已经更新了我的例子来解释这一点.
编辑2:我的问题有两个解决方案. 戴夫的发现简单易行.但 …
我想add()用Java 编写单个方法,可以添加整数,字符串等.泛型会帮助我.
我无法理解泛型的终极目标.我感到很困惑.
泛型与重载?
public Integer add(Integer i, Integer j){return i+j;}
public String add(String i, String j){return i+j;}
public <T> T add(T i, T j){return i+j;} //this gives me error.
Run Code Online (Sandbox Code Playgroud)
请让我离开它.
谢谢.
我是Java新手,但不是编程(我通常用Ruby编写代码).我在Java代码示例中看到的一件事是使用<>而不是()将params传递给对象.下面是一个代码示例(摘自Google Web Toolkit教程):
public void onValueChange(ValueChangeEvent<String> event) {
String token = event.getValue();
// depending on the value of the token, do whatever you need
...
}
Run Code Online (Sandbox Code Playgroud)
它与铸造有关还是别的什么?有人可以向我解释这表示或用于什么?谢谢!
我想在java中编写一个泛型方法,如下所示:
public <T extends Number & Comparable<T>> void test(T[] a){
T b=a[0];
if(a[0]>0){
a[0]*=a[0];
b+=a[1];
}
}
Run Code Online (Sandbox Code Playgroud)
再后来,我可以提供任一Integer[]或Double[]或其他Number亚型的方法.但我上面尝试的代码给了我错误.
请帮我.谢谢.
我试图模拟在另一个模糊的编程范例中使用的一种指针,所以我可以将一些代码移植到Java.另一种语言不是面向对象的,而是 Pascal的松散灵感.
在原始语言中,我们可以编写这样的代码.首先,使用文本.
// Start with text.
Text myVar = "Bonjour"
Pointer myPointer = ->myVar // Referencing a string variable, storing the reference in another variable of type `Pointer`.
Message( myPointer-> ) // Dereferencing the pointer, to retrieve `myVar`, and pass the string to a command `Display` that displays the message on screen in a dialog box.
Run Code Online (Sandbox Code Playgroud)
然后,切换到数字.
// Switch gears, to work with an number.
Integer vResult = ( Random % ( vEnd - vStart + …Run Code Online (Sandbox Code Playgroud) 我正在从C ++迁移到Java。现在,我正在尝试泛型方法。但是编译器总是抱怨以下错误
对于类型T HelloTemplate.java / helloTemplate / src / helloTemplate,未定义方法getValue()
错误指向t.getValue()行据我了解,T是类MyValue,它具有方法getValue
怎么了?我该如何解决。我正在使用Java1.8
public class MyValue {
public int getValue() {
return 0;
}
}
public class HelloTemplate {
static <T> int getValue(T t) {
return t.getValue();
}
public static void main(String[] args) {
MyValue mv = new MyValue();
System.out.println(getValue(mv));
}
}
Run Code Online (Sandbox Code Playgroud)