Discussion:
member function overwrite
(too old to reply)
didan
2008-06-17 18:37:17 UTC
Permalink
I want to overwrite AnsiString's ToDouble() member funtion. can
you tell me how to do it?
Thanks!
Remy Lebeau (TeamB)
2008-06-17 18:49:26 UTC
Permalink
Post by didan
I want to overwrite AnsiString's ToDouble() member funtion.
Why? What exactly are you trying to accomplish?
Post by didan
can you tell me how to do it?
Sorry, but you cannot. The compiler will not allow you to derive a new
class from AnsiString, and thus you cannot override ToDouble() (which you
can't do anyway since it is not virtual).


Gambit
didan
2008-06-17 20:14:26 UTC
Permalink
Post by Remy Lebeau (TeamB)
Post by didan
I want to overwrite AnsiString's ToDouble() member funtion.
Why? What exactly are you trying to accomplish?
we found that when we use ToDouble to convert a AnsiString
which includes not only numbers but also letters, it will give error. However, it damages the memory after we hit this error
a few times. So we like to check the string first before we do
ToDouble(). since ToDouble() is all over our codes, so we would
like to see if there is a simple way.
Remy Lebeau (TeamB)
2008-06-17 21:52:34 UTC
Permalink
Post by didan
we found that when we use ToDouble to convert a AnsiString
which includes not only numbers but also letters, it will give error.
As it should be.
Post by didan
However, it damages the memory after we hit this error
a few times.
It shouldn't be since the only memory written to is the 8 bytes for the
return value, and the Exception object that is created to report the
conversion error. What kind of damage are you referring to exactly, and how
do you know it is ToDouble()'s fault in the first place?
Post by didan
So we like to check the string first before we do ToDouble().
Have a look at TryStrToFloat(). It does the exact same conversion that
ToDouble() does, but will not raise an exception if an error occurs. It
will return a bool indicating success/fail, and if successful will also
return the converted value as well.


Gambit
Rene
2008-06-19 11:58:51 UTC
Permalink
Post by Remy Lebeau (TeamB)
Post by didan
I want to overwrite AnsiString's ToDouble() member funtion.
Why? What exactly are you trying to accomplish?
Post by didan
can you tell me how to do it?
Sorry, but you cannot. The compiler will not allow you to derive a new
class from AnsiString
Hi Gambit,

I have just learned about inheritance and saw this message and got curious.
Why is it that one cannot derive a class from AnsiString?

Yours sincerely,
Rene
Remy Lebeau (TeamB)
2008-06-19 17:06:04 UTC
Permalink
Post by Rene
Why is it that one cannot derive a class from AnsiString?
Because Borland explicitally does not allow it. Look at the declaration of
AnsiString in dstring.h:

class RTL_DELPHIRETURN AnsiString
{
...
};

RTL_DELPHIRETURN is a precompiler macro in sysmac.h that expands to
__declspec(delphireturn). If you look up __declspec in the documentation,
you will see the following statement:

__declspec(delphireturn)

The delphireturn argument is for internal use only by the VCL and CLX in
C++Builder. It is used for declarations of classes that were created in
C++Builder to support Object Pascal's built-in data types and language
constructs because they do not have a native C++ type. These include
Currency, AnsiString, Variant, TDateTime, and Set. The delphireturn argument
marks C++ classes for VCL- or CLX-compatible handling in function calls as
parameters and return values. This modifier is needed when passing a
structure by value to a function between Object Pascal and C++.

One consequence of that is any type declared with __declspec(delphireturn)
cannot be derived from, in order to maintain the compatibility between and
C++. Inheritance changes the structure of the type being derived. If
AnsiString could be derived from, it would not be compatible with Object
Pascal anymore.


Gambit
Rene
2008-06-20 19:20:48 UTC
Permalink
Post by Remy Lebeau (TeamB)
Post by Rene
Why is it that one cannot derive a class from AnsiString?
Because Borland explicitally does not allow it. Look at the declaration
class RTL_DELPHIRETURN AnsiString
{
...
};
RTL_DELPHIRETURN is a precompiler macro in sysmac.h that expands to
__declspec(delphireturn). If you look up __declspec in the documentation,
__declspec(delphireturn)
The delphireturn argument is for internal use only by the VCL and CLX
in C++Builder. It is used for declarations of classes that were created in
C++Builder to support Object Pascal's built-in data types and language
constructs because they do not have a native C++ type. These include
Currency, AnsiString, Variant, TDateTime, and Set. The delphireturn
argument marks C++ classes for VCL- or CLX-compatible handling in function
calls as parameters and return values. This modifier is needed when
passing a structure by value to a function between Object Pascal and C++.
One consequence of that is any type declared with __declspec(delphireturn)
cannot be derived from, in order to maintain the compatibility between and
C++. Inheritance changes the structure of the type being derived. If
AnsiString could be derived from, it would not be compatible with Object
Pascal anymore.
Thank You for this explanation.

Yours sincerely,
Rene

Loading...