We help IT Professionals succeed at work.

Using "using"

glebspy
glebspy asked
on
Why doesn't this compile? What's the correct
syntax for what I'm trying to do?

#include <iostream>

class A{
public:
  operator float(void)const{return 0;}
};

class B{
public:
  operator float(void)const{return 1;}
};

class C:
public A,public B
{
public:
  using A::operator float(void)const;
};

int main(void)
{
  C c;
  cout<<float(c)<<endl;
}

//If there are any typos I'm sorry, I don't have
//cut and paste. Please concentrate on the "using"
//issue.

//Thanks
Comment
Watch Question

AxterSenior Software Engineer

Commented:
Try the following:

#include <iostream>

using namespace std;
AxterSenior Software Engineer

Commented:
Oops!
I didn't see the using A::

Using is for namespace.
In your code, A is not a namespace.  It's a class.
So it would be improper to put using, and it would be improper to put the A there as well.

What exactly are you trying to do with the operator float(void)const?

Are you trying to over write it?
AxterSenior Software Engineer

Commented:
OK,
I think I see what you're trying to do.
Try the following:

class A{
public:
     operator float(void)const{return 0;}
};

class B{
public:
     operator float(void)const{return 1;}
};

class C:
public A,public B
{
public:
     operator float(void)const
     {
          return A::operator float();
     }
};

int main(void)
{
     C c;
     cout<<float(c)<<endl;
     return 0;
}

AxterSenior Software Engineer

Commented:
Also try this.

class C: public A,public B
{
public:
     using A::operator float;
};

I believe that is the right syntax for selecting a specific multi inherited function.

BUT!!!
I tried compiling this in VC, and it didn't work.
I think it's a VC bug, because the following does compile, and runs correctly.

class Z: public X,public Y
{
public:
     using X::f;
};

int main(void)
{
     Z z;
     cout<<z.f()<<endl;
Senior Software Engineer
Commented:
Also if you change your code to the following, it will also compile:

class C: public A,public B
{
public:
    using A::operator float;
};

int main(void)
{
C c;
cout<<c.operator float()<<endl; // Use operator float
return 0;
}

Author

Commented:
Thanks Axter, I'll try that stuff soon.

Author

Commented:
Thanks very much
Just for information, I think it is almost certainly a VC++ bug; it compiles and runs just fine with g++.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.