Prev: union members accessed with another member (whose data type requires less space)
Next: union members accessed with another member (whose data type requires less space)
From: Navaneeth on 8 Nov 2009 21:53 I am trying to create a class named "tree_node" which can hold a label (char) and any type of data. I came up with the following code. template<typename T> struct node_traits { typedef T& reference; typedef T* pointer; }; template<typename T> struct node_traits<T*> { typedef T* reference; typedef T* pointer; }; template<typename T> class tree_node { public: typedef typename node_traits<T>::reference reference; typedef typename node_traits<T>::pointer pointer; tree_node(reference item) { //item_ = item; Works only if T is pointer item_ = new T(item); } reference item() { return item_; } private: pointer item_; }; My intention here is to create a container that can hold any type of object. Like STL, I would like to copy create the object for all non- pointer types. The above code will not work as I expected. Consider the following usages. Foo f; tree_node<Foo> node(f); // works well tree_node<Foo*> node1(new Foo); // will not work until i change the implementation of class 1 - How will I create the class in such way that it accepts all types. 2 - Is there anyway to keep the object inside the class without using pointers? I'd not be interested in any libraries that have such a class because I am creating this just for learning. Thanks -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: mos on 13 Nov 2009 03:42
On Nov 9, 8:53 am, Navaneeth <navaneet...(a)gmail.com> wrote: > I am trying to create a class named "tree_node" which can hold a label > (char) and any type of data. I came up with the following code. > > template<typename T> > struct node_traits > { > typedef T& reference; > typedef T* pointer; > > }; > > template<typename T> > struct node_traits<T*> > { > typedef T* reference; > typedef T* pointer; > > }; > > template<typename T> > class tree_node > { > public: > typedef typename node_traits<T>::reference reference; > typedef typename node_traits<T>::pointer pointer; > > tree_node(reference item) > { > //item_ = item; Works only if T is pointer > item_ = new T(item); > } > > reference item() > { > return item_; > } > > private: > > pointer item_; > > }; > > My intention here is to create a container that can hold any type of > object. Like STL, I would like to copy create the object for all non- > pointer types. The above code will not work as I expected. Consider > the following usages. > > Foo f; > tree_node<Foo> node(f); // works well > tree_node<Foo*> node1(new Foo); // will not work until i change the > implementation of class > > 1 - How will I create the class in such way that it accepts all types. > 2 - Is there anyway to keep the object inside the class without using > pointers? > > I'd not be interested in any libraries that have such a class because > I am creating this just for learning. > > Thanks > I haven't tested this fully, but it compiles just fine:<typename T> class tree_node { template<typename T> struct node_traits { typedef T& reference; typedef T* pointer; typedef T value_type; }; // specialized for pointer template<typename T> struct node_traits<T*> { typedef T* reference; typedef T* pointer; typedef T* value_type; }; // specialized for reference template<typename T> struct node_traits<T&> { typedef T& reference; typedef T* pointer; typedef T& value_type; }; public: typedef typename node_traits<T>::reference reference; typedef typename node_traits<T>::pointer pointer; typedef typename node_traits<T>::value_type value_type; tree_node(reference item) : item_(item) { } reference item() { return item_; } private: value_type item_; }; int x = 10; tree_node<int> copied_node(x); tree_node<int *> ptr_node(new int(11)); tree_node<int &> ref_node(x); --m -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |