Solved

Use of class1<class2> *pname

Posted on 1998-03-11
6
238 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 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.

822 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