Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Typecasting operator on simple types vs. classes

Reply
Thread Tools

Typecasting operator on simple types vs. classes

 
 
Nicolay Korslund
Guest
Posts: n/a
 
      09-29-2003
Hi! I'm having a little trouble with the typecast operator, can anybody
help me with the rules for when the this operator is invoked?

In the class 'Test' in the code below, the typecast operator returns a
'mytype'. In main() I use the [] operator on 'Test'-class objects. If
'mytype' is a pointer the [] operator works fine. But if I instead try
to call an overloaded [] operator, the typecast operator is not invoked,
and I get a compiler error (unless I make an explicit cast.) So does the
'operator mytype ()' only work properly with simple types?

Any help would be appreciated!

Nico

template <class mytype>
class Test
{
mytype i;

public:

operator mytype () {return i;}
};

class Test2
{
public:
int operator [] (int i) {return i;}
};

int main(void)
{
Test<double*> t1;
Test<Test2> t2;

t1[0]; // This line works fine
t2[0]; // This line doesn't compile
((Test2)t2)[0]; // This also works

return 0;
}

 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      09-30-2003
Nikolai Borissov wrote:
> Nicolay Korslund wrote:


> I would suggest adding [] operator to the *Test* class, like this
>
> int operator[](int ind) {return i[ind];}
>
> Then you don't have to think about typecasting.


Except that t1[0] returns a double and t2[0] returns an int.

I'm thinking the other answer is a proxy class.

 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      09-30-2003
Nicolay Korslund wrote:
> Hi! I'm having a little trouble with the typecast operator, can anybody
> help me with the rules for when the this operator is invoked?
>
> In the class 'Test' in the code below, the typecast operator returns a
> 'mytype'. In main() I use the [] operator on 'Test'-class objects. If
> 'mytype' is a pointer the [] operator works fine. But if I instead try
> to call an overloaded [] operator, the typecast operator is not invoked,
> and I get a compiler error (unless I make an explicit cast.) So does the
> 'operator mytype ()' only work properly with simple types?
>
> Any help would be appreciated!
>
> Nico
>
> template <class mytype>
> class Test
> {
> mytype i;
>
> public:
>
> operator mytype () {return i;}
> };
>
> class Test2
> {
> public:
> int operator [] (int i) {return i;}
> };
>
> int main(void)
> {
> Test<double*> t1;
> Test<Test2> t2;
>
> t1[0]; // This line works fine
> t2[0]; // This line doesn't compile
> ((Test2)t2)[0]; // This also works
>
> return 0;
> }
>


I'm not sure why the code above does not work. There seems to be only
one conversion operator and one operator[] definition. I looked through
the copy of the standard I have and it is unfortunatly very wordy about
the subject and hard to filter the relevant pieces.


This is another version of your example that does work. (at least on gcc
3.3.1).


template <class mytype> class TestProxy;

template <class mytype>
class Test
{
friend class TestProxy<mytype>;
mytype v;

public:

operator mytype & ()
{
return v;
}

TestProxy<mytype> operator [] (int i);

};

template <typename T>
class PtrType
{
public:
typedef int Type; // HACK ALERT
};

template <typename T>
class PtrType<T*>
{
public:
typedef T & Type;
};

template <class mytype> class TestProxy
{
public:

Test<mytype> & m_test;
int m_i;

TestProxy( Test<mytype> & i_test, int i )
: m_test( i_test ),
m_i( i )
{
}

operator typename PtrType<mytype>::Type ()
{
return m_test.v[m_i];
}
};

template <class mytype>
inline TestProxy<mytype> Test<mytype>:perator [] (int i)
{
return TestProxy<mytype>( *this, i );
}

class Test2
{
public:
int operator [] (int i)
{
return i;
}

};

int main(void)
{
Test<double*> t1;
Test<Test2> t2;

t1[1]; // This line works fine
int x = t2[2]; // This line doesn't compile
((Test2)t2)[3]; // This also works

return 0;
}

 
Reply With Quote
 
Nikolai Borissov
Guest
Posts: n/a
 
      09-30-2003
Nicolay Korslund wrote:

<SNIP>
> In the class 'Test' in the code below, the typecast operator returns a
> 'mytype'. In main() I use the [] operator on 'Test'-class objects. If
> 'mytype' is a pointer the [] operator works fine. But if I instead try
> to call an overloaded [] operator, the typecast operator is not invoked,
> and I get a compiler error (unless I make an explicit cast.) So does the
> 'operator mytype ()' only work properly with simple types?

