Custom Array Class

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2008
Posts: 67
Reputation: c++noobie is an unknown quantity at this point 
Solved Threads: 1
c++noobie c++noobie is offline Offline
Junior Poster in Training

Custom Array Class

 
0
  #1
Dec 14th, 2008
I'm trying to create an template of an array class that encapsulates an array of a given type with several methods that are useful but nonstandard to regular c++ arrays. I have created a link struct to contain elements of the array and pointers to both the next and previous elements, and I am trying to overload the << operator to allow the value of a link to be printed easily simply by writing "cout << link_var" but I am having problems. Here is my code
  1. #if !defined _LINK_
  2. #define _LINK_
  3.  
  4. #if !defined _IOSTREAM_
  5. #include _IOSTREAM_
  6. #endif
  7.  
  8. typedef unsigned short int size_l;
  9.  
  10. template <typename T>
  11. class list;
  12.  
  13. template <typename T>
  14. class link
  15. {
  16. typedef link<T>* link_p;
  17.  
  18. T value;
  19. link_p next, previous;
  20. public:
  21. link()
  22. : value(NULL), next(NULL), previous(NULL)
  23. {}
  24.  
  25. link( T _value, link_p _next= NULL, link_p _previous= NULL )
  26. : value(_value), next(_next), previous(_previous)
  27. {}
  28.  
  29. T rvalue() { return this->value; }
  30.  
  31. link_p rnext() { return this->next; }
  32.  
  33. link_p rprevious() { return this->previous; }
  34.  
  35. const char* type() { return typeid(T).name(); };
  36.  
  37. friend class list<T>;
  38.  
  39. template <typename CharT>
  40. friend std::basic_ostream<CharT> &operator<< ( std::basic_ostream<CharT> &out, const link &_link )
  41. {
  42. out << _link.rvalue();
  43.  
  44. return out;
  45. }
  46. };
  47.  
  48. #endif /* _LINK_ */
Also, a little side not, I'm not sure if the type() method is the proper return type, code, etc. I would appreciate any help that can be offered, thank you.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 67
Reputation: c++noobie is an unknown quantity at this point 
Solved Threads: 1
c++noobie c++noobie is offline Offline
Junior Poster in Training

Re: Custom Array Class

 
0
  #2
Dec 14th, 2008
Ok, I found a fix, I replaced the const link& with a regular link on line 40, but I don't understand why it can't be passed like that to avoid the recreation of the variable. If anyone could clear that up for me, I would appreciate it.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 67
Reputation: c++noobie is an unknown quantity at this point 
Solved Threads: 1
c++noobie c++noobie is offline Offline
Junior Poster in Training

Re: Custom Array Class

 
0
  #3
Dec 14th, 2008
I'm sorry to keep throwing on questions, but I found one more question I have, is there a way to create a standard for operator function? For example, if I want something like this for operator declarations within a class to be able to work with all arithmetic operators (+, -, * and /)
  1. T operator{operator type here}( T _value ) { return ( this->value {operator type here} _value ); }
Last edited by c++noobie; Dec 14th, 2008 at 6:06 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC