473,785 Members | 2,400 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

External cast operators

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(cons t 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

Jul 22 '05 #1
2 7316
Ulrich Heinen wrote:
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.
<snip/>
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 );

Jul 22 '05 #2
Ulrich Heinen wrote:
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(cons t 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.
I've tried

operator A(const B &) {
/* ... */
}

but this is apparently wrong. Any hints?

Thanks,

Ulrich Heinen


Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
4997
by: Ali R. | last post by:
Hi guys, I have a string class, which is a wrapper for a char * with a few operators, like + What I am trying to do is reduce the number of overloaded operators so for insteance class String { friend String operator + (const String &Left,const char *Right);
5
1509
by: Gernot Frisch | last post by:
Hi, I have a problem. I want to be able to write this: a = b + d + c; Where a can be a double or class A; b,d,c can each indiviually be one of these: double, class A or class B. The result of an addition is always class A, or double (double + double can't be changed).
36
6695
by: MSG | last post by:
The answer is neither. Use macros. #define ALLOC(size, type) ((type) *) malloc((size) * sizeof(type)) #define NEW(type, name, size) (type) * (name) = ALLOC((size), (type)) They are both type-safe and concise. Compare: NEW(int, x, 1000);
8
4108
by: GlennDoten | last post by:
I just happened to be looking through the implementation of the System.Version class in the SSCLI and one of the constructors starts like this: public Version(String version) { if ((Object) version == null) throw new ArgumentNullException("version"); Does anyone have any ideas why the string parameter is being cast to an object prior to checking for null? Is there some circumstance where not
17
2677
by: Hazz | last post by:
In this sample code of ownerdraw drawmode, why does the '(ComboBox) sender' line of code need to be there in this event handler? Isn't cboFont passed via the managed heap, not the stack, into this cboFont_DrawItem event handler? Why does it need to be cast? -hazz ,................. cboFont.Items.AddRange(FontFamily.Families); } private void cboFont_DrawItem(object sender,
5
2367
by: Frederick Gotham | last post by:
Before I begin, here's a list of assumptions for this particular example: (1) unsigned int has no padding bits, and therefore no invalid bit- patterns or trap representations. (2) All types have the same alignment requirements. (3) sizeof(double) >= sizeof(unsigned) ===================================== We can cast from double to unsigned int as follows:
8
5495
by: ManicQin | last post by:
Hi, I've browsed the STL code a bit and stumble upon the next line (in the operator << overload of both long and short outputs - the line is for checking does the input is a manipulator) long _Tmp = (_Bfl == ios_base::oct || _Bfl == ios_base::hex) ? (long)(unsigned short)_Val //<-This Line : (long)_Val; Is there any profound reason why they "Double" casting the _Val? Thanks.
16
4719
by: JoseMariaSola | last post by:
How may operators and operands does (typename) expression has? I'd say one operator, the cast operator, and two operands: typename and expression. But everywhere I read, cast is categorized as an unary operator. Why is that? Is it just a syntax cotegory? Thanks.
7
3951
by: * Tong * | last post by:
Hi, I couldn't figure out how to properly type cast in this case: $ cat -n type_cast.c 1 #include <stdio.h> 2 3 typedef unsigned char Byte; 4 typedef signed char Small_Int; 5
0
9646
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10157
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10096
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7504
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6742
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5514
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4055
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2887
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.