<SNIP>
>
> template <class mytype>
> class Test
> {
> mytype i;
>
> public:
>
> operator mytype () {return i;}
> };
>
> class Test2
> {
> public:
> int operator [] (int i) {return i;}
> };
>
> int main(void)
> {
> Test<double*> t1;
> Test<Test2> t2;
>
> t1[0]; // This line works fine
> t2[0]; // This line doesn't compile
> ((Test2)t2)[0]; // This also works
>
> return 0;
> }


There is a rule that in a conversion sequence only one use-defined
conversion operator is allowed.
My guess is that the rule covers overloaded operators too.
t1[0] works because there is only one use-defined conversion is involved :
operator mytype () {return i;}
It returns object of double* type for which built-in [] operator is defined.
t2[0], however, casts through the above user-defined conversion to Test2
type, which, in turn, has only use-defined [] operator. Of course, when you
remove one of user-defined operators from implicit sequence by explicit
typecasting, everything works: ((Test2)t2)[0]

I would suggest adding [] operator to the *Test* class, like this

int operator[](int ind) {return i[ind];}

Then you don't have to think about typecasting.

Nikolai Borissov


 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      09-30-2003
Nikolai Borissov wrote:
> Gianni Mariani wrote:
>
>>Nikolai Borissov wrote:
>>
>>>Nicolay Korslund wrote:

>>
>>>I would suggest adding [] operator to the *Test* class, like this
>>>
>>>int operator[](int ind) {return i[ind];}
>>>
>>>Then you don't have to think about typecasting.

>>
>>Except that t1[0] returns a double and t2[0] returns an int.

>
>
> That's right, because t1 and t2 are objects of different type. It's already
> not a technical matter, rather a design one.
> OP is supposed to know the purpose of his design. Maybe t1[] being of type
> int and t2[] of type double is exactly what he wants.
>
>>I'm thinking the other answer is a proxy class.
>>

>
>
> And I'm thinking about Occum's Razor:
>
> "Entities must not be multiplied beyond what is necessary"


The counter is the Mariani conjecture.

"unneccessarily complex solutions are a product of adopting
unneccessarily simple tools".

Here is the orginal in English.

If you use tools too simple for the given task you will end up with a
solution that is more complex than neccassary.

>
> Here's an original Latin version:
>
> "Pluralitas non est ponenda sine neccesitate"


 
Reply With Quote
 
Nikolai Borissov
Guest
Posts: n/a
 
      09-30-2003
Gianni Mariani wrote:
> Nikolai Borissov wrote:
> > Nicolay Korslund wrote:

>
> > I would suggest adding [] operator to the *Test* class, like this
> >
> > int operator[](int ind) {return i[ind];}
> >
> > Then you don't have to think about typecasting.

>
> Except that t1[0] returns a double and t2[0] returns an int.


That's right, because t1 and t2 are objects of different type. It's already
not a technical matter, rather a design one.
OP is supposed to know the purpose of his design. Maybe t1[] being of type
int and t2[] of type double is exactly what he wants.
>
> I'm thinking the other answer is a proxy class.
>


And I'm thinking about Occum's Razor:

"Entities must not be multiplied beyond what is necessary"

Here's an original Latin version:

"Pluralitas non est ponenda sine neccesitate"

Nikolai Borissov


 
Reply With Quote
 
Nikolai Borissov
Guest
Posts: n/a
 
      09-30-2003

Gianni Mariani wrote:

> The counter is the Mariani conjecture.
>
> "unneccessarily complex solutions are a product of adopting
> unneccessarily simple tools".
>
> Here is the orginal in English.
>
> If you use tools too simple for the given task you will end up with a
> solution that is more complex than neccassary.


Good addendum to Occum's Razor )

I've realized what's the problem with returning correct type.
Should be interesting to think about it the other day (just don't have time
now).
I'll analyze your solution too.

Ciao

Nikolai Borissov


 
Reply With Quote
 
Nicolay Korslund
Guest
Posts: n/a
 
      09-30-2003
> I've realized what's the problem with returning correct type.
> Should be interesting to think about it the other day (just don't have time
> now).
> I'll analyze your solution too.
>


Ok, thank you both for the help.

Nicolay

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
operator++ execution order differs between scalar types and classes Zeguudo Zegudissimo C++ 4 09-02-2010 01:34 PM
types of classes in template classes lobequadrat@googlemail.com C++ 5 04-27-2007 05:33 PM
OOP casting classes (using one single variable for similar types of classes) roberts.noah@gmail.com C++ 8 01-07-2006 04:13 PM
Can XSD simple types be derived from complex types? Soren Kuula XML 2 12-01-2005 07:51 PM
ArrayList typecasting from binary SQL data =?Utf-8?B?Smlt?= ASP .Net 1 04-11-2005 04:49 PM



Advertisments