views:

146

answers:

3

Is there a way to get Object data from its class procedure or function without instantiate it?

A: 

I'm not sure this is what your talking about but...

type
  tmyclasstype = class of tmyclass;

  tmyclass = class(TObject)
   class function a:integer;
   class function b:tmyclass;
   class function c:tmyclasstype;
  end;

...

class tmyclass.function a:integer;
begin
  result := 0;
end;

class tmyclass.function b:tmyclass;
begin
  result := tmyclass.create;
end;

class tmyclass.function c:tmyclasstype;
begin
  result := tmyclass;
end;

IIRC, these are all valid examples of class methods. Anything else is not valid as you can't access any structures, variables or non-classed methods of an object with out instantiating it.

Ryan J. Mills
A: 

To add to Ryan's answer, you can call the class functions without instantiating objects such as this:

var 
   MyInt: Integer begin
begin
   MyInt := TMyClass.a;
M Schenkel
+2  A: 

You seem to have gotten it wrong:

  • Classes are specification on how data is layed out in memory, including code, but no data.
  • Objects are instances, meaning that they are data in memory, associated with a type.
  • Class methods are methods that have access to class information, but which do not have access to data or instances. This way, they can be called without instantiation.

Without instantiation, there is no data, and you cannot access data if it's not there.

Lars D