surfcode
surfcode

Reputation: 465

Type conversion to another class with namespace?

Normally, one could write a conversion operator to convert to another class like

struct A {};

struct B
{
    operator A()
    {
        return A();
    } 
};

Now, what if A struct has a namespace different than B:

namespace mars {
struct A {};
}

namespace jupiter {
struct B
{
    operator A()  //??
    {
        return A();
    } 
};
}

What should the operator A() statement become?

Upvotes: 1

Views: 1491

Answers (2)

Marco A.
Marco A.

Reputation: 43662

You can simply fully qualify the names

namespace mars {
  struct A {};
}

namespace jupiter {
  struct B
  {
    operator mars::A()
    {
      return mars::A();
    } 
  };
}

As a suggestion: try not to do something like

using namespace mars;

in the global scope: that would pollute it and render things more complex when the application grows (e.g. name clashing). Especially for the std namespace, it is often more desirable to fully qualify names to avoid this phenomenon.

Upvotes: 3

utnapistim
utnapistim

Reputation: 27375

The operator should become:

namespace jupiter {
struct B
{
    operator mars::A()
    {
        return mars::A();
    } 
};
}

Upvotes: 2

Related Questions