template <>
color<float>::operator GLfloat const * () const
{
return data_;
}
color<float>::operator GLfloat * ()
{
return data_;
}
error C2556: '__thiscall color<float>::operator`float *'(void) const'
: overloaded function differs only by return type from '__thiscall
color<float>::operator`const float *'(void) const'
e:\ogl\tutorials\oo-glbase\glcad\color.h(124) : see
declaration of 'operator`const float *''
Is it me or MSVC (6.0 with sp5) ?
<snipped code>
> error C2556: '__thiscall color<float>::operator`float *'(void) const'
> : overloaded function differs only by return type from '__thiscall
> color<float>::operator`const float *'(void) const'
> e:\ogl\tutorials\oo-glbase\glcad\color.h(124) : see
> declaration of 'operator`const float *''
>
>
> Is it me or MSVC (6.0 with sp5) ?
It's you :)
At least VC6 VC.Net and g++ 3.2 agree. I won't swear about what the standard
says :)
Here's the problem:
1. You need to create the class specialization explicitly.
2. You need to return &data_ not data_.
The usage looks pretty dubiuos to me but the code below will
compile, run, and do what you expect. I wouldn't recommend using it though :)
Looks to unsafe to me. Returning pointers to private arrays is not really a
good idea imho.
Regards /Magnus
#include <iostream>
using namespace std;
typedef float GLfloat[4];
template <class T>
class color
{
T data_[4];
public:
operator GLfloat const * () const
{
throw( "unimplemented conversion" );
}
operator GLfloat * ()
{
throw( "unimplemented conversion" );
}
};
template <>
class color<float>
{
float data_[4];
public:
color<float>()
{
data_[0] = 255;
data_[1] = 255;
data_[2] = 255;
data_[3] = 255;
}
operator GLfloat const * () const;
operator GLfloat * ();
};
color<float>::operator GLfloat const * () const
{
return &data_;
}
color<float>::operator GLfloat * ()
{
return &data_;
}
int main()
{
color<float> myfloatColor;
GLfloat* myGLfloat;
myGLfloat = myfloatColor;
for(int i = 0; i < 4; ++i)
cout << (*myGLfloat)[i] << endl;
return 0;
}
>template <>
>class color<float>
>{
> float data_[4];
>public:
> color<float>()
> {
> data_[0] = 255;
> data_[1] = 255;
> data_[2] = 255;
> data_[3] = 255;
> }
>
> operator GLfloat const * () const;
> operator GLfloat * ();
>};
>
>color<float>::operator GLfloat const * () const
>{
> return &data_;
>}
>
>color<float>::operator GLfloat * ()
>{
> return &data_;
>}
>
>
>int main()
>{
> color<float> myfloatColor;
> GLfloat* myGLfloat;
> myGLfloat = myfloatColor;
>
> for(int i = 0; i < 4; ++i)
> cout << (*myGLfloat)[i] << endl;
>
>
> return 0;
>}
>
Oh yes!! Thanks!
typedef float GLfloat;
>template <class T>
>class color
>{
> T data_[4];
> ................
>public:
> operator GLfloat const * () const
You'd be better off leaving out the implementation entirely, so the
mistake is found at link time.
operator GLfloat const*() const; //unimplemented.
> {
> throw( "unimplemented conversion" );
> }
> operator GLfloat * ()
> {
> throw( "unimplemented conversion" );
> }
> ................
>};
>
>
>template <>
>color<float>::operator GLfloat const * () const
>{
> return data_;
>}
template<>
>color<float>::operator GLfloat * ()
>{
> return data_;
>}
>
>
>error C2556: '__thiscall color<float>::operator`float *'(void) const'
>: overloaded function differs only by return type from '__thiscall
>color<float>::operator`const float *'(void) const'
> e:\ogl\tutorials\oo-glbase\glcad\color.h(124) : see
>declaration of 'operator`const float *''
>
>
>Is it me or MSVC (6.0 with sp5) ?
MSVC 6.0. It is legal to specialise member functions independently of
the surrounding class.
Compiles fine on gcc 3.2 and como 4.3, with the changes above.
Tom