mary
mary

Reputation: 919

generic programming in c++ and typedef inside a class

let's say I have the following code in A.cpp file:

template <typename T>
class A{
   typedef T myType;
   myType foo();
}

If I want to implement the foo function in this file , what is the syntax to write the function declaration? I thought it'll be:

template <class T>
myType A<T>::foo(){
.
.
.
}

obviously it's wrong.

Upvotes: 0

Views: 301

Answers (2)

spraff
spraff

Reputation: 33395

template <typename T>
typename A<T> :: myType A<T> :: foo ()
{
}

Upvotes: 0

thiton
thiton

Reputation: 36049

Yes, the typedef is only available within the class, and the return type is not in the class:

template <class T>
typename A<T>::myType A<T>::foo() {}

Upvotes: 4

Related Questions