update overloading [][] ..working but not with a template operator ..arrrrrrrrrgghghhh 
Author Message
 update overloading [][] ..working but not with a template operator ..arrrrrrrrrgghghhh

Okay I've got it going sorta.  What I really really need is someone to just
help me out with getting the blasted template bit working.  Theirs has to be
something...I dunno typename, wierd metalanguage trickery that's gota let me
do this !! pleeeeeeaase !! let their be a god !!

#include "stdio.h"

template <class T>
class C{
public:

 T i;

Quote:
};

class B{
public:
 B(){
 p.i = 2;}

 C<int> p;

 template <class T>
 C<T>& operator[](int i){
  return p;
 }

Quote:
};

class A{
public:
 B p;

 B& operator[](int i){
  return p;
 };

Quote:
};

int main() {

 // attempt to get a reference to C
 A y;
 B z;

 C<int> &x = z[2];            // yea this is stuffing up...refer below for
error msg.

 // C<int> &x = y[2][3];

 printf("x.i = %i",x.i);

 return 0;

Quote:
}

I think what I'm asking for is impossible cause it relies on the compiler
instantiating the template [] operator based on what it is assingning the
result to...I need some way of telling vc what T will be but I cant stick a
T in the middle of the parameter list cause thats not how u call the []
operator.  So yea I get this error.

i:\temp\pointer_stuff\overload_[][].cpp(43) : error C2783: 'class C<T>
&__thiscall B::operator [](int)' : could not deduce template argument for
'T'

Sort of the same reason why we can't overload a function based on the return
type I guess.  You see in the main app. I'm doing each column is a template
class and depending on which column i.e. the first index the user chooses I
want to return the appropriate instantiated template type. i.e. the T in the
operator[] func will depend on what it is being assigned to.

Can I do this a different way by like providing a template conversion
operator?  If so can anyone give me a example on how to do this.

Thanx heaps...forever gratefull!!....though I suspect that this just isn't
possible...oh well no harm in asking I guess.
Jason



Fri, 04 Apr 2003 03:00:00 GMT  
 update overloading [][] ..working but not with a template operator ..arrrrrrrrrgghghhh


Quote:
> class B{
> public:
>  B(){
>  p.i = 2;}

>  C<int> p;

>  template <class T>
>  C<T>& operator[](int i){
>   return p;
>  }
> };

    The template parameter doesn't figure into the function signature.
Hence you are attempting to overload on the return type.  That is not
allowed.

The only way I can see getting this to work is:
class B
{
 public:
  B()
 {
  p.i = 2;

Quote:
}

  C<int> p;

  template <class T>
  C<T>& Index(int i)    // C<T>& Index(int i, T* = NULL) prior to v6sp3
 {
   return p;
  }

Quote:
};

int main()
{
 B z;

 C<int> &x = z.Index<int>(2);

--
Truth,
James Curran
http://www.NJTheater.com          (Professional)
http://www.NovelTheory.com      (Personal)
http://www.BrandsForLess.com   (Day Job)



Fri, 04 Apr 2003 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. class templates and overloaded operators

2. operator overloading in template class

3. error C2440 when try to compile template class with overloaded operator

4. error C2440 when try to compile template class with overloaded operator

5. Template functions & overloaded operators

6. Help! Double-Linked List, Templates, and Operator Overloading

7. Resolving overload Template operator<<() with VS 2003

8. overloading operator<< in template

9. Operator overloading in a template class ?

10. Operator overloading and templates

11. Operator Overloading attempt doesn't quite work...

12. CXX0058: Error: Overloaded Operator Not Found

 

 
Powered by phpBB® Forum Software