Solved

Templates

Posted on 2000-04-11
20
266 Views
Last Modified: 2013-11-20

 How can I create a template whose parameters can be only
pointers to objects ?


 Thanks
0
Comment
Question by:e6694811
  • 7
  • 6
  • 3
  • +3
20 Comments
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 2703674
Can you explain yourself more clearly ?
0
 

Author Comment

by:e6694811
ID: 2703745

 Consider ,for example the following template
 class:

 template <class C1 , class C2 ,..,class CN> class MyClass
{
 .....
.......

 

}



 As you may know  ,we have to instantiate the class by giving the arguments for the class templates. For my
class ,we can write something like :


 MyClass <int ,int , .. ,int*> instance;


 But I don't want this instatiation to be possible ,because there are parameters that are not pointers
to objects .In other words ,all the parameters should
be pointers to objects .Therefore the two instantations
below are correct:


 MyClass <int * ,CPoint * ,..,CPoint*> instance;
 MyClass> CRect * ,double * ,.., BOOL*>
 

 I hope you know what I mean .

 Regards
0
 

Expert Comment

by:jcajr
ID: 2703828
How about this:

template <class T1, class T2, etc. >
class Pointers
{
   T1* Pointer1;
   T2* Pointer2;
    ....
};

Then the declaration
Pointers<int,CString> p;
results in p.Pointer1 being a int* and p.Pointer2 being a CString*


0
 

Author Comment

by:e6694811
ID: 2704350

 Hmmm are you sure that your solution works ?
0
 

Author Comment

by:e6694811
ID: 2704358
Adjusted points from 30 to 55
0
 
LVL 3

Expert Comment

by:MDarling
ID: 2704416
jcajr's solution looks fine - what's the problem?

0
 
LVL 5

Expert Comment

by:Wyn
ID: 2704422
why is jcajr wrong ?


Regards
W.Yinan
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2704444
The arguments in template to create object all deponds ,the programmer,how you specify it...It's your business...
To assure it's a pointer,you specify as
T* pointer...
Regards
W.Yinan
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2705956
you can use templates to do compile time checking.

the simplest is probably this:

template <typename T> class X {
    enum { _ASSERT_ = sizeof(*T()) };
public:
    X() {}
    ...
};

this wil fail unless T is a pointer type.
ie this works

  X<int*> xpint;

but this will generate a compilation error

  X<int> xint;

(ie you'll get a compile time error if you try to say
0
 

Author Comment

by:e6694811
ID: 2707621

 
 Thanks ,RON but I think I'll accept jcajr's solution.
 


 
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 10

Expert Comment

by:RONSLOW
ID: 2710090
Jcar's solution doesn't do what you wanted (ie only allow template parameters to be pointer types).

Or have you changed your question?

PS: what JCar does is the (obvious) solution to not having that limitation in the first place ... but it does not address how to ensure the parameters are pointer types - which is the question you asked.

0
 

Author Comment

by:e6694811
ID: 2711463

 It's true .I didn't realize that Jcar's solution does not have any kind
of restriction.

 Does any MFC template have the same limitation (only pointers are instantiable).


 Thanks.
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2711805
What I proposed above will generate an error if you use a non-pointer type as a parameter.  ie if you use a non-pointer type as a template parameters, then you'll get a compilation error message.

Generally, you don't need to worry too much about the types passed to a template.  If your template is written assuming the behaviour of one type (eg -> for pointers), and the type you supply doesn't have that behaviour, then you'll get error when you instantiate the template and try to use that behaviour.

That is what MFC relies upon for its classes (like CTypedPtrArray) .. if you supply a non-pointer, then you'll get errors when you use the template.  That assumes that you actully call the functions that require the type to be a pointer .. template member functions don't get generated (and properly checked for errors) until they are used (called/referenced).  That is the advantage of the method I used .. the typedef always gets checked.
0
 

Expert Comment

by:jcajr
ID: 2711829
Been away for a couple of days...

 "Does any MFC template have the same limitation (only pointers are instantiable). "

Not sure what you mean by this sentence. Actually, I like RONSLOW's answer; as he says, you will get a compile-time error. Example below:

template <class T>
class Pointer
{
   enum {_DUMMY_ = sizeof(*T())};

   T pointerToSomething;
....
};

Only caveat I can see is that type T must possess a default contstructor, i.e. a constructor that takes no arguments or has defaults for all arguments.
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2711852
jcajr .. why did you post a rehash of my comment as your answer?  I thnk if anyone should do that, it would be me, no?

Also the caveat will not be a problem .. simple pointer types (eg int*) always have default constructors anyway.
0
 

Expert Comment

by:jcajr
ID: 2712003
Quite right. I was trying to post a clarification, and hit the wrong button. Sorry about that.
0
 

Author Comment

by:e6694811
ID: 2712116

 Dummy answer rejected...All my classes have a default constructor ,so it's no problem .
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2712141
e.. in what I was suggesting, the class of the object pointed TO doesn't matter .. it doesn't need default constructors etc.  It is the pointer ITSELF that needs a default constructor, and all primitive tupes (including pointers to anything) have default constructors.  ie you can declare an
  int* p;
without needing arguments after the p.

Do you want me to propose and answer again, or do you want to leave the question open a little longer?

0
 

Author Comment

by:e6694811
ID: 2712380

 Send an answer ,if you want.
0
 
LVL 10

Accepted Solution

by:
RONSLOW earned 55 total points
ID: 2712471
same as above
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
VB.NET how to use the Vertical ScrollBar 5 87
Generic progress indicator 6 109
maxBlock challenge 30 100
Detect file exist or not 3 78
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

705 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

19 Experts available now in Live!

Get 1:1 Help Now