943,704 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1568
  • C++ RSS
Dec 14th, 2008
0

Custom Array Class

Expand Post »
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
c++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
c++noobie is offline Offline
71 posts
since Feb 2008
Dec 14th, 2008
0

Re: Custom Array Class

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.
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
c++noobie is offline Offline
71 posts
since Feb 2008
Dec 14th, 2008
0

Re: Custom Array Class

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 /)
cpp Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
c++noobie is offline Offline
71 posts
since Feb 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Integer partition: code works - any suggestion to improve it?
Next Thread in C++ Forum Timeline: Adding Arrays to my game





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC