U
Ulrich Heinen
Let's say I have two classes A and B with similar goals but completely
different implementations. In my application code I want to use class A
exclusively, but instances of B should also be accepted. This can be
achieved when B provides an appropriate cast operator, e.g.
class A {
public:
/* ... */
protected:
/* ... */
};
class B {
public:
/* ... */
operator A() const;
protected:
/* ... */
};
void someMethod(const A & a) {
/* ... */
}
void someOtherMethod() {
A a;
B b;
someMethod(a);
someMethod(b); // invokes the cast operator to make A from B
}
You get the point. Now, when classes A and B are in two different libraries,
the introduction of the cast operator creates a dependency libB -> libA.
This could be avoided if the cast operator resides outside class B, i.e. in
the actual application. Is there a way to do this? I've tried
operator A(const B &) {
/* ... */
}
but this is apparently wrong. Any hints?
Thanks,
Ulrich Heinen
different implementations. In my application code I want to use class A
exclusively, but instances of B should also be accepted. This can be
achieved when B provides an appropriate cast operator, e.g.
class A {
public:
/* ... */
protected:
/* ... */
};
class B {
public:
/* ... */
operator A() const;
protected:
/* ... */
};
void someMethod(const A & a) {
/* ... */
}
void someOtherMethod() {
A a;
B b;
someMethod(a);
someMethod(b); // invokes the cast operator to make A from B
}
You get the point. Now, when classes A and B are in two different libraries,
the introduction of the cast operator creates a dependency libB -> libA.
This could be avoided if the cast operator resides outside class B, i.e. in
the actual application. Is there a way to do this? I've tried
operator A(const B &) {
/* ... */
}
but this is apparently wrong. Any hints?
Thanks,
Ulrich Heinen