?
Solved

Use of class1<class2> *pname

Posted on 1998-03-11
6
Medium Priority
?
242 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
[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
  • 5
6 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 450 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

765 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