Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Virtual constructors

0 views
Skip to first unread message

Roman

unread,
Jun 11, 2002, 10:44:34 PM6/11/02
to
Could somebody please explain to me what exactly the difference is between a
virtual and a non virtual constructor?

Thanks in advance!


Peter Gummer

unread,
Jun 11, 2002, 11:13:43 PM6/11/02
to
"Roman" <rom...@servidor.unam.mx> wrote in message news:3d06b555_2@dnews...

> Could somebody please explain to me what exactly the difference is between
a
> virtual and a non virtual constructor?

The difference is the same as for any other method: virtuals can be
overridden by subclasses.

Peter


Alvin Lee

unread,
Jun 11, 2002, 11:14:11 PM6/11/02
to
"Roman" <rom...@servidor.unam.mx> wrote in news:3d06b555_2@dnews:

> Could somebody please explain to me what exactly the difference
> is between a virtual and a non virtual constructor?
>
> Thanks in advance!
>
>

Virtual constructors (like any virtual method) can be overridden in
descendant classes.

--
Alvin Lee

Katiuscia Liverani

unread,
Jun 12, 2002, 2:06:56 AM6/12/02
to
I agree with Peter and Alvin.
More: if you make a class from another class, you will write a new
constructor (override), wich does new code and inherited cod (if you like).
Roman <rom...@servidor.unam.mx> wrote in message 3d06b555_2@dnews...

Serge Sapozhnikov (Cooldev.com)

unread,
Jun 12, 2002, 2:51:09 AM6/12/02
to
Hello!

Virtual constructor is just the same thing as virtual function - you can
override it.
When you create an instance using class ref (and a base class has a virtual
constructor) a proper constructor get called. This wouldn't be possible with
a static one.

Good luck, Serge
--
http://www.cooldev.com/
#CoolControls, CoolMenus and more Cool VCL packs for Delphi development.
#KoolSockets, KoolStorage and other kernel level solutions.
#CdRwLib SDK, WNASPI SDK packages for CD manipulations.
#Offshore development of high quality.
CoolDev.Com


Dustin Campbell

unread,
Jun 12, 2002, 6:20:15 AM6/12/02
to
virtual constructor is polymorphic. non-virtual isn't.

Regards,
Dustin Campbell
Microsys Technologies, Inc.

"Roman" <rom...@servidor.unam.mx> wrote in message news:3d06b555_2@dnews...

Jens Geyer

unread,
Jun 12, 2002, 9:27:41 AM6/12/02
to

At first, all of the answers are right.
At second, another important difference is the presence of the "virtual"
keyword.


....

What crap!!!

An constructor needs to be virtual, when you want to create instances using
a class reference (as Serge already told you). *That* is the most common
reason for making a ctor virtual. Please, have a look at "constructos and
class references" in the Object Pascal Help, where the details are
explained.

PS: Of course, you can override the ctor ... but this "explanation" does not
explain the very special function of virtual ctors.

PPS: It was not intended to harm anyone personally, so there is really no
need for flames.

JensG

robert

unread,
Jun 12, 2002, 9:34:44 AM6/12/02
to

hello

virtual constructors only make sens if you have some different class references descending from one base class

small exaple:

TBase = class
constructor Create; virtual; {abstract; }
end;
TBaseClass = class of TBase;

TClassA = class(TBaseClass)
constructor Create; override;
end;

TClassB = class(TBaseClass)
constructor Create; override;
end;

var
abc: array of TBaseClass;
begin
abc[1] := TClassA;
abc[2] := TClassB;
end;

function CreateObjectByNumber(num: Integer): TBase;
begin
result := abs[num].Create;
end;

this is (like) RegisterClass(), GetClass()... work


robert

"Jens Geyer" <j...@vsx.net> wrote:
>
>At first, all of the answers are right.
>At second, another important difference is the presence of the "virtual"
>keyword.
>
>

>.....

0 new messages