Member Templates

W

Wojtek Michalik

templete <class T>

class MyClass
{
private:
T member1;
T* member2;

template <class Type2>
class Embedded
{
T foo;
Type2 something;
};
public:
template <class AnotherType>
void function(AnotherType arg);
}
/* definition of member function template
template<class T> template <class AnotherType>
void MyClass< class T>::function(AnotherType arg)
{ /* do something */}

/* instantiation of it */

MyClass<int> example(constructor_args);
int a;
example.function( a );
/*
I found similiar but longer example in "C++ Primier" of Stanley B. Lippman
chapter 16.7. The book says nothing about instantiation an use of member template
<class Type2> class Embedded.
Can anybody give me an axample of use of such template ????
*/
 
V

Victor Bazarov

Wojtek Michalik said:
templete <class T>

class MyClass
{
private:
T member1;
T* member2;

template <class Type2>
class Embedded
{
T foo;
Type2 something;
};
public:
template <class AnotherType>
void function(AnotherType arg);
}
/* definition of member function template
template<class T> template <class AnotherType>
void MyClass< class T>::function(AnotherType arg)
{ /* do something */}

/* instantiation of it */

MyClass<int> example(constructor_args);
int a;
example.function( a );
/*
I found similiar but longer example in "C++ Primier" of Stanley B. Lippman
chapter 16.7. The book says nothing about instantiation an use of member template
<class Type2> class Embedded.
Can anybody give me an axample of use of such template ????
*/

I can't think of any use for MyClass::Embedded, but if you need
it instantiated, it should go something like

typename MyClass<int>::Embedded<char> myclass_embedded = { 42, 'F' };

Although, it's impossible to do outside 'MyClass' scope because the
member 'Embedded' is declared "private".

Victor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,460
Latest member
eibafima

Latest Threads

Top