Board index » delphi » Simple OO problem using Object Pascal?

Simple OO problem using Object Pascal?

Hi,

I'm new to Delphi and I've got what I consider to be a simple OO
problem, but for the life of me, I can't figure out a simple way of
programming it in Delphi. I want an object that will do perform a
service using itself and another object passed to it, and return a
result that is another object of the same type.

TObj = class
public
        function doit(aObj: TObj): TObj;
end;

function TObj.doit(aObj: TObj): TObj;
begin
        { do stuff and return resulting new object...}
end;

I'd also like to be able to concatenate the function as in:

doit(doit(doit(obj1)));

The only way I can see for the doit function to return an object is
for it to create a new object and return it. But then I can't figure
out how to free it in the case of concatenating the function call.

I can see a few ways to do this but they all seem relatively kludgy or
require giving up some capabilities.

Thanks.

Bill Svirsky  svir...@rytebyte.com

 

Re:Simple OO problem using Object Pascal?


Quote
Bill Svirsky wrote in message <34aab735.60653...@news.rytebyte.com>...
>The only way I can see for the doit function to return an object is
>for it to create a new object and return it.

that part is easy.

Quote
> But then I can't figure
>out how to free it in the case of concatenating the function call.

You can't. We had this discussion early last year in
the Borland forums. Check 'Result mysteries' via dejanews,
or check the borland fora directly with your newsserver
set to forums.borland.com in the objectpascal newsgroup.
--
Grace + Peace | Peter N Roth | Engineering Objects Int'l
Author: "Creating a Robust Type-Safe TList", Delphi Developer Oct 97
Visit our website at http://www.inconresearch.com/eoi
"On the internet, nobody knows you're a dog." - P Steiner

Re:Simple OO problem using Object Pascal?


Hi Bill,

**please** don't feel offended by this reply, but I think that before trying
to accomplish what you describe, you should have a reasonable understanding
of what OOP is, and what it is *not*. Many people get trapped by RAD-tools
(Delphi ,VC++, VB) all the time, because making the user interface goes so
wonderfully fast & easy, but then.... it suddenly stops. Code must be
written to make the app do something _useful_ . Then, frustration follows...
Most of the people in these friendly Delphi newsgroups will go at great
lengths to help anyone out with his/her problem, but you must do some of the
footwork yourself.

Try to study OOP with a simple TObject with some simple datafields, some
simple methods , and *play* with it. Study the books that come with Delphi.
They are incomplete and don't go into great depth, but as a starting point,
they are very reasonable, and contain many useful examples. Also, study the
demos that come with Delphi.
Then, if you have a specific problem, post it here again, you *will* get
answers!

Best regards & good luck!
--
Dirk Claessens
_____________________________________________
Office : <dirk.claessens...@belgium.agfa.com>
Home   : <dirk.claess...@village.uunet.be>
_____________________________________________

Quote
Bill wrote in message <34aab735.60653...@news.rytebyte.com>...

|
|I'm new to Delphi and I've got what I consider to be a simple OO
|problem, but for the life of me, I can't figure out a simple way of
|programming it in Delphi. I want an object that will do perform a
|service using itself and another object passed to it, and return a
|result that is another object of the same type.
[snip]
|The only way I can see for the doit function to return an object is
|for it to create a new object and return it. But then I can't figure
|out how to free it in the case of concatenating the function call.
[snip]

Re:Simple OO problem using Object Pascal?


Quote
Dirk Claessens wrote:

[...]

Quote
> Try to study OOP with a simple TObject with some simple datafields, some
> simple methods , and *play* with it. Study the books that come with Delphi.
> They are incomplete and don't go into great depth, but as a starting point,
> they are very reasonable, and contain many useful examples. Also, study the
> demos that come with Delphi.
> Then, if you have a specific problem, post it here again, you *will* get
> answers!

        Seems like all good advice in general. But he _did_ have a very
specific question, seems to me.

--
David Ullrich

sig.txt not found

Re:Simple OO problem using Object Pascal?


why not pass by reference instead?

On Wed, 31 Dec 1997 21:48:10 GMT, svir...@rytebyte.com (Bill Svirsky)
wrote:

 >Hi,
 >
 >I'm new to Delphi and I've got what I consider to be a simple OO
 >problem, but for the life of me, I can't figure out a simple way of
 >programming it in Delphi. I want an object that will do perform a
 >service using itself and another object passed to it, and return a
 >result that is another object of the same type.
 >
 >TObj = class
 >public
 >   function doit(aObj: TObj): TObj;
 >end;
 >
 >function TObj.doit(aObj: TObj): TObj;
 >begin
 >   { do stuff and return resulting new object...}
 >end;
 >
 >I'd also like to be able to concatenate the function as in:
 >

Quote
>doit(doit(doit(obj1)));

>The only way I can see for the doit function to return an object is
>for it to create a new object and return it. But then I can't figure
>out how to free it in the case of concatenating the function call.

>I can see a few ways to do this but they all seem relatively kludgy or
>require giving up some capabilities.

>Thanks.

>Bill Svirsky  svir...@rytebyte.com

---------------------http://w3.nai.net/~perfecto--------------------------
Perry Fecteau, Republican
The new pages go up January 1,1998
---------------------http://w3.nai.net/~perfecto--------------------------

Re:Simple OO problem using Object Pascal?


Hmmm.  I may well be missing something, but I'll take a stab.

Do you want to discard the parameter object aObj immediately after
constructing the new object?  (In other words, are you basically
"subsuming" or "embedding" aObj into the new object?).  Why not just do
"aObj.Free" in TObj.DoIt?

Otherwise, I guess you're wanting to use each aObj in some way after
constructing the new object.  Try:

TObj = class
private
        Child: TObj;
end;

function TObj.doit(aObj: TObj): TObj;
begin
        Child := aObj;
         { do stuff and return resulting new object...}
end;

destructor TObj.Destroy;
begin
        {possible other stuff}
        Child.Free;
end;

That way, if you do a .Free on the result of your concatenated calls,
all of the "child" TObjs will get destroyed OK also, by recursion, the
same way they were created.

By the way, seems like it would be clearer if function DoIt was
constructor DoIt, rather can calling a constructor (as I assume you do)
from within DoIt.  But that may be personal preference on my part...

Quote
Bill Svirsky wrote:

> Hi,

> I'm new to Delphi and I've got what I consider to be a simple OO
> problem, but for the life of me, I can't figure out a simple way of
> programming it in Delphi. I want an object that will do perform a
> service using itself and another object passed to it, and return a
> result that is another object of the same type.

> TObj = class
> public
>         function doit(aObj: TObj): TObj;
> end;

> function TObj.doit(aObj: TObj): TObj;
> begin
>         { do stuff and return resulting new object...}
> end;

> I'd also like to be able to concatenate the function as in:

> doit(doit(doit(obj1)));

> The only way I can see for the doit function to return an object is
> for it to create a new object and return it. But then I can't figure
> out how to free it in the case of concatenating the function call.

> I can see a few ways to do this but they all seem relatively kludgy or
> require giving up some capabilities.

> Thanks.

> Bill Svirsky  svir...@rytebyte.com

Re:Simple OO problem using Object Pascal?


Possibly, but to me it *seemed* that Bill Svirsky was having very basic
problems with Delphi and/or OOP. Although he said he's new to Delphi, he may
very well have a degree in computer science. I just think it's not a good
idea to try DoIt(DoIt(DoIt(Obj))) - I may have forgotten a bracket- as a
first exercise.
But then, who am I...

regards!
--
Dirk Claessens
_____________________________________________
Office : <dirk.claessens...@belgium.agfa.com>
Home   : <dirk.claess...@village.uunet.be>
_____________________________________________
David Ullrich heeft geschreven in bericht
<34ABE621.1...@math.okstate.edu>...
[...]
| Seems like all good advice in general. But he _did_ have a very
|specific question, seems to me.
|
|--
|David Ullrich
|
|sig.txt not found

Re:Simple OO problem using Object Pascal?


Quote
Dirk Claessens wrote:

> Possibly, but to me it *seemed* that Bill Svirsky was having very basic
> problems with Delphi and/or OOP. Although he said he's new to Delphi, he may
> very well have a degree in computer science. I just think it's not a good
> idea to try DoIt(DoIt(DoIt(Obj))) - I may have forgotten a bracket- as a
> first exercise.
> But then, who am I...

        Yes yes yes - if I didn't agree with almost everything you'd said
I wouldn't have said "Seems like all good advice in general" the way I did.
Was just commenting specifically on your "if you have a specific problem" -
he did have a very specific problem. (Yes, I agree with everyone else that
the best solution here is to avoid the problem.)

--
David Ullrich

sig.txt not found

Other Threads