开发者

Problem about common class

first of all, sorry for title, but is very hard explain good in few words. Then il problem is this. I have two class(object): Tclass1 and Tclass2. These are indipendent from them and both classes(objects) call a third class(object): for example Tclass3. As i can share info of Tclass3 between Tclass1 and Tclass2?

Try to explain better with a example:

Tclass1 = class
private
  class3: Tclass3;
public
  property err: Tclass3 read class3 write class3;
  ...
end;

Tclass2 = class
private
  class3: Tclass3;
public
  property err: Tclass3 read class3 write class3;
  ...
end;

Tclass3 = class
private
  icode: word;
public
  property code: word read icode;
  ...
end;

and main program is:

var
  class1: Tclass1;
  class2: Tclass2;
begin
  class1 := Tclass1.create;
  try
    class2 := Tclass2.create;
    try
      class2.err := class1.err;  // <--- problem is here
         ...
         ... // processing...
         ... 
      class1.err := class2.err;  // <--- problem is here
      writeln (class1.err.code)      
    finally
      class2.free; 
    end;
  finally
    class1.free;
  end;
end;

of course, in Tclass1 and Tclass2 i call开发者_运维技巧 create method of Tclass3 and instance it. Now when i run it, make a exception, but i can't read it becouse console is closed fastly. I have applied to a class(object) same rules of a variable; infact if i use a variable to place of it, all work fine. Not is possible solve same with class(object)? Thanks again very much.


Your question is a bit vague. But let me try to understand.

  1. You have two classes, that own an instance of a third class. (They are responsible for creating and deleting the class).
  2. You want to share information (but not the class itself) between the two classes.

In that case, you could create an Assign method that copys the fields of one object to another object:

Tclass3 = class
private
  icode: word;
public
  procedure Assign(const AValue: TClass3); virtual;

  property code: word read icode;
  ...
end;

procedure TClass3.Assign(const AValue: TClass3);
begin
  Assert(AValue<>nil);
  icode := AValue.icode;
end;

If you want to share the same object between the two, you need to decide which of the classes owns the object. (Or you could even create a separate owner). But a better solution would be to use an interface to TClass3, so you could take advantage of reference counting.


"Now when i run it, make a exception, but i can't read it becouse console is closed fastly."

You can solve that problem as follows:

In the .dpr file of your console application you probably have something like this:

begin
  try
    // do stuff
  except
    on e:Exception do
      writeln(e.message);
  end;
end.

Just change it into this:

begin
  try
    // do stuff
  except
    on e:Exception do
    begin
      // show error, and wait for user to press a key
      writeln(e.message);
      readln;
    end;
  end;
end.

That should make debugging a bit easier.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