Recent

Author Topic: problem with Create inheritance  (Read 10319 times)

Frederic Da Vitoria

  • Jr. Member
  • **
  • Posts: 73
problem with Create inheritance
« on: June 22, 2013, 10:09:59 pm »
Hello,

I am converting a Delphi 6 project to Lazarus. I used the project conversion tool and got a project with still some issues. I managed to solve the first issues, but now I am stuck on this seemingly simple one:

o_MergeUsages unit contains the definition of o_MergeUsage class, to which I did no specify an ancestor (which means the ancestor is actually TObject). This class contains 2 variables and redefines Create as
Code: [Select]
    o_MergeUsage = class
    pCoordinates  : AnsiString ;
    pPicture      : AnsiString ;
    constructor Create (const coords, pict : string) ;
    end { mergeusage } ;
In the implementation, the first line of Create is "inherited Create ;":
Code: [Select]
  constructor o_mergeusage.Create (const coords, pict : string) ;
  begin                                                 { mergeusage.Create }
    inherited Create ;
    pCoordinates := coords ;
    pPicture := TrimQuotes (pict)
  end ;                                                 { mergeusage.Create }
This works perfectly in Delphi 6. But Lazarus complains on the "inherited Create ;": "Error: Illegal expression".

Of course, I can't use simply
Code: [Select]
inherited ;because o_MergeUsage.Create has one parameter while it's ancestor (TObject) has none.

What am I doing wrong?
« Last Edit: June 23, 2013, 01:10:40 am by davitof »
davitof
Lazarus 1.0.10 + FPC 2.6.2 / Windows7 64bits

Leledumbo

  • Hero Member
  • *****
  • Posts: 8833
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: problem with Create inheritance
« Reply #1 on: June 23, 2013, 03:50:01 am »
Looks like a bug in your compiler, what version it is? I can't reproduce with trunk.

Frederic Da Vitoria

  • Jr. Member
  • **
  • Posts: 73
Re: problem with Create inheritance
« Reply #2 on: June 23, 2013, 12:20:43 pm »
Lazarus 1.0.10, FPC 2.6.2 SVN revision 41613, i386.

I also tried adding "{$MODE Delphi}" (which the converter hadn't found necessary), but it didn't change anything.
davitof
Lazarus 1.0.10 + FPC 2.6.2 / Windows7 64bits

Leledumbo

  • Hero Member
  • *****
  • Posts: 8833
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: problem with Create inheritance
« Reply #3 on: June 23, 2013, 12:28:01 pm »
I'll try at office tommorrow (I only have trunk at home and only latest stable at office). My guess is that it's really a bug in 2.6.2 that has been fixed in trunk. You can also search the bugtracker to ensure.

Frederic Da Vitoria

  • Jr. Member
  • **
  • Posts: 73
Re: problem with Create inheritance
« Reply #4 on: June 23, 2013, 01:10:55 pm »
I just searched the bug tracker but I could not find any mention of my issue. But maybe it is described in another way, searching bug trackers often does not find things which are really there.

I now realize that there is no reason not to include the complete code of my unit, just to ensure I have not missed a relevant point in my description
Code: [Select]
unit o_MergeUsages;

{ tobject -> MergeUsage }

{$A+,B-,I+,X-}

Interface

  uses XString ;

  type

    o_MergeUsage = class
    pCoordinates  : AnsiString ;
    pPicture      : AnsiString ;
    constructor Create (const coords, pict : string) ;
    end { mergeusage } ;

Implementation

{ tobject -> MergeUsage }

  constructor o_mergeusage.Create (const coords, pict : string) ;
  begin                                                 { mergeusage.Create }
    inherited Create ;
    pCoordinates := coords ;
    pPicture := TrimQuotes (pict)
  end ;                                                 { mergeusage.Create }

end.

The XString unit referenced in the uses clause is plain non-object Pascal.
davitof
Lazarus 1.0.10 + FPC 2.6.2 / Windows7 64bits

Leledumbo

  • Hero Member
  • *****
  • Posts: 8833
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: problem with Create inheritance
« Reply #5 on: June 23, 2013, 02:33:52 pm »
Now that you post the complete unit I can see where the problem is:
Quote
{$A+,B-,I+,X-}
You have extended syntax disabled, which means ALL function results (including constructor call) MUST be assigned somewhere. This makes expression like:
Code: [Select]
inherited Create;illegal due to its result being discarded.

Bart

  • Hero Member
  • *****
  • Posts: 5648
    • Bart en Mariska's Webstek
Re: problem with Create inheritance
« Reply #6 on: June 23, 2013, 02:43:31 pm »
@Leledumbo: You beat me by 1 minute  8)

Bart

Leledumbo

  • Hero Member
  • *****
  • Posts: 8833
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: problem with Create inheritance
« Reply #7 on: June 23, 2013, 02:58:24 pm »
@Bart: 10 minutes according to the timestamp 8)

Bart

  • Hero Member
  • *****
  • Posts: 5648
    • Bart en Mariska's Webstek
Re: problem with Create inheritance
« Reply #8 on: June 23, 2013, 07:18:04 pm »
Time flies, when you're having fun  8)

Bart

Frederic Da Vitoria

  • Jr. Member
  • **
  • Posts: 73
Re: problem with Create inheritance
« Reply #9 on: June 23, 2013, 10:23:31 pm »
Thanks to both of you, this was indeed the solution. Disabling extended syntax obviously means different things for Borland and for FPC. It seems Borland does not really consider a constructor as a function. FPC is stricter, which I can perfectly understand.

Now I have an easy solution: {$X+}. But if I wanted to keep to strict syntax (which I usually prefer), what would be the correct way to do this (creating a constructor and calling the ancestor's constructor)? IIRC, you can't use overload with constructors, so I see only two ways:
  • enable extended syntax
  • Give to my constructor another name than "Create"

I there another (better) option?
davitof
Lazarus 1.0.10 + FPC 2.6.2 / Windows7 64bits

Bart

  • Hero Member
  • *****
  • Posts: 5648
    • Bart en Mariska's Webstek
Re: problem with Create inheritance
« Reply #10 on: June 23, 2013, 11:28:26 pm »
Just assign the value and discard it?

Code: [Select]
constructor o_mergeusage.Create (const coords, pict : string) ;
var
  p: pointer;
begin
  p := inherited create;
  ...
end;

Bart

Frederic Da Vitoria

  • Jr. Member
  • **
  • Posts: 73
Re: problem with Create inheritance
« Reply #11 on: June 23, 2013, 11:48:31 pm »
Hmm, would it work? AFAIK, the point in calling the ancestor's create is to do whatever initialization jobs must be performed on the new instance. If I assign the value to a local variable, wouldn't the inherited Create actually work on a new set of data instead of the current one? In other words, I am creating a o_MergeUsage instance which inherits TObject variables. Will invoking TObject.Create as you suggest reset the TObject variables in the o_MergeUsage instance being created (which is what I want) or will it create a new TObject (pointed with p) and reset it (which would be completely useless to me)?
davitof
Lazarus 1.0.10 + FPC 2.6.2 / Windows7 64bits

Frederic Da Vitoria

  • Jr. Member
  • **
  • Posts: 73
Re: problem with Create inheritance
« Reply #12 on: June 24, 2013, 10:18:22 am »
I decided to test my previous question myself, but now I am stuck in another unexpected point:
Code: [Select]
program Class_inheritance;

//{$mode delphi}{$H+}{$X-}
{$A+}
{$B-}
{$I+}
{$X-} // sinon Lazarus bloque

uses
  Classes
  { you can add units after this };

type
  o_Class1 = class
    fString1 : string ;
    constructor Create ;
  end ;

  o_Class2 = class (o_Class1)
    fString2 : string ;
    constructor Create (aStr : string) ;
  end ;

constructor o_Class1.Create ;
var t_o : pointer ;
begin
  t_o := inherited Create ;
  fString1 := 'test value'
end ;

constructor o_Class2.Create (aStr : string) ;
var c_1 : pointer;
begin
  c_1 := inherited Create ;
  fstring2 := aStr
end ;

var
  C2 : o_Class2 ;

begin
  C2 := o_Class2.Create ('test param') ;
  WriteLn (C2.fString1)
end.

I get an error Operation ">" not supported for types "Pointer" and "Pointer" on line 26. Is there somewhere a more or less complete list of the differences between Delphi and Lazarus? Or am I doing things which have become illegal in recent versions of Delphi?
davitof
Lazarus 1.0.10 + FPC 2.6.2 / Windows7 64bits

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: problem with Create inheritance
« Reply #13 on: June 24, 2013, 12:32:00 pm »
This looks like bug to me. None of the possible syntaxes work. I changed pointer type first to o_Class1, but then it complained that it needs to be TObject, cause it's the ancestor:
Code: [Select]
constructor o_Class1.Create;
var p: TObject;  //p: pointer;
begin
  p:=inherited Create;
end;

unit1.pas(37,1) Error: Operation ">" not supported for types "Pointer" and "Pointer"
It highlights the line "begin".

This is with SVN versions of FPC 2.7.1 and Lazarus 1.1.
« Last Edit: June 24, 2013, 12:35:06 pm by User137 »

Frederic Da Vitoria

  • Jr. Member
  • **
  • Posts: 73
Re: problem with Create inheritance
« Reply #14 on: June 24, 2013, 01:03:18 pm »
I am starting to wonder: could it be that I'm the only one trying to use $X- on pfc?
davitof
Lazarus 1.0.10 + FPC 2.6.2 / Windows7 64bits

 

TinyPortal © 2005-2018