Hey guys, I'm working on converting some existing Java Code over to C++, trying to use only built in standard libraries because the platform we are building on we won't be able to have any outside libraries, and it's been a while since I had to write "fancy" c++ code so here is probably an easy question for you.

I have a base class in Java that is a Number as a return type, each of its chidren return a different type (int, double, float etc). Also have a function that takes an object in java, and each child acts differently on it.

public abstract class Base{
...
 public abstract Number doSomething();
public abstract void setMe(Object blah);
...
}
public class Child1 extends Base{
...
public Number doSomething(){
value= new Double(somedouble);
return value;
}

public void setMe(Object blah){
value=(Double)value;
}

What would be the best way to go about this? C++ doesn't have a generic like Number.. can I use a template in c++ aka template<class Number>.

Do I have to do anything special to make that work? Also this will be on a Linux system.

Have a look at the boost::any class. http://www.boost.org/doc/libs/1_41_0/doc/html/any.html

If you combine it with some of the typelist ideas in the boost::mpl then you can basically do what you want.

The downside is that this style of programming often looses type safety, which is "a bad thing" as it moves compile errors to run-time exceptions.

I am trying to stay away from Boost if possible, and use the built in typical standard libraries. Is there not a way to do it with templates and the like? It's been a while since I had to do any template wrangling, but I will head to boost as a last resort if I can just yank out the few headers that are needed and not have to install it onto the end system.

Oh I probably should mention, this code will get ported to Solaris 8, so trying to minimize anything that might not port well.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.