9
I made the following code in C++:
#include <iostream>
template <typename type> class Foo
{
public:
Foo(type FooTest)
{
std::cout << "Foo " << FooTest;
}
};
int main
{
new Foo<double>(10.0);
}
Output: "Foo 10".
Java: (Mainclass.java)
public class MainClass
{
public static void main(String[] args)
{
new Foo<Double>(10.0);
}
}
class Foo<type>
{
public Foo(type FooTest)
{
System.out.print("Foo ");
System.out.print(FooTest);
}
}
Output: "Foo 10.0".
This, just one case to demonstrate what happens, the output is different in both cases for double. How can I match them (Java = 10 and C++ = 10.0) ?
It’s not the focus of the question but I think it’s important to note that your C++ code does some things that are not recommended/unnecessary, such as using
new
in this case ( causing a memory leak ) and creating a class for a function that could be released.– Tiago Gomes
@Tiagogomes of course. If I want to make a code demonstrative, I should not worry too much about improvements; even so, to make a similar code in Java, the correct thing is to use orientation-to-objects.
– user2692