473,503 Members | 2,150 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Creating mutually non-convertible yet identical classes

The problem I am facing is that an integer is the ideal representation
for many different classes I am using. I want to write overloaded
functions that are able to differentiate between the classes without
mutual interference. The naive, yet wrong, solution is:

typedef int A;
typedef int B;

void f(A a);
void f(B b); // Error

since typedef does not introduce a new type. In addition, I want the
naked expression "b = a" to error out during compile time.
The solution that I have implemented looks like this:

#define DECLARE_UNIQUE_CLASS_ALIAS(_ALIAS, _BASE)\
struct _ALIAS\
{\
_BASE value;\
_ALIAS(const _BASE& v)\
{\
this->value = v;\
}\
operator _BASE() const\
{\
return this->value;\
}\
};

DECLARE_UNIQUE_CLASS_ALIAS(A, int)
DECLARE_UNIQUE_CLASS_ALIAS(B, int)

This has the desired effect, but I am not happy with it because:
1. It uses macros, and is not debuggable.
2. Since the machine code for each alias is identical, and this will
be used a lot, this code will not be optimized by the compiler.

Could someone could suggest a solution that does not have the defects
that my code has?

Nov 3 '07 #1
4 1576
re****@gmail.com wrote:
The problem I am facing is that an integer is the ideal representation
for many different classes I am using. I want to write overloaded
functions that are able to differentiate between the classes without
mutual interference. The naive, yet wrong, solution is:

typedef int A;
typedef int B;

void f(A a);
void f(B b); // Error

since typedef does not introduce a new type. In addition, I want the
naked expression "b = a" to error out during compile time.
[..]

Could someone could suggest a solution that does not have the defects
that my code has?
Use enums, maybe?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 3 '07 #2
On Nov 3, 9:22 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
red...@gmail.com wrote:
The problem I am facing is that an integer is the ideal representation
for many different classes I am using. I want to write overloaded
functions that are able to differentiate between the classes without
mutual interference. The naive, yet wrong, solution is:
typedef int A;
typedef int B;
void f(A a);
void f(B b); // Error
since typedef does not introduce a new type. In addition, I want the
naked expression "b = a" to error out during compile time.
[..]
Could someone could suggest a solution that does not have the defects
that my code has?

Use enums, maybe?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask- Hide quoted text -

- Show quoted text -
I think for some cases, enums will work fine. The problem with them is
that you are at the mercy of the compiler's internal representation of
enum. In particular, if the integral type to be overloaded is int64
and the compiler maxes enum out to 32 bit precision, the code will
fail.
Also, the enum solution only works for integers and can not be
generalized to any other type. I was hoping for some generic template,
non-macro based solution.

Nov 3 '07 #3
re****@gmail.com wrote:
The problem I am facing is that an integer is the ideal representation
for many different classes I am using. I want to write overloaded
functions that are able to differentiate between the classes without
mutual interference. The naive, yet wrong, solution is:

typedef int A;
typedef int B;

void f(A a);
void f(B b); // Error

since typedef does not introduce a new type. In addition, I want the
naked expression "b = a" to error out during compile time.

How about something like

template <typename T, typename V>
struct Unique
{
V value;

Unique( const V& value = V() ) : value(value) {}

operator V() const { return value; }
};

struct A_; typedef Unique<A_,intA;
struct B_; typedef Unique<B_,intB;

void f(A a);
void f(B b);

int main() {
A a;
B b;

a = 21; // OK
a = b; // Error
}

--
Ian Collins.
Nov 3 '07 #4
On Nov 3, 11:24 am, Ian Collins <ian-n...@hotmail.comwrote:
red...@gmail.com wrote:
The problem I am facing is that an integer is the ideal representation
for many different classes I am using. I want to write overloaded
functions that are able to differentiate between the classes without
mutual interference. The naive, yet wrong, solution is:
typedef int A;
typedef int B;
void f(A a);
void f(B b); // Error
since typedef does not introduce a new type. In addition, I want the
naked expression "b = a" to error out during compile time.

How about something like

template <typename T, typename V>
struct Unique
{
V value;

Unique( const V& value = V() ) : value(value) {}

operator V() const { return value; }

};

struct A_; typedef Unique<A_,intA;
struct B_; typedef Unique<B_,intB;

void f(A a);
void f(B b);

int main() {
A a;
B b;

a = 21; // OK
a = b; // Error

}

--
Ian Collins.
Thank you. This is precisely what I was looking for.

Nov 3 '07 #5

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

Similar topics

5
2806
by: Doug Baroter | last post by:
Hi, DDL: -- create table #task (taskID int identity(1,1) primary key, taskName varchar(25) unique, taskCompleteDate dateTime, taskComplete bit default(0)); /* Business Rules: a) if...
5
1861
by: wooks | last post by:
I have defined a schema with an xsd:choice element for 2 mutually exclusive fields. When both are present I get an error which is good, but what is not so good is the error message which says...
6
6092
by: owen | last post by:
Generally speaking, what does it mean when I see a "button" with red text showing this message instead of the control I've dragged onto the web form in Design View.? (But the page works fine at...
3
459
by: softengine | last post by:
Can and how do you alter a data view to include a look up field from another data table? The data table of the dataview only has the key, the value I need is in another data table. Can and how...
5
3703
by: WertmanTheMad | last post by:
Ive been playing with this for a few days and thought I might thow it out for seggestions. I have Several Queries that need counts returned The Queries are Mutually Exclusive meaning whatever...
14
1548
by: G Patel | last post by:
Pg. 140 of K&R2 shows an example of mutually referential structure declarations... struct t { struct s *p; }; struct s {
22
2716
by: ypjofficial | last post by:
Is there any possibility of invoking the member functions of a class without creating an object (or even a pointer to ) of that class. eg. #include <iostream.h> class test { public: void...
7
2323
by: Joe Van Dyk | last post by:
Hi, Say I have: class Latitude { friend bool operator==(const Latitude& lhs, const Latitude& rhs); friend bool operator<(const Latitude& lhs, const Latitude& rhs); friend std::ostream&...
2
3363
by: arun | last post by:
Hi Can any one suggest me how to access the mutually exclusive check box extender in a table of 7 rows by 14 columns. Only one can be selected in each row. Other than checking each one with if...
10
11385
by: Nathan Sokalski | last post by:
How do I create a new System.Drawing.Imaging.ColorPalette? ColorPalette does not have a constructor, and the Entries property is ReadOnly. How are we supposed to specify a palette other than the...
0
7205
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,...
0
7349
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...
1
7008
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...
0
5594
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4688
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...
0
3177
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3168
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1521
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 ...
0
399
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...

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.