Page 2 of 2 FirstFirst 12
Results 11 to 13 of 13

Thread: Constructor casting

  1. #11

    Constructor casting

    The main purpose of what I'm trying to achieve is a hierarchy for my engine. I want to make a flexible class that will let me create children within it, so I don't need to create them in my main app. For example:
    [pascal]
    // ** I DO NOT WANT IT WORK LIKE THIS **
    var
    X: TClass1;
    Y: TClass2;
    begin
    X := TClass1.Create('objectx');
    Y := TClass2.Create('objecty', 3.14);
    try
    X.Add(Y);
    finally
    X.Free(); // Y is freed here
    end;
    end;

    // ** INSTEAD, I WANT IT LOOK LIKE THIS **
    var
    X: TClass1;
    begin
    X := TClass1.Create('objectx');
    try
    with TClass2(X.AddNew(TClass2)) do
    // do something
    finally
    X.Free();
    end;
    end;
    [/pascal]
    I could easily achieve this using metaclasses, but the problem is that I wanna pass an extra parameter to the TClass2 constructor.

    Assuming for above posts, it is not possible, am I right?

  2. #12

    Constructor casting

    Assuming for above posts, it is not possible, am I right?
    It is possible, it seems both Grudzio's (virtual simple constructor + Init methods) and my (like virtual constructor taking "array of const") ideas could be useful for you

  3. #13

    Constructor casting

    Ok, I'll give them both a shot, thanks a lot, men!

Page 2 of 2 FirstFirst 12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •