Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C class to delphi conversion help needed 1

Status
Not open for further replies.

whosrdaddy

Vendor
Mar 11, 2003
4,231
BE
I have the following class in C(++, I think)


Code:
#DisableSH
[navy][i]// for automatic syntax highlighting see faq102-6487 
[/i][/navy][b]class[/b] PASN_Integer : [b]public[/b] PASN_ConstrainedObject
[navy][i]{
    PCLASSINFO(PASN_Integer, PASN_ConstrainedObject);
  public:
    PASN_Integer(unsigned val = 0);
    PASN_Integer(unsigned tag, TagClass tagClass, unsigned val = 0);

    PASN_Integer & operator=(unsigned value);
    operator unsigned() const { return value; }[/i][/navy]
    unsigned GetValue() [b]const[/b] [navy][i]{ return value; }[/i][/navy]
    void SetValue(unsigned v) [navy][i]{ operator=(v); }[/i][/navy]

    [b]virtual[/b] Comparison Compare([b]const[/b] PObject & obj) [b]const[/b];
    [b]virtual[/b] PObject * Clone() [b]const[/b];
    [b]virtual[/b] void PrintOn(ostream & strm) [b]const[/b];

    [b]virtual[/b] void SetConstraintBounds(ConstraintType [b]type[/b], int lower, unsigned upper);
    [b]virtual[/b] PString GetTypeAsString() [b]const[/b];
    [b]virtual[/b] PINDEX GetDataLength() [b]const[/b];
    [b]virtual[/b] BOOL Decode(PASN_Stream &);
    [b]virtual[/b] void Encode(PASN_Stream &) [b]const[/b];

[teal]#[/teal]ifdef P_INCLUDE_PER
    BOOL DecodePER(PPER_Stream & strm);
    void EncodePER(PPER_Stream & strm) [b]const[/b];
[teal]#[/teal]endif
...


the part I have problems with, is the "operator" part.
It seems that per operator type (like ==, +=, <, >, and so on) you can include a method function.
How would I do this in Delphi?

like in this case the "operator=" would be something like Assign in Delphi? or is this the name of the method?

Can someone clarify this for me? I Don't have the time to read a book about C++ and I need to know fast!

TIA!

//Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
It seems that per operator type (like ==, +=, <, >, and so on) you can include a method function.
How would I do this in Delphi?

I know a *little* C++ (and I don't know this could possibly be Java or C# that was posted, by the looks of things (like the phrase "const")?). I'm not sure exactly what you're asking. To read what I see above and your question and to rephrase it into a more Delphi-like state, are you asking how to define a constant within a class by using a method that exists within the same class? Or if this is what it could possibly be? Just trying to help my thought process (and maybe others) on where to look to help you out.
 
Tonhu,

the operator overloading thing is exactly what I was looking for!

Thanks a million! (and a star offcourse)

[thumbsup]


so the line

PASN_Integer & operator=(unsigned value);

would be in delphi something like :

class operator Implicit(Value : Dword) : TASN_Integer;

Please correct me if I'm wrong!

//Daddy






-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Ok, making progress here. have some more difficulties:

look at this C++ class: (it is really C++ Glenn9999)

Code:
/** Class for ASN Boolean type.
*/
class PASN_Boolean : public PASN_Object
{
    PCLASSINFO(PASN_Boolean, PASN_Object);
  public:
    PASN_Boolean(BOOL val = FALSE);
    PASN_Boolean(unsigned tag, TagClass tagClass, BOOL val = FALSE);

    PASN_Boolean & operator=(BOOL v) { value = v; return *this; }
    operator BOOL() const { return value; }
    BOOL GetValue() const { return value; }
    void SetValue(BOOL v) { value = v; }

    virtual Comparison Compare(const PObject & obj) const;
    virtual PObject * Clone() const;
    virtual void PrintOn(ostream & strm) const;

    virtual PString GetTypeAsString() const;
    virtual PINDEX GetDataLength() const;
    virtual BOOL Decode(PASN_Stream &);
    virtual void Encode(PASN_Stream &) const;

  protected:
    BOOL value;
};

the hard parts are these :

a)-> operator BOOL() const { return value; }

in delphi this would mean something like:

Code:
var v : TASN_Boolean;
...
if v then DoSomething

//only way around would be

if v.value then DoSomething
I would rather avoid this thing, because it means harder class conversion and more translation errors
if I could have the Value property marked as default, that would solve the problem. (but it is only available for arrays :( )

b)-> virtual Comparison Compare(const PObject & obj)

look at the implementation:

PObject::Comparison PASN_Boolean::Compare(const PObject & obj) const
{
return value == ((const PASN_Boolean &)obj).value ? EqualTo : GreaterThan;
}

Don't have a clue how to translate this in a fashionable way...

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
again, any type of help will be appreciated!

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
look at this C++ class: (it is really C++ Glenn9999)

Never can tell the circumstances, I guess. It just had a lot of unfamiliar things in it that I didn't remember from when I dealt with the C++ beast once upon a time. So thought maybe it was some newer C++ based variant.

return value == ((const PASN_Boolean &)obj).value ? EqualTo : GreaterThan;

I do recognize this - I'm not sure there's is an incredibly fashionable way to handle this thing in Delphi. What I do remember quite well from C++ is that there's some things that tend to say and imply a whole lot and this is one of them. To that end, I always had to have a manual handy because I couldn't remember all the nuances.

Since Delphi has its roots in Pascal, and Pascal was intended as a teaching language, the syntax is of a WYSIWYG nature, which this is most certainly not. Of course, that doesn't

Off the top of my head if I were to attempt it:
Code:
{ result := expr1 ? expr2 : expr3 }
if expr1 = 0 then
  result := expr3
else
  result := expr2;

For those that are curious, this link documents what he's doing:

Hopefully I got that reasonably correct and that helps some.
 
of course that doesn't" in my last post:

Of course, that doesn't mean that there isn't some Delphi equivalent to it that I haven't run across yet.

:)
 
I am aware of the C syntax, the problem I am having is the EqualTo and GreaterThan things...

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
only solution I can come with is this:

Code:
TComparison = (LessThan, EqualTo, GreatherThan)

...
function Compare(Object : Tobject) : TComparison;
begin
 if TASN_Boolean(Object).Value then 
  Result:=EqualTo
 else
  Result:=GreaterThan;
end;

Thanks for your help anyway!

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top