Discussion:
Problem with class.
(too old to reply)
Cooper
2010-03-04 13:42:43 UTC
Permalink
Hello, problem is this: i have two classes for example tclass1, tclass2 so
defined:

tclass1 = class
public
function dosomething2: byte;
procedure dosomething1;
end;

where dosomething1 call dosomething2 in internal block, and:

tclass2 = class (tclass1)
public
function dosomething2: byte;
end;

now, i want that result returned from tclass1 and tclass2 change only
changing function dosomething2; tclass1 and tclass2 are same; but
difference is only from dosomething2.
I have tried but calling procedure dosomething1 from tclass2 it return every
value of dosomething1 of tclass1 and not from dosomething2 of from tclass2
with value from dosomething2 of tclass2.
As i can to solve it?
Forgive for my english or if not explained good, if not right, i try to
explain better.
Thanks very much.
Cooper
2010-03-04 13:52:47 UTC
Permalink
Post by Cooper
Hello, problem is this: i have two classes for example tclass1, tclass2
tclass1 = class
public
function dosomething2: byte;
procedure dosomething1;
end;
tclass2 = class (tclass1)
public
function dosomething2: byte;
end;
now, i want that result returned from tclass1 and tclass2 change only
changing function dosomething2; tclass1 and tclass2 are same; but
difference is only from dosomething2.
I have tried but calling procedure dosomething1 from tclass2 it return
every value of dosomething1 of tclass1 and not from dosomething2 of from
tclass2 with value from dosomething2 of tclass2.
As i can to solve it?
Forgive for my english or if not explained good, if not right, i try to
explain better.
Thanks very much.
Forgotten, in general i have a class base (example: tclass) that receive
data and return value; in this class is present a function where result of
class depend from it.
I need to do more classes, for example: tclass1, tclass2, tclassN eredited
from tclass where change only one function and all other is same of tclass.
So in tclass1 i define function1 and have one result; in tclass2 i define
function2 and have other result etc.
I think that is something similar to "template" if told correctly (sorry if
otherwise); but not understood as to do it.
Can help me, solving this problem? Otherwise i need to copy tclass in
tclass1, tclass2, tclassN changing only function interested; and not think
that is good solution.
Thanks you again.
Hans-Peter Diettrich
2010-03-04 14:18:05 UTC
Permalink
Post by Cooper
Hello, problem is this: i have two classes for example tclass1, tclass2
tclass1 = class
public
function dosomething2: byte;
virtual;
Post by Cooper
procedure dosomething1;
end;
tclass2 = class (tclass1)
public
function dosomething2: byte;
override;
Post by Cooper
end;
now, i want that result returned from tclass1 and tclass2 change only
changing function dosomething2; tclass1 and tclass2 are same; but
difference is only from dosomething2.
As I understand it, you want a *virtual* method dosomething2. See above.

DoDi
Cooper
2010-03-04 16:13:46 UTC
Permalink
Post by Hans-Peter Diettrich
Post by Cooper
Hello, problem is this: i have two classes for example tclass1, tclass2
tclass1 = class
public
function dosomething2: byte;
virtual;
Post by Cooper
procedure dosomething1;
end;
tclass2 = class (tclass1)
public
function dosomething2: byte;
override;
Post by Cooper
end;
now, i want that result returned from tclass1 and tclass2 change only
changing function dosomething2; tclass1 and tclass2 are same; but
difference is only from dosomething2.
As I understand it, you want a *virtual* method dosomething2. See above.
Yeah, thanks very much :))))) solved! again thanks :)))
Cooper
2010-03-04 17:15:02 UTC
Permalink
Post by Hans-Peter Diettrich
As I understand it, you want a *virtual* method dosomething2. See above.
Solved about problem of before, now i have this problem: dosomething2 not
every has same parameters.
I thinked to use: OverLoad on Override / Virtual directive but give me
some problem; where i mistake?
Shortly situation is this:

tclass1 = class
public
function dosomething2: byte; virtual; [1]
procedure dosomething1;
end;

where dosomething1 call dosomething2 in internal block, and:

tclass2 = class (tclass1)
public
function dosomething2 (x, y: real) : byte; override; [2]
end;

Me thinked to add: overload so:

[1] function dosomething2: byte; virtual; overload;
[2] function dosomething2 (x, y: real) : byte; override;
overload;

but not ok :( where mistake?
Thanks again for preciouse help.
Rudy Velthuis
2010-03-04 18:42:56 UTC
Permalink
As I understand it, you want a virtual method dosomething2. See
above.
OverLoad on Override / Virtual directive but give me some problem;
tclass1 = class
public
function dosomething2: byte; virtual; [1]
procedure dosomething1;
end;
tclass2 = class (tclass1)
public
function dosomething2 (x, y: real) : byte; override; [2]
end;
[1] function dosomething2: byte; virtual; overload;
[2] function dosomething2 (x, y: real) : byte; override;
overload;
but not ok :( where mistake?
Well, it is not quite sure what "not ok" means, but all overrides must
- of course - have the same signature, otherwise they can't be called
the same way from the base class. IOW, dosomething1 calls dosomething2
WITOUT parameters. How should it know you override do something MUCH
LATER with 2 parameters?

It can't know that and that is why all overrides must have the same
signature (parameters and return value). That is the only way virtual
functions CAN work.
--
Rudy Velthuis http://rvelthuis.de

"Why yes -- a bulletproof vest."
-- James Rodges, murderer, on his final request before the
firing squad.
Cooper
2010-03-04 18:51:21 UTC
Permalink
Post by Rudy Velthuis
Well, it is not quite sure what "not ok" means, but all overrides must
- of course - have the same signature, otherwise they can't be called
the same way from the base class. IOW, dosomething1 calls dosomething2
WITOUT parameters. How should it know you override do something MUCH
LATER with 2 parameters?
Yes, i have situation where in tclass2 function need other parameters. In
particular i have this error:

[DCC Error] test.pas(20): E2137 Method 'dosomething2' not found in base
class

But about error, if i put function without parameter (as in tclass1) then
work.
Post by Rudy Velthuis
It can't know that and that is why all overrides must have the same
signature (parameters and return value). That is the only way virtual
functions CAN work.
Not understood :( In theory need work?
Rudy Velthuis
2010-03-04 23:03:03 UTC
Permalink
Post by Cooper
Post by Rudy Velthuis
Well, it is not quite sure what "not ok" means, but all overrides
must - of course - have the same signature, otherwise they can't be
called the same way from the base class. IOW, dosomething1 calls
dosomething2 WITOUT parameters. How should it know you override do
something MUCH LATER with 2 parameters?
Yes, i have situation where in tclass2 function need other
[DCC Error] test.pas(20): E2137 Method 'dosomething2' not found in
base class
But about error, if i put function without parameter (as in tclass1)
then work.
Yes, that is what I said. It must have the same signature (same
parameters and result type) as the original, otherwise it can't be
called virtually.
Post by Cooper
Post by Rudy Velthuis
It can't know that and that is why all overrides must have the same
signature (parameters and return value). That is the only way
virtual functions CAN work.
Not understood :( In theory need work?
What I am saying that to be able to override, the function must look
exactly the same: same number and types of parameters, same function
result, because the function calling it can't know which one of these
will be called: the original or one of the overrides.
--
Rudy Velthuis http://rvelthuis.de

"Disobedience, in the eyes of anyone who has read history, is
man's original virtue. It is through disobedience that progress
has been made, through disobedience and through rebellion."
-- Oscar Wilde
Hans-Peter Diettrich
2010-03-04 19:52:48 UTC
Permalink
Post by Cooper
Solved about problem of before, now i have this problem: dosomething2
not every has same parameters.
I thinked to use: OverLoad on Override / Virtual directive but give me
some problem; where i mistake?
The virtual and override methods must have the same argument list, while
overloaded versions must have different argument lists.
Post by Cooper
tclass1 = class
public
function dosomething2: byte; virtual; [1]
procedure dosomething1;
end;
tclass2 = class (tclass1)
public
function dosomething2 (x, y: real) : byte; override; [2]
end;
[1] function dosomething2: byte; virtual; overload;
[2] function dosomething2 (x, y: real) : byte; override;
overload;
but not ok :( where mistake?
override+overload requires a virtual method with the same argument list
in the base class.

Solution 1: give the method an different name in TClass2.
Solution 2: remove override in TClass2.
Solution 3: add the same method as virtual+overload in TClass1.

#3 may be what you want.

DoDi
Cooper
2010-03-04 20:15:48 UTC
Permalink
Post by Hans-Peter Diettrich
Post by Cooper
Solved about problem of before, now i have this problem: dosomething2
not every has same parameters.
I thinked to use: OverLoad on Override / Virtual directive but give me
some problem; where i mistake?
The virtual and override methods must have the same argument list, while
overloaded versions must have different argument lists.
Post by Cooper
tclass1 = class
public
function dosomething2: byte; virtual; [1]
procedure dosomething1;
end;
tclass2 = class (tclass1)
public
function dosomething2 (x, y: real) : byte; override; [2]
end;
[1] function dosomething2: byte; virtual; overload;
[2] function dosomething2 (x, y: real) : byte; override;
overload;
but not ok :( where mistake?
override+overload requires a virtual method with the same argument list in
the base class.
Solution 1: give the method an different name in TClass2.
Solution 2: remove override in TClass2.
Solution 3: add the same method as virtual+overload in TClass1.
#3 may be what you want.
Thanks :) i try too this solution.
Before me tried to solve too in other mode, so:

tclass2 = class (tclass1)
protected
var x, y: real;
public
function dosomething2 : byte; override;
end;

shortly adding variable x, y in class and using it from procedure/function.
Is too correct solution or give some problem in general?
Thanks again very much.
Maarten Wiltink
2010-03-05 08:43:57 UTC
Permalink
<overriding virtual methods>
Post by Cooper
Post by Hans-Peter Diettrich
dosomething2 not every has same parameters.
[...]
Post by Cooper
Post by Hans-Peter Diettrich
The virtual and override methods must have the same argument list,
[...]
Post by Cooper
tclass2 = class (tclass1)
protected
var x, y: real;
public
function dosomething2 : byte; override;
end;
shortly adding variable x, y in class and using it from
procedure/function. Is too correct solution or give some problem
in general?
The expectation with virtual methods is that they can do their work
with the same information that sufficed for the original case (TClass1).

You can break that expectation, as you did, and add the extra information
to your object. But if you then need to set the extra information before
every time you call your virtual method, you haven't won much.

The problem is that parameters to methods were invented to make the
calls stateless, and by putting state (information) in your object and
allowing the method override to depend on it, the method isn't stateless
anymore.

This is a general concern with objects, which were invented to keep state
and methods together, and it's not a bad thing in itself. You should just
strike a balance. On one end, you have an object from a deep class
hierarchy, full of data, replete with methods that take _no_ parameters;
on the other, an object with no fields, and only class methods. The
latter is a 'library', the former 'job security'.

Groetjes,
Maarten Wiltink

Loading...