almost newbie like question regarding type conversion



 DEVELOP > c-Plus-Plus > almost newbie like question regarding type conversion

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Anton Lauridsen antoncl do not spam me at hotmail"
Date: 05 Dec 2004 03:41:53 AM
Object: almost newbie like question regarding type conversion
Hi
This is quite embarrsing, having 16+ years of experience in writing c/c++
code, and suddenly I have something I cannot figure out in the language.
Here is the deal:
I want to define two different simple integral types, which are incompatible
with each other, or atleast one simple integral type which is incompatible
with 'int', thus either triggering a compiler waring or error when assigning
between the types.
My problem is really due to bad coding (code I have inherited :-)
the code operates on a structure like this:
struct Node {
int Id;
bla.
bla.
bla.
};
and arrays of struct Node:
struct Node data[300];
Here is the fun part, orginally there was no difference between an index
into the array and the value of Id, i.e. if the Id contained 17, then it
would always be located at data[17].
I am implementing a change, which causes this to be no longer true. Now the
code fails in 10 gazilion places, because it relies heavily on this
invariant, i.e. data[n.Id], in a variariety of subtle different ways are
scattered all over the code.
My plan is to replace the datatype of Id inside the Node struture with a
type and have the compiler find all the places where this invariant is
assumed, e.g.
typedef int NODEID;
struct Node {
NODEID Id;
bla.
bla.
bla.
}:
and then get an error when something like this happens;
for(int i=0; i<xxx; data[i].Id = i++);
any suggestions?
.

User: "adbarnet"

Title: Re: almost newbie like question regarding type conversion 05 Dec 2004 04:04:52 AM
If you are just trying to catch all locations where NODEID.Id is
constructed, assigned, or incremented at compile time, then you could write
a class which dis-allows these methods (declare them private and don't
implment them). That way the compiler will give you each location where it
happens.
If you want the run-time behaviour to throw an exception, then implement the
methods and have them throw.
"Anton Lauridsen" <antoncl do not spam me at hotmail> wrote in message
news:41b2d919$0$216$edfadb0f@dread11.news.tele.dk...

Hi

This is quite embarrsing, having 16+ years of experience in writing c/c++
code, and suddenly I have something I cannot figure out in the language.

Here is the deal:

I want to define two different simple integral types, which are
incompatible with each other, or atleast one simple integral type which is
incompatible with 'int', thus either triggering a compiler waring or error
when assigning between the types.

My problem is really due to bad coding (code I have inherited :-)

the code operates on a structure like this:

struct Node {
int Id;
bla.
bla.
bla.
};

and arrays of struct Node:

struct Node data[300];

Here is the fun part, orginally there was no difference between an index
into the array and the value of Id, i.e. if the Id contained 17, then it
would always be located at data[17].

I am implementing a change, which causes this to be no longer true. Now
the code fails in 10 gazilion places, because it relies heavily on this
invariant, i.e. data[n.Id], in a variariety of subtle different ways are
scattered all over the code.

My plan is to replace the datatype of Id inside the Node struture with a
type and have the compiler find all the places where this invariant is
assumed, e.g.

typedef int NODEID;
struct Node {
NODEID Id;
bla.
bla.
bla.
}:

and then get an error when something like this happens;
for(int i=0; i<xxx; data[i].Id = i++);

any suggestions?




Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
.
User: "Anton Lauridsen antoncl do not spam me at hotmail"

Title: Re: almost newbie like question regarding type conversion 05 Dec 2004 07:47:48 AM
Yep .. thank you
I got so focussed on trying to solve the issue with a typedef rather than a
class.
"adbarnet" <adbarnet@barnet.com> wrote in message
news:41b2dda9$1_2@127.0.0.1...

If you are just trying to catch all locations where NODEID.Id is
constructed, assigned, or incremented at compile time, then you could
write a class which dis-allows these methods (declare them private and
don't implment them). That way the compiler will give you each location
where it happens.

If you want the run-time behaviour to throw an exception, then implement
the methods and have them throw.


"Anton Lauridsen" <antoncl do not spam me at hotmail> wrote in message
news:41b2d919$0$216$edfadb0f@dread11.news.tele.dk...

Hi

This is quite embarrsing, having 16+ years of experience in writing c/c++
code, and suddenly I have something I cannot figure out in the language.

Here is the deal:

I want to define two different simple integral types, which are
incompatible with each other, or atleast one simple integral type which
is incompatible with 'int', thus either triggering a compiler waring or
error when assigning between the types.

My problem is really due to bad coding (code I have inherited :-)

the code operates on a structure like this:

struct Node {
int Id;
bla.
bla.
bla.
};

and arrays of struct Node:

struct Node data[300];

Here is the fun part, orginally there was no difference between an index
into the array and the value of Id, i.e. if the Id contained 17, then it
would always be located at data[17].

I am implementing a change, which causes this to be no longer true. Now
the code fails in 10 gazilion places, because it relies heavily on this
invariant, i.e. data[n.Id], in a variariety of subtle different ways are
scattered all over the code.

My plan is to replace the datatype of Id inside the Node struture with a
type and have the compiler find all the places where this invariant is
assumed, e.g.

typedef int NODEID;
struct Node {
NODEID Id;
bla.
bla.
bla.
}:

and then get an error when something like this happens;
for(int i=0; i<xxx; data[i].Id = i++);

any suggestions?







Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com

.



  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER