Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C++ Templates

Posted on 1999-06-23
2
Medium Priority
?
198 Views
Last Modified: 2010-04-15
see this code:
class string{
public:
      str[20];
      int a;
      int b;
}

///////////////////////////////

template <type,key>
class word
{
private:
      type data;          
      int q;
      int p;
public:
      whatever you want
}

///////////////////////////////////

template <class type,class key>
class nod
{
private:
      word<type,key> tp;
public:

      add(word<type,key>&);

      
      whatever you want
}

template ......... add(word<type,key>& w)
{
/*      
here how do you tell the compiler that you want to access certain
element in the 'type' that I will instantiate as 'key'

eg.: I will do      in main()  nod<string,int>
Is there no way to tell the compiler that
I what I want to do is nod <string, word::a> I hope you understand
what I mean although the syntax may be incorrect.

Secondly suppose I want to access word::b
so how do I do
can I do(probably can't)   int i=w.data::key;
i.e get the element said as key in the data type in the word class.

*/
}
0
Comment
Question by:mahmad
[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
2 Comments
 
LVL 2

Expert Comment

by:obg
ID: 1262909
There is a C++ area...
0
 

Accepted Solution

by:
murrayc earned 240 total points
ID: 1262910
If I understand that:

You can access types used by classes if you declare a public typedef:

e.g.
class word
{
public:
    typedef int type_example;
};

However, this is a bit different when you want to make a template argument type public:


template <class type,class key>
class word
{
public:
    typedef typename key type_key;
};

Note that MS VC++ doesn't require the use of the typename keyword, but egcs does.



0

Featured Post

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.

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

664 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