Solved

Use of class1<class2> *pname

Posted on 1998-03-11
6
234 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C++ to C# code conversion issue 4 100
How to print into std::string like sprintf using C++11? 12 50
C++ :Change value from  DisableCMD registry 4 51
What is sub-make ? 2 39
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

895 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

17 Experts available now in Live!

Get 1:1 Help Now