🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #4775

Re: inherited Create;

#4775
From: Remy Lebeau (TeamB)
Date: Wed, 30 Nov 2005 23:50
73 lines
3599 bytes
"Alistair" <NoSpamPlease@Somewhere.Com> wrote in message
news:438e31c6$1@newsgroups.borland.com...

> So is this possible in C++?

Is what possible in C++?  Please be more specific.

> Can I declare a class A with a Do() method and then have
> that Do() method in class A (base class) call the inherited
> version in class B ( where class B : public A { ... }; ) ??

In general, yes.  That is what virtual methods and polymorphism is all
about.

However, that is not what the TThread code you showed is actually doing.
The 'inherited' keyword is used to access an ancestor base class, not a
descendant class.  The TThread constructor you show is calling the
constructor in the TObject ancestor base class, not your MyThread descendant
class.

> Basically, I am looking at creating my own class that does
> exactly what TThread does. E.g. :
<snip>
> Somehow the base TThread class calls the Execute in the
> class that inherits TThread

Execute() is a virtual method in TThread that you are overriding in a
descendant class.  Polymorphism ensures that the Execute() method of the
top-most descendant class is the one that is actually run when called via a
pointer to a base class.  In this case, TThread creates a thread object
provided by the OS (such as by using CreateThread() on Win32 systems) that
calls a global function (ThreadProc(), which is also in classes.pas) in the
context of a separate thread then the one that is creating the TThread class
object instance.  That global function is passed a pointer to a TThread
object instance and then calls the virtual Execute() method via that base
class pointer, ie:

    function ThreadProc(Thread: TThread): Integer;
    var
        FreeThread: Boolean;
    begin
        try
            Thread.Execute; // <-- polymorphic call here
        finally
            FreeThread := Thread.FFreeOnTerminate;
            Result := Thread.FReturnValue;
            Thread.FFinished := True;
            Thread.DoTerminate;
            if FreeThread then Thread.Free;
            EndThread(Result);
        end;
    end;

    constructor TThread.Create(CreateSuspended: Boolean);
    var
    Flags: DWORD;
    begin
        inherited Create; // <-- calls the TObject constructor
        AddThread;
        FSuspended := CreateSuspended;
        Flags := 0;
        if CreateSuspended then Flags := CREATE_SUSPENDED;
        FHandle := BeginThread(nil, 0, @ThreadProc, Pointer(Self), Flags,
FThreadID); // <-- start the worker thread here
    end;

> I am assuming it does this by the 'inherited Create;'.

You assume incorrectly.


Gambit
Technical Details
Message-ID: <438e3b10$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!newsfeed2.telusplanet.net!newsfeed.telus.net!west2.newsfeed.sprint-canada.net!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail
References: <438e31c6$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,3e5cce9567b943d3
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <438e31c6$1@newsgroups.borland.com>
Subject: Re: inherited Create;
Date: Wed, 30 Nov 2005 15:50:44 -0800
Lines: 75
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1409
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409
NNTP-Posting-Host: 68.69.17.41
Message-ID: <438e3b10$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1133394704 68.69.17.41 (30 Nov 2005 15:51:44 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!newsfeed2.telusplanet.net!newsfeed.telus.net!west2.newsfeed.sprint-canada.net!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail