Announcement

Collapse
No announcement yet.

Wie mache ich die Variablen der Klassen in allen Formularen nutzbar?

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Wie mache ich die Variablen der Klassen in allen Formularen nutzbar?

    Einzelne Variblen in andere Units zu bekommen ist ja ganz einfach mit

    <b>extern int</b> a;

    Aber wie funktioniert das Ganze mit Klassen die man definiert hat.
    Habe ich zum Bsp.
    klasse a;
    so kann ich sie nicht einfach mit
    <b>extern</b> klasse a;
    zugreifbar machen...
    Danke im vorraus für eure Hilfe.
    Bis dann
    Daniel

  • #2
    Das geht auch mit extern. Du mußt nur die Klasse nochmals deklarieren

    Beispiel

    Unit1 :

    <pre>
    //---------------------------------------------------------------------------

    #include <vcl.h>
    #pragma hdrstop

    #include "Unit1.h"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TForm1 *Form1;
    class a
    {
    private:
    int x;
    };

    a mya;
    //---------------------------------------------------------------------------
    __fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
    {
    }
    //---------------------------------------------------------------------------


    Unit 2:

    class a
    {
    private:
    int x;
    };

    extern mya; // hier wir auf die Klasse in Unit 1 verwiesen.

    </pre&gt
    Christian

    Comment


    • #3
      Hi Daniel,<br>
      wenn ich dein Problem richtig verstanden habe, brauchst du Header-Dateien, wie "a.h" im folgenden Beispiel:
      <pre class="sourcecode"><code>
      <font color="#003399"><i>//--------------------------------------------------------------------------- </i></font>
      <font color="#003399"><i>// Datei a.h</i></font>
      <font color="#003399"><i>//--------------------------------------------------------------------------- </i></font>
      <b>class</b> a
      {
      <b>private</b>:
      <b>int</b> x;
      };
      <br>
      <b>extern</b> a mya;
      <font color="#003399"><i>//--------------------------------------------------------------------------- </i></font>
      <br>
      <br>
      <font color="#003399"><i>//--------------------------------------------------------------------------- </i></font>
      <font color="#003399"><i>// Datei main.cpp</i></font>
      <font color="#003399"><i>//--------------------------------------------------------------------------- </i></font>
      <font color="990099">#include &quot;a.h&quot; </font>
      <b>extern</b> a mya;
      <br>
      <b>int</b> main()
      {
      <font color="#003399"><i>// Mach was mit mya</i></font>
      };
      <font color="#003399"><i>//--------------------------------------------------------------------------- </i></font>
      </code></pre>
      (@Christian: Ich hab einfach mal dein Beispiel recycelt. Hoffentlich gibt's keine Lizenzprobleme. ;-))
      <p>Uli

      Comment


      • #4
        Hi, Daniel!<br>
        Mist, zuviel Copy&Paste im vorigen Posting: das <tt>extern</tt> in "main.cpp" ist natürlich zuviel des Guten. Aber wie das "Bekanntmachen" von <b>Variablen</b> geht, weißt du ja. Dein Problem war das Bekanntmachen des <b>Typs</b>, richtig?
        <br>Ciao, Uli

        Comment


        • #5
          Danke für die vielen Antworten. Es läuft jetzt.
          Thx
          Danie

          Comment

          Working...
          X