PDA

View Full Version : Calling other constructor ok?



User137
05-08-2011, 01:46 AM
Assume i have class A (fictional code)

type
TClassA = class
public
constructor Create(width, height: integer);
constructor Create;
// Seems to compile fine without adding word "overload"
end;

implementation

constructor TClassA.Create(width, height: integer);
begin
DoSomething(width, height);
end;

constructor TClassA.Create;
begin
Create(100, 100);
// Now what happens here? Am i creating an object that will not get
// assigned to any variable, basically a memory leak?
end;
How should i call the other constructor to refer self as its object? If there's a reserved word for this i don't know. "inherited" won't do as this case is not a parent class constructor.

edit: Well, my class seems to work with this technique so i suppose its all ok... It was a class for FrameBuffer, rendering to texture and showing that on screen works :D


constructor Create(const _texture: integer; _depth: boolean = false);

// This calls above Create with a newly created empty texture
constructor Create(const width, height: integer; transparency: boolean; _depth: boolean = false);

Edit2:
So what i guess is happening is that there is a world of difference with Object.Create and Class.Create. Object.Create will not allocate a new object but instead uses it as a normal procedure. Right?

SuperMaximo93
05-08-2011, 09:52 AM
So what i guess is happening is that there is a world of difference with Object.Create and Class.Create. Object.Create will not allocate a new object but instead uses it as a normal procedure. Right?
That's correct. I prefer using Objects for this reason (and also I prefer the more verbose method of dynamically allocating/deallocating memory for them for some reason, i.e. new(myObject, create(param1, param2, ...)) and dispose(myObject, destroy))

Carver413
06-08-2011, 04:40 AM
objects are not well supported and certainly not recomended by delphi or free pascal.

Traveler
06-08-2011, 11:39 AM
That's an odd statement. Care to elaborate?

Carver413
06-08-2011, 04:53 PM
Well I remember reading a long,long time ago that Object was only there for backward compatibility and not recomended. there is also been bug reports in free pascal about using them. don't think I've ever used them myself so don't really keep up on that to much. what advantages do they offer over classes.