?
Solved

C++ templates

Posted on 1998-11-05
4
Medium Priority
?
213 Views
Last Modified: 2010-05-18
I'm looking for some information or discussion on some of
more odd uses of C++ templates, particularly of the forms

     class A : public B<A>

     template<class B>
     class D : public B

and similar such forms where you are templatizing some
aspect of an inherhitance hierarchy, and introducing what
looks like strange coupling between base and derived
classes.


I've tried, without luck, to find J. Coplien's C++ report
article on Curiously Recurring Template Patterns. I'm
looking for insight on how these constucts are used,
how they are useful, etc.

Hoping there's an expert out there that can enlighten me!

Thanks!

--shri
0
Comment
Question by:shriv
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 5

Accepted Solution

by:
yonat earned 100 total points
ID: 1177061
There was an article by James Coplien in the C++ Report (I think)  a while back about this. The title was something like "Curiously Recurring Template Patterns".

If I remember correctly, class A : public B<A> is used to achieve code reuse without lossing type safety: B implements the algorithms, and some of it's functions return A-s. Using this form, B "knows" the specific A used, so it can return the right type.

Example:

template<class T>
class Singleton
{
public:
   static T& instance();
protected:
   Singleton() {}
private:
   Singleton(const Singleton&);
   Singleton& operator=(const Singleton&);
};

class Database: public Singleton<Database>
{
private:
   Database();
   friend class Singleton<Database>;
};

template<class B> class D : private B can be used to implement the adapter pattern. I am not sure what you can use public derivation for in this case.

Hope this helps.
0
 
LVL 5

Expert Comment

by:yonat
ID: 1177062
It was the February 1995 issue, and is reprinted in the book "C++ Gems".
0
 

Author Comment

by:shriv
ID: 1177063
I appreciate the pointer to the article. I think, though, that the implications of these template
forms are more subtle than the explanation. I understand that templates are used for
type saftey & reuse: this is the most common usage in collection classes. The "deriving
from itself" appears to be something else entirely, and I believe, has uses beyond
just the singleton example, uses which I'm still unable to grok at this point.

I'd also appreciate an expansion on the adapter pattern implementation.


0
 
LVL 5

Expert Comment

by:yonat
ID: 1177064
Here is another example of class A : public B<A>: Let's say you want to keep track of all instances of a class. Then you write:

template <typename T>
class TypeTracker
{
        static std::List<TypeTracker *> theListOfAllT;
public:
        TypeTracker()  { theListOfAllT.push_back(this); }
        ~TypeTracker() { theListOfAllT.erase(this); }
        staic const std::List<TypeTracker *> &getAll() { return theListOfAll; }
};
TypeTracker::theListOfAll;

class MyTrackedClass : virtual public TypeTracker<MyTrackedClass>
{
        ...
};

As for the adapter, see more info at http://rampages.onramp.net/~huston/adapter.html and http://rampages.onramp.net/~huston/adapter.cpp .

0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question