Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 316
  • Last Modified:

Templates


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


 Thanks
0
e6694811
Asked:
e6694811
  • 7
  • 6
  • 3
  • +3
1 Solution
 
ShaunWildeCommented:
Can you explain yourself more clearly ?
0
 
e6694811Author Commented:

 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
 
jcajrCommented:
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
e6694811Author Commented:

 Hmmm are you sure that your solution works ?
0
 
e6694811Author Commented:
Adjusted points from 30 to 55
0
 
MDarlingCommented:
jcajr's solution looks fine - what's the problem?

0
 
WynCommented:
why is jcajr wrong ?


Regards
W.Yinan
0
 
WynCommented:
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
 
RONSLOWCommented:
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
 
e6694811Author Commented:

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


 
0
 
RONSLOWCommented:
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
 
e6694811Author Commented:

 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
 
RONSLOWCommented:
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
 
jcajrCommented:
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
 
RONSLOWCommented:
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
 
jcajrCommented:
Quite right. I was trying to post a clarification, and hit the wrong button. Sorry about that.
0
 
e6694811Author Commented:

 Dummy answer rejected...All my classes have a default constructor ,so it's no problem .
0
 
RONSLOWCommented:
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
 
e6694811Author Commented:

 Send an answer ,if you want.
0
 
RONSLOWCommented:
same as above
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 6
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now