Re: templating



Robert Rehammar schrieb:
class myNodeClass : NodeTree<tClass> {
	myNodeClass (string s) :
		NodeTree<tClass> {
		...
	};
}

You forgot the parentheses:

 	myNodeClass (string s) :
 		NodeTree<tClass>() {
 		...
 	};


But this won't compile as the template for NodeTree, tClass, requires a
parameter (the string s) and I simply can't figure out how to pass it to
the template.

Writing the constructor as

myNodeTree (string s): NodeTree<tClass (s)> won't compile, complaining
that s can't appear in a constant expression, so this seem so suggest
that I can't template with a class that require any parameters for
initiation...

You don't want to pass a parameter to a class at compile time but at runtime. I assume that NodeTree is the Glib::NodeTree<> template class. It takes a *compile time* parameter T which is your class tClass. You want to pass it a *runtime parameter* when it gets constructed while the program is executing. So you have to know what NodeTree<> does with its template parameter T. Looking at NodeTree's declaration you will find an explicit constructor: explicit NodeTree(const T& data). This means that you can pass NodeTree's constructor a tClass object which in turn you can construct with the string like so:

class myNodeClass: NodeTree<tClass>
{
  // better take a const reference to avoid string copying
  myNodeClass(const string& s):
    NodeTree<tClass>(tClass(s))
  {
    // ...
  }
};



Klaus


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]