P
Paulo Matos
Hi all,
Guess I wish to do some parsing for a calculator which might include
rational numbers.
So I can have integers (sequence of digits possibly started by -) and
rationals (two integers separated by '/').
I did a NumberWrapper which makes the use of a number, be it an integer
or rational transparent to the user.
Number(1, 2); represents 1/2
Number(2); represents 1
First issue, I have something like this in number (or would like to):
class Number {
....
private:
enum numberType {INTEGER, RATIONAL};
numberType type;
union {
int integer;
Rational rational; // Class defined by me...
};
};
Problem is Rational has non-trivial constructors so it just can't be
there. One way it to remove the union and just have an int and a
rational, never both are used at the same time (that wastes space but
saves me a lot of trouble).
Another way is to have a void* and then based on type I just cast it to
int or to rational as I need, obviously with caution, deleting and
freeing what I need. Well, this seems a lot of trouble!
Another is to just represent an integer as a rational with denominator
1 and forget the int or wrapper at all and use rationals all the way.
Another issue has to do with how to structure things around the
parsing. Bison uses unions for communication with flex so I have a
Number * in the union and whenever flex finds a rational or number,
allocates a new number and bison get's it so I can use it in parsing.
struct parsingState {
union {
Number *num;
};
ParsingStructureType type;
};
Problem is when I need to do some computations...
Guess I have Number *num; in the union and I want to update it to
become -num. I have defined operator-() in Number so I do:
parsingState ps;
....
ps->num = - ps->num;
It seems to be that this will leak right? (-ps->num) will allocate
another Number because operator-() is
Number Number:perator-() const;
and I'll lose my previous allocated number.
Any ideas on how I can do this or organize my structures?
Regards,
Paulo Matos
Guess I wish to do some parsing for a calculator which might include
rational numbers.
So I can have integers (sequence of digits possibly started by -) and
rationals (two integers separated by '/').
I did a NumberWrapper which makes the use of a number, be it an integer
or rational transparent to the user.
Number(1, 2); represents 1/2
Number(2); represents 1
First issue, I have something like this in number (or would like to):
class Number {
....
private:
enum numberType {INTEGER, RATIONAL};
numberType type;
union {
int integer;
Rational rational; // Class defined by me...
};
};
Problem is Rational has non-trivial constructors so it just can't be
there. One way it to remove the union and just have an int and a
rational, never both are used at the same time (that wastes space but
saves me a lot of trouble).
Another way is to have a void* and then based on type I just cast it to
int or to rational as I need, obviously with caution, deleting and
freeing what I need. Well, this seems a lot of trouble!
Another is to just represent an integer as a rational with denominator
1 and forget the int or wrapper at all and use rationals all the way.
Another issue has to do with how to structure things around the
parsing. Bison uses unions for communication with flex so I have a
Number * in the union and whenever flex finds a rational or number,
allocates a new number and bison get's it so I can use it in parsing.
struct parsingState {
union {
Number *num;
};
ParsingStructureType type;
};
Problem is when I need to do some computations...
Guess I have Number *num; in the union and I want to update it to
become -num. I have defined operator-() in Number so I do:
parsingState ps;
....
ps->num = - ps->num;
It seems to be that this will leak right? (-ps->num) will allocate
another Number because operator-() is
Number Number:perator-() const;
and I'll lose my previous allocated number.
Any ideas on how I can do this or organize my structures?
Regards,
Paulo Matos