Link to home
Start Free TrialLog in
Avatar of e6694811
e6694811

asked on

Templates


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


 Thanks
Avatar of ShaunWilde
ShaunWilde

Can you explain yourself more clearly ?
Avatar of e6694811

ASKER


 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
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*



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

why is jcajr wrong ?


Regards
W.Yinan
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
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

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


 
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.


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

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


 Send an answer ,if you want.
ASKER CERTIFIED SOLUTION
Avatar of RONSLOW
RONSLOW

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial