External cast operators

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
 
J

Jeff Schwab

Ulrich said:
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.

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?

<snip/>

The usual approach is to derive A and B publicly from an abstract class
encapsulating their common interface.

struct A { /* ... */ };
struct A_impl_1: A { /* ... */ };
struct A_impl_2: B { /* ... */ };

void some_method( A const& a );
 
R

Rolf Magnus

Ulrich said:
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.

You mean a conversion operator. Usually, one would define a conversion
constructor instead of a conversion operator though, unless you cannot
add a constructor to the target type (e.g. because it's part of a
library you use or because it's a built-in type).
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?

You can't make non-member conversion operators.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,242
Latest member
KendrickKo

Latest Threads

Top