<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Zugriff auf erzeugte Instanz]]></title><description><![CDATA[<p>Hi,</p>
<p>ich habe eine Frage bzgl. Zugriff auf eine erzeugte Instanz aus einer anderen Funktion.</p>
<p>Im folgenden Beispiel kann ich im Timer Event nicht auf Intp1 zugreifen. Was mach ich hier falsch bzw. geht das auch ohne die Instanz global anzulegen ?</p>
<p>Unit1.cpp</p>
<pre><code class="language-cpp">//---------------------------------------------------------------------------

#include &lt;vcl.h&gt;
#pragma hdrstop

#include &quot;Unit1.h&quot;
#include &quot;Unit2.h&quot;
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource &quot;*.dfm&quot;
TForm1 *Form1;

int CInterpreter::c = 0;

 //---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
 CInterpreter Intp1;
 Intp1.Parser();
 Intp1.Modify_c( 1000 );
 // hier wird einiges gemacht
 Intp1.Parser();
 Timer1-&gt;Enabled = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
 Timer1-&gt;Enabled = false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
 Intp1.Parser();  // Kein Zugriff
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
 Close();	
}
//---------------------------------------------------------------------------
</code></pre>
<p>Unit2.cpp</p>
<pre><code class="language-cpp">//---------------------------------------------------------------------------

#pragma hdrstop

#include &quot;Unit2.h&quot;
#include &quot;Unit1.h&quot;

//---------------------------------------------------------------------------

#pragma package(smart_init)

// Definition des Konstruktors CInterpreter
__fastcall CInterpreter::CInterpreter()
{
 a = 5;
 b = 5;
}
//---------------------------------------------------------------------------

// Definition des Destruktors CInterpreter
__fastcall CInterpreter::~CInterpreter()
{
}
//---------------------------------------------------------------------------

void __fastcall CInterpreter::Parser()
{
 c += (a + b);
 Form1-&gt;ListBox1-&gt;Items-&gt;Add(&quot;P = &quot; + IntToStr(c));
}
//---------------------------------------------------------------------------

void __fastcall CInterpreter::Modify_c( int value )
{
 c += value;
}
//---------------------------------------------------------------------------
</code></pre>
<p>Unit2.h</p>
<pre><code class="language-cpp">//---------------------------------------------------------------------------

#ifndef Unit2H
#define Unit2H
//---------------------------------------------------------------------------

class CInterpreter
{
 private:
	int a, b;
	static int c;
 public:
	__fastcall CInterpreter();   // Konstruktor
	__fastcall ~CInterpreter();  // Destruktor
	void __fastcall Parser();
	void __fastcall Modify_c( int );
};
#endif
//---------------------------------------------------------------------------
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/229585/zugriff-auf-erzeugte-instanz</link><generator>RSS for Node</generator><lastBuildDate>Sun, 27 Sep 2026 18:40:55 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/229585.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 13 Dec 2008 22:34:52 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Zugriff auf erzeugte Instanz on Sat, 13 Dec 2008 22:34:52 GMT]]></title><description><![CDATA[<p>Hi,</p>
<p>ich habe eine Frage bzgl. Zugriff auf eine erzeugte Instanz aus einer anderen Funktion.</p>
<p>Im folgenden Beispiel kann ich im Timer Event nicht auf Intp1 zugreifen. Was mach ich hier falsch bzw. geht das auch ohne die Instanz global anzulegen ?</p>
<p>Unit1.cpp</p>
<pre><code class="language-cpp">//---------------------------------------------------------------------------

#include &lt;vcl.h&gt;
#pragma hdrstop

#include &quot;Unit1.h&quot;
#include &quot;Unit2.h&quot;
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource &quot;*.dfm&quot;
TForm1 *Form1;

int CInterpreter::c = 0;

 //---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
 CInterpreter Intp1;
 Intp1.Parser();
 Intp1.Modify_c( 1000 );
 // hier wird einiges gemacht
 Intp1.Parser();
 Timer1-&gt;Enabled = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
 Timer1-&gt;Enabled = false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
 Intp1.Parser();  // Kein Zugriff
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
 Close();	
}
//---------------------------------------------------------------------------
</code></pre>
<p>Unit2.cpp</p>
<pre><code class="language-cpp">//---------------------------------------------------------------------------

#pragma hdrstop

#include &quot;Unit2.h&quot;
#include &quot;Unit1.h&quot;

//---------------------------------------------------------------------------

#pragma package(smart_init)

// Definition des Konstruktors CInterpreter
__fastcall CInterpreter::CInterpreter()
{
 a = 5;
 b = 5;
}
//---------------------------------------------------------------------------

// Definition des Destruktors CInterpreter
__fastcall CInterpreter::~CInterpreter()
{
}
//---------------------------------------------------------------------------

void __fastcall CInterpreter::Parser()
{
 c += (a + b);
 Form1-&gt;ListBox1-&gt;Items-&gt;Add(&quot;P = &quot; + IntToStr(c));
}
//---------------------------------------------------------------------------

void __fastcall CInterpreter::Modify_c( int value )
{
 c += value;
}
//---------------------------------------------------------------------------
</code></pre>
<p>Unit2.h</p>
<pre><code class="language-cpp">//---------------------------------------------------------------------------

#ifndef Unit2H
#define Unit2H
//---------------------------------------------------------------------------

class CInterpreter
{
 private:
	int a, b;
	static int c;
 public:
	__fastcall CInterpreter();   // Konstruktor
	__fastcall ~CInterpreter();  // Destruktor
	void __fastcall Parser();
	void __fastcall Modify_c( int );
};
#endif
//---------------------------------------------------------------------------
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1630141</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1630141</guid><dc:creator><![CDATA[C++ NeuAnfänger]]></dc:creator><pubDate>Sat, 13 Dec 2008 22:34:52 GMT</pubDate></item><item><title><![CDATA[Reply to Zugriff auf erzeugte Instanz on Sat, 13 Dec 2008 23:01:19 GMT]]></title><description><![CDATA[<p>Hi,</p>
<p>sorry, da ist mir viel zu viel Überflüssiges und &quot;Proprietäres&quot; bei, um mich durch den Code zu wühlen (wo ist denn das &quot;Timer Event&quot;, das &quot;...auf Intp1 zugreifen ...&quot; soll ? Vermutlich meinst Du &quot;TForm1::Timer1Timer()&quot;).<br />
Prinzipiell würde ich sagen: Du solltest Dir Gedanken bzgl. der Lebenszeit von Objekten machen: &quot;auto-Objekte&quot; (auch &quot;Stackobjekte&quot; genannt) werden nach Verlassen des Scopes (z.B. der jeweiligen Funktion) wieder abgeräumt.<br />
Lösnug:<br />
a) Scope weiter fassen (z.B. zu einem Member des umschließenden Objekts machen) oder<br />
b) Objekt dynamisch (&quot;Heapobjekt&quot;) anlegen - Nachteil: &quot;Ownership&quot; muss geklärt sein: Wer räumt es dann (manuell) wieder ab?</p>
<p>a) ist meistens die bessere Lösung - in Deinem Fall kann Intp1 anscheinend ein Member von TForm1 sein, oder ?</p>
<p>Gruß,</p>
<p>Simon2.</p>
<p>BTW: Deine Variablennamen sind nicht so dolle - gerade mit &quot;Klassennummerierung&quot; würde ich gar nicht erst anfangen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1630146</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1630146</guid><dc:creator><![CDATA[Simon2]]></dc:creator><pubDate>Sat, 13 Dec 2008 23:01:19 GMT</pubDate></item><item><title><![CDATA[Reply to Zugriff auf erzeugte Instanz on Sat, 13 Dec 2008 23:14:39 GMT]]></title><description><![CDATA[<p>Hi Simon2,</p>
<p>danke für Deine Hilfe. Kannst Du mir sagen was Du genau mit a) meinst (Beispiel) ?</p>
<p>Und was ist an meinen Variablennamen nicht i.O. bzw. Klassennummerierung ?</p>
<p>Ich möchte es gerne lernen, aber ich verstehe noch nicht was Du damit meinst !</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1630152</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1630152</guid><dc:creator><![CDATA[C++ NeuAnfänger]]></dc:creator><pubDate>Sat, 13 Dec 2008 23:14:39 GMT</pubDate></item><item><title><![CDATA[Reply to Zugriff auf erzeugte Instanz on Sat, 13 Dec 2008 23:40:40 GMT]]></title><description><![CDATA[<p>C++ NeuAnfänger schrieb:</p>
<blockquote>
<p>Hi Simon2,</p>
<p>danke für Deine Hilfe. Kannst Du mir sagen was Du genau mit a) meinst (Beispiel) ?...</p>
</blockquote>
<p>Naja, irgendwo ist doch die Klassendefinition von TForm1. Sowas in der Art wie:</p>
<pre><code class="language-cpp">class TForm1 {
...
};
</code></pre>
<p>Da muss eben noch Dein &quot;CInterpreter Intp1;&quot; rein - und nicht erst in die Funktion TForm1::ButtonClick().<br />
Dann wird das CInterpreter-Objekt names &quot;Intp1&quot; zusammen mit dem TForm1-Objekt angelegt (und zusammen mit ihm vernichtet) und Du hast in allen Memberfunktionen von TForm1 zugriff darauf - also auch in TForm1::Timer1Timer().<br />
Wenn ich recht sehe, hat Du das mit &quot;a&quot;, &quot;b&quot; und &quot;c&quot; bereits gemacht.</p>
<p>C++ NeuAnfänger schrieb:</p>
<blockquote>
<p>Und was ist an meinen Variablennamen nicht i.O. bzw. Klassennummerierung ?</p>
</blockquote>
<p>Naja, was soll mir als Außenstehendem (oder Dir, wenn Du Dir Dein Programm in 5 Jahren nochmal ansiehst) sagen, wenn da steht<br />
a<br />
b<br />
c<br />
TForm1 (Warum &quot;1&quot; ? Was kann die mehr als die &quot;TForm&quot; ? ... oder die &quot;TForm2&quot; ? ...<br />
Timer1Timer (Was ist das - ein &quot;TimerTimer&quot; ... und besonders ein &quot;Timer1Timer&quot;? Gibt's auch einen &quot;Timer2Timer&quot;? )<br />
...</p>
<p>Variablen sollte man möglichst gleich ansehen, wofür sie da sind.</p>
<p>C++ NeuAnfänger schrieb:</p>
<blockquote>
<p>Ich möchte es gerne lernen, aber ich verstehe noch nicht was Du damit meinst !</p>
</blockquote>
<p>Doppelt gut:<br />
- Lernen wollen und<br />
- (sinnvoll) Nachfragen.<br />
<img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f60b.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_savoring_food"
      title=":yum:"
      alt="😋"
    /> <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44d.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_up"
      title=":+1:"
      alt="👍"
    /></p>
<p>Gruß,</p>
<p>Simon2.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1630158</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1630158</guid><dc:creator><![CDATA[Simon2]]></dc:creator><pubDate>Sat, 13 Dec 2008 23:40:40 GMT</pubDate></item><item><title><![CDATA[Reply to Zugriff auf erzeugte Instanz on Sat, 13 Dec 2008 23:48:22 GMT]]></title><description><![CDATA[<p>Hi Simon2,</p>
<p>danke nochmal für Deine Hilfe. Das hilft mir schon weiter.</p>
<p>P.S. a,b,c habe ich nur als Beispiel genommen. Normalerwiese nehme ich schon andere Namen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1630162</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1630162</guid><dc:creator><![CDATA[C++ NeuAnfänger]]></dc:creator><pubDate>Sat, 13 Dec 2008 23:48:22 GMT</pubDate></item><item><title><![CDATA[Reply to Zugriff auf erzeugte Instanz on Sun, 14 Dec 2008 12:30:41 GMT]]></title><description><![CDATA[<p>*hust* Falsches Forum *hust*</p>
<p>Das nächste mal hast du hier bessere Chancen: <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /><br />
<a href="http://www.c-plusplus.net/forum/viewforum-var-f-is-2.html" rel="nofollow">http://www.c-plusplus.net/forum/viewforum-var-f-is-2.html</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1630287</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1630287</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Sun, 14 Dec 2008 12:30:41 GMT</pubDate></item><item><title><![CDATA[Reply to Zugriff auf erzeugte Instanz on Sun, 14 Dec 2008 20:04:37 GMT]]></title><description><![CDATA[<p>C++ NeuAnfänger schrieb:</p>
<blockquote>
<p>...P.S. a,b,c habe ich nur als Beispiel genommen. Normalerwiese nehme ich schon andere Namen.</p>
</blockquote>
<p>Dann ist ja gut.<br />
(das gilt für &quot;TForm1&quot; und &quot;Timer1Timer&quot; hoffentlich auch ... ) <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
<p>Schön, wenn ich helfen konnte.</p>
<p>Gruß,</p>
<p>Simon2.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1630493</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1630493</guid><dc:creator><![CDATA[Simon2]]></dc:creator><pubDate>Sun, 14 Dec 2008 20:04:37 GMT</pubDate></item></channel></rss>