Link to home
Start Free TrialLog in
Avatar of glebspy
glebspy

asked on

Using "using"

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
Avatar of Axter
Axter
Flag of United States of America image

Try the following:

#include <iostream>

using namespace std;
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?
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;
}

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;
ASKER CERTIFIED SOLUTION
Avatar of Axter
Axter
Flag of United States of America image

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
Avatar of glebspy
glebspy

ASKER

Thanks Axter, I'll try that stuff soon.
Avatar of glebspy

ASKER

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