Solved

Use of class1<class2> *pname

Posted on 1998-03-11
6
233 Views
Last Modified: 2010-04-10
I have recently taken on a project where the previous coder has used a lot of statements of the form:

SOME_NAME<double> *ptrname;

I initially thought that SOME_NAME must be a class but I can see no declarations for it in any header files included in (or with) the project.

I would be greatful to anyone who can solve this one which has baffled myself and my colleagues.

Thanks
0
Comment
Question by:gavinpat
  • 5
6 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 150 total points
ID: 1183345
answer coming.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1183346
SOME_NAME<> is a template class.  If you are not familar with templates, they are a little like C's old macros (#define's) but much safer and more powerful.  (they aren't really that much alike, but the serve the same purposes)

more coming.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1183347
Template classes allow you to create many different classes that contain the same logic but work on different data types.  For example say you want to create a stack class.  you could create a stack that stores integers.  but what if you later need to store characters, or say "class WIDGIT"  You would have to create seperate stack classes for each of these data types.  Template classes automate this.

The template class is sort fo like the recipe for the class in general.  Then you can create specific classes from the template for specific types.

When you see something that is a template class name followed by "<", some type, and ">" that is telling the compiler that you want a template class instantiated (made automatically) for the specfied type.

for example if we declared a stack template class

template<class T> class STACK
{
  int count
 T Ary[100]; // room for 100 items.
}
/// not a good class, but a good example.

Then we could create stacks for characters and integers like this

STACK<char> characterstack;
STACK<int> integer stack;

Note that STACK is not a type, it can't be used like other types (char, int, double)  However, STACK<char> or STACK<anything>  is a type like any other type.  It can be used like any other type  (passed to procedures, used in typedefs, etc.)  It just is a weird looking type.


0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 22

Expert Comment

by:nietod
ID: 1183348
Now if you see SOME_NAME<double> and it compiles, then the template class SOME_NAME is defined somewhere.  it may not be written byt eh programmer who wrote the project, that is, it might not be in the .h files he/she produced.  The standard C++ library contains many templates.  It could be one of those.  (They are defined in include files and those files will be "#include"ed in you source code somewhere.)

If you can't find them.  post some example names here and I'll left you know if they are part of the standard library.
0
 

Author Comment

by:gavinpat
ID: 1183349
Is it legal to have class templates as friend classes such as say:

template <class TEMPLATE_TYPE>
class ALT_MATRIX_ROW

{
      TEMPLATE_TYPE       *pRow;
      
public:
// Other code for class goes in here
//

friend class ALT_MATRIX<TEMPLATE_TYPE>;
}
0
 
LVL 22

Expert Comment

by:nietod
ID: 1183350
Yes, that is legal.  Almost everything that is legal for a non-template is legal for a template.  Derived template classes.  template classes derived from non-template classes.  template classes derived from template classes.  Template classes whose members are template classes.  Template classes that contain private template classes.  Lots of other possibilities are all legel.  Most work.  Implimentation of templates is not 100% yet but they are getting close.  Microsoft VC++ 5.0 supports the standard almost 100% and does a good job with a few quirks.  Borland C++ 5 does less well, but Borland builder is supposidly 100% compliant.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

747 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now