9 Replies - 569 Views - Last Post: 14 March 2011 - 01:34 PM Rate Topic: -----

#1 xcodemaster  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 14-March 11

implementation of current function template class

Posted 14 March 2011 - 10:36 AM

I am implementing a current function for a sequence template class
that uses a linked list toolkit.

The current function is supposed to return the current item in the sequence.

I have a sequence template class. Therefore I would like to know if the header I wrote for the function is accurate. I worked on a similar problem before but that time I used an array to manipulate the items.

the header file for that is as follows
sequence::value_type sequence::current( ) const


since now I am manipulating the items via a linked list I wrote the

header file as follows
typename sequence<Item>::Item sequence<Item>::current( ) const 


I would like to know if this is accurate.

Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: implementation of current function template class

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: implementation of current function template class

Posted 14 March 2011 - 11:13 AM

I don't think your question is very clear. Are you asking about the use of "typename" or if the return type of sequence<Item>::Item is correct?

can you expand a little on what you are asking exactly?
Was This Post Helpful? 0
  • +
  • -

#3 xcodemaster  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 14-March 11

Re: implementation of current function template class

Posted 14 March 2011 - 11:24 AM

View PostNickDMax, on 14 March 2011 - 11:13 AM, said:

I don't think your question is very clear. Are you asking about the use of "typename" or if the return type of sequence<Item>::Item is correct?

can you expand a little on what you are asking exactly?


well definitely is the return type correct?
Was This Post Helpful? 0
  • +
  • -

#4 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: implementation of current function template class

Posted 14 March 2011 - 11:33 AM

Does it compile? If so, then its correct. If not, there's something wrong. I'm not sure what answer you're looking for
Was This Post Helpful? 0
  • +
  • -

#5 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: implementation of current function template class

Posted 14 March 2011 - 12:09 PM

So you have a collection class sequence<T> and the function sequence<T>::current() returns a an instance of T.

generally you just use:
template <typename Item>
class Sequence {
...
Item current( ) const;
...
};


template <typename Item>
Item Sequence<Item>::current( ) const {
 ...
}



The only reason you would need a syntax like:

typename sequence<Item>::Item sequence<Item>::current( ) const

is if "Item" were the name of a type declared within the class sequence which would really lead to some confusing code and probably lots of errors.
Was This Post Helpful? 0
  • +
  • -

#6 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: implementation of current function template class

Posted 14 March 2011 - 12:32 PM

As an example take this dumb little class:
template <typename T>
class Stack10 {
    T value[10];
    int inx;
    public:
    
    typedef T value_type;
    
    Stack10();
   
    bool push(const T& item);
    T pop();
    
    T* begin();
    T* end();
    
};


here I abstracted a little by declaring "value_type" -- if I want to USE Stack10::value_type as a type I need to precede it with the keyword "typename" so that the compiler knows that I don't mean some member of Stack10...

So why would you ever want to do this?

lets say I wanted to make a generic index function that would return the i'th item of a collection (not just my Stack10, but also Stack20, and Stack30 etc.) -- So now I want something like this:

template<typename StackType>
typename StackType::value_type itemAt(StackType stack, size_t index);


See since StackType is a template argument I don't know if it is a Stack10<int> or a Stack20<double> and I have no way of telling what the template argument was to the StackType -- so the collection has to contain that information for me.

you will notice that all STL collections have the typedef value_type and it is very handy.
Was This Post Helpful? 0
  • +
  • -

#7 xcodemaster  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 14-March 11

Re: implementation of current function template class

Posted 14 March 2011 - 12:42 PM

View PostNickDMax, on 14 March 2011 - 12:09 PM, said:

So you have a collection class sequence<T> and the function sequence<T>::current() returns a an instance of T.

generally you just use:
template <typename Item>
class Sequence {
...
Item current( ) const;
...
};


template <typename Item>
Item Sequence<Item>::current( ) const {
 ...
}



The only reason you would need a syntax like:

typename sequence<Item>::Item sequence<Item>::current( ) const

is if "Item" were the name of a type declared within the class sequence which would really lead to some confusing code and probably lots of errors.


Thank you very much I did use Item as the name of a type inside the class declaration. ie.,
template <class Item> // template prefix
	class sequence // class sequence definition
    {
    public:
        
		// TYPEDEFS and MEMBER CONSTANTS
        typedef Item value_type;


right now I am working on the implementation file for this header file.

I am wandering and this is why I am asking do I really need the segment
typedef Item value_type;
in the header file?

thanks I have a better understanding of the issue now.
Was This Post Helpful? 0
  • +
  • -

#8 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: implementation of current function template class

Posted 14 March 2011 - 12:57 PM

Quote

I am wandering and this is why I am asking do I really need the segment
typedef Item value_type;

in the header file?


Well, probably not - BUT if you are following Generic Programming practices (of which I am not an expert) then it is probably a good idea. I generally do this for any collection class that I do. Note that it is NOT for use inside of the class or any of its member functions. It is needed for external functions/classes that may use your class a template argument.

So STL uses this kind of pattern a great deal.

BUT -- making an STL-compatible collection requires more than just declaring the type MyClass::value_type -- so unless you have a specific requirement there really is no reason to do this.
Was This Post Helpful? 0
  • +
  • -

#9 xcodemaster  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 14-March 11

Re: implementation of current function template class

Posted 14 March 2011 - 01:02 PM

View PostNickDMax, on 14 March 2011 - 12:57 PM, said:

Quote

I am wandering and this is why I am asking do I really need the segment
typedef Item value_type;

in the header file?


Well, probably not - BUT if you are following Generic Programming practices (of which I am not an expert) then it is probably a good idea. I generally do this for any collection class that I do. Note that it is NOT for use inside of the class or any of its member functions. It is needed for external functions/classes that may use your class a template argument.

So STL uses this kind of pattern a great deal.

BUT -- making an STL-compatible collection requires more than just declaring the type MyClass::value_type -- so unless you have a specific requirement there really is no reason to do this.


Thanks I am going with
template <class Item>
	Item sequence<Item>::current( ) const


This was very helpful, I highly appreciate the clarification.
Was This Post Helpful? 0
  • +
  • -

#10 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: implementation of current function template class

Posted 14 March 2011 - 01:34 PM

:D if it was helpful you could click on the little green "Was This Post Helpful?" button...
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1