<?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[[Gelöst] Verständnissfrage: An welcher Stelle erstes Objekt erstellen?]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe eine sehr einfache Klasse, die einfach nur die Koordinaten und Größe eines Rechtecks beschreibt, und die Setter und Getter Methoden dazu hat.</p>
<pre><code>class SimpleShape
{
public:
    int GetXPosition();
    int GetYPosition();
    int GetWidth();
    int GetHeight();
    void SetXPosition(int xposition);
    void SetYPosition(int yposition);
    void SetWidth(int width);
    void SetHeight(int height);
    SimpleShape(int sx,int sy);
    ~SimpleShape();
private:
    int itsXPosition;
    int itsYPosition;
    int itsWidth;
    int itsHeight;
};

SimpleShape::SimpleShape(int sx,int sy)
{
    itsXPosition=sx;
    itsYPosition=sy;
    itsWidth=200;
    itsHeight=50;
}

SimpleShape::~SimpleShape()
{
}

int SimpleShape::GetXPosition()
{
    return itsXPosition;
}

int SimpleShape::GetYPosition()
{
    return itsYPosition;
}

int SimpleShape::GetWidth()
{
    return itsWidth;
}

int SimpleShape::GetHeight()
{
    return itsHeight;
}

void SimpleShape::SetXPosition(int xposition)
{
    itsXPosition=xposition;
}

void SimpleShape::SetYPosition(int yposition)
{
    itsYPosition=yposition;
}

void SimpleShape::SetWidth(int width)
{
    itsWidth=width;
}

void SimpleShape::SetHeight(int height)
{
    itsHeight=height;
}
</code></pre>
<p>Nach der Message WM_PAINT, hohlt sich das Programm die Werte über die Getter Methoden und zeichnet ein Rechteck.<br />
Soweit so gut.<br />
Bei einem Klick in das Fenster soll sich das Rechteck aber um 5 Pixel nach unten bewegen.<br />
Habe also WM_LBUTTONDOWN mit aufgenommen. Aber das Rechteck bewegt sich kein stück.<br />
Ich hab ja das Gefühl das ich das erste Objekt an der falschen Stelle initialisiere. Aber wenn ich es ausserhalb der Callback Routine erstelle bekomme ich immer Out of Scope fehler.<br />
Das gesammte Programm sieht im Moment so aus:</p>
<pre><code>#if defined(UNICODE) &amp;&amp; !defined(_UNICODE)
    #define _UNICODE
#elif defined(_UNICODE) &amp;&amp; !defined(UNICODE)
    #define UNICODE
#endif

#include &lt;tchar.h&gt;
#include &lt;windows.h&gt;
#include &lt;simpleshape.h&gt;

/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/*  Make the class name into a global variable  */
TCHAR szClassName[ ] = _T(&quot;CodeBlocksWindowsApp&quot;);

int WINAPI WinMain (HINSTANCE hThisInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR lpszArgument,
                     int nCmdShow)
{
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default colour as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&amp;wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           _T(&quot;Code::Blocks Template Windows App&quot;),       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           544,                 /* The programs width */
           375,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nCmdShow);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&amp;messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&amp;messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&amp;messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}

/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{

    HDC         hdc;
    PAINTSTRUCT ps;
    SimpleShape FirstShape(50,50);

    switch (message)                  /* handle the messages */
    {
        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;
        case WM_PAINT:
            hdc = BeginPaint(hwnd, &amp;ps);

            Rectangle(hdc,
                      FirstShape.GetXPosition(),
                      FirstShape.GetYPosition(),
                      FirstShape.GetXPosition()+FirstShape.GetWidth(),
                      FirstShape.GetYPosition()+FirstShape.GetHeight());

            EndPaint(hwnd, &amp;ps);
            return 0;
        case WM_LBUTTONDOWN:
            FirstShape.SetYPosition(FirstShape.GetYPosition()+5);
            InvalidateRect(hwnd, NULL, TRUE);
            return 0;
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}
</code></pre>
<p>Mein Objekt wird bei Zeile 86 erstellt, also innerhalb der Callback Routine vor der Message Case Abfrage.</p>
<p>Wäre dankbar wenn jemand etwas Licht auf dieses Thema scheinen könnte.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/327448/gelöst-verständnissfrage-an-welcher-stelle-erstes-objekt-erstellen</link><generator>RSS for Node</generator><lastBuildDate>Thu, 09 Jul 2026 10:45:25 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/327448.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 12 Aug 2014 17:15:03 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to [Gelöst] Verständnissfrage: An welcher Stelle erstes Objekt erstellen? on Tue, 12 Aug 2014 18:06:36 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe eine sehr einfache Klasse, die einfach nur die Koordinaten und Größe eines Rechtecks beschreibt, und die Setter und Getter Methoden dazu hat.</p>
<pre><code>class SimpleShape
{
public:
    int GetXPosition();
    int GetYPosition();
    int GetWidth();
    int GetHeight();
    void SetXPosition(int xposition);
    void SetYPosition(int yposition);
    void SetWidth(int width);
    void SetHeight(int height);
    SimpleShape(int sx,int sy);
    ~SimpleShape();
private:
    int itsXPosition;
    int itsYPosition;
    int itsWidth;
    int itsHeight;
};

SimpleShape::SimpleShape(int sx,int sy)
{
    itsXPosition=sx;
    itsYPosition=sy;
    itsWidth=200;
    itsHeight=50;
}

SimpleShape::~SimpleShape()
{
}

int SimpleShape::GetXPosition()
{
    return itsXPosition;
}

int SimpleShape::GetYPosition()
{
    return itsYPosition;
}

int SimpleShape::GetWidth()
{
    return itsWidth;
}

int SimpleShape::GetHeight()
{
    return itsHeight;
}

void SimpleShape::SetXPosition(int xposition)
{
    itsXPosition=xposition;
}

void SimpleShape::SetYPosition(int yposition)
{
    itsYPosition=yposition;
}

void SimpleShape::SetWidth(int width)
{
    itsWidth=width;
}

void SimpleShape::SetHeight(int height)
{
    itsHeight=height;
}
</code></pre>
<p>Nach der Message WM_PAINT, hohlt sich das Programm die Werte über die Getter Methoden und zeichnet ein Rechteck.<br />
Soweit so gut.<br />
Bei einem Klick in das Fenster soll sich das Rechteck aber um 5 Pixel nach unten bewegen.<br />
Habe also WM_LBUTTONDOWN mit aufgenommen. Aber das Rechteck bewegt sich kein stück.<br />
Ich hab ja das Gefühl das ich das erste Objekt an der falschen Stelle initialisiere. Aber wenn ich es ausserhalb der Callback Routine erstelle bekomme ich immer Out of Scope fehler.<br />
Das gesammte Programm sieht im Moment so aus:</p>
<pre><code>#if defined(UNICODE) &amp;&amp; !defined(_UNICODE)
    #define _UNICODE
#elif defined(_UNICODE) &amp;&amp; !defined(UNICODE)
    #define UNICODE
#endif

#include &lt;tchar.h&gt;
#include &lt;windows.h&gt;
#include &lt;simpleshape.h&gt;

/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/*  Make the class name into a global variable  */
TCHAR szClassName[ ] = _T(&quot;CodeBlocksWindowsApp&quot;);

int WINAPI WinMain (HINSTANCE hThisInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR lpszArgument,
                     int nCmdShow)
{
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default colour as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&amp;wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           _T(&quot;Code::Blocks Template Windows App&quot;),       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           544,                 /* The programs width */
           375,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nCmdShow);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&amp;messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&amp;messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&amp;messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}

/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{

    HDC         hdc;
    PAINTSTRUCT ps;
    SimpleShape FirstShape(50,50);

    switch (message)                  /* handle the messages */
    {
        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;
        case WM_PAINT:
            hdc = BeginPaint(hwnd, &amp;ps);

            Rectangle(hdc,
                      FirstShape.GetXPosition(),
                      FirstShape.GetYPosition(),
                      FirstShape.GetXPosition()+FirstShape.GetWidth(),
                      FirstShape.GetYPosition()+FirstShape.GetHeight());

            EndPaint(hwnd, &amp;ps);
            return 0;
        case WM_LBUTTONDOWN:
            FirstShape.SetYPosition(FirstShape.GetYPosition()+5);
            InvalidateRect(hwnd, NULL, TRUE);
            return 0;
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}
</code></pre>
<p>Mein Objekt wird bei Zeile 86 erstellt, also innerhalb der Callback Routine vor der Message Case Abfrage.</p>
<p>Wäre dankbar wenn jemand etwas Licht auf dieses Thema scheinen könnte.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2413139</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2413139</guid><dc:creator><![CDATA[0BackBONE0]]></dc:creator><pubDate>Tue, 12 Aug 2014 18:06:36 GMT</pubDate></item><item><title><![CDATA[Reply to [Gelöst] Verständnissfrage: An welcher Stelle erstes Objekt erstellen? on Tue, 12 Aug 2014 17:24:33 GMT]]></title><description><![CDATA[<p>0BackBONE0 schrieb:</p>
<blockquote>
<p>ich habe eine sehr einfache Klasse,</p>
</blockquote>
<p>Schonmal daran gedacht, die Methoden inline zu schreiben? Deine sehr einfache Klasse kommt sehr bloatig daher.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2413141</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2413141</guid><dc:creator><![CDATA[inlineskater]]></dc:creator><pubDate>Tue, 12 Aug 2014 17:24:33 GMT</pubDate></item><item><title><![CDATA[Reply to [Gelöst] Verständnissfrage: An welcher Stelle erstes Objekt erstellen? on Tue, 12 Aug 2014 17:24:46 GMT]]></title><description><![CDATA[<p>Bei jedem Callback beziehst du dich ja auf statische Werte, immer auf 50. Wenn du jedes Mal mit 50 arbeitest, wie soll sich da was bewegen? Du musst ja schon die aktuelle Position deines Rechtecks verändern, oder nicht?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2413142</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2413142</guid><dc:creator><![CDATA[out]]></dc:creator><pubDate>Tue, 12 Aug 2014 17:24:46 GMT</pubDate></item><item><title><![CDATA[Reply to [Gelöst] Verständnissfrage: An welcher Stelle erstes Objekt erstellen? on Tue, 12 Aug 2014 17:35:33 GMT]]></title><description><![CDATA[<p>hm, ja da hast Du wohl recht.<br />
Hatte es noch in WinMain Probiert. Da bekomme ich dann den Fehler:</p>
<pre><code>error: 'FirstShape' was not declared in this scope|
</code></pre>
<p>Hab gerade gemerkt das es recht gut funktioniert wenn ich das Objekt global mache.<br />
Ich weiß nur nicht ob das schlau ist.<br />
Ich hab schon öffter gelesen das es keine gute Idee ist Variablen global zu machen.<br />
Ist das bei Objekten auch so?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2413146</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2413146</guid><dc:creator><![CDATA[0BackBONE0]]></dc:creator><pubDate>Tue, 12 Aug 2014 17:35:33 GMT</pubDate></item><item><title><![CDATA[Reply to [Gelöst] Verständnissfrage: An welcher Stelle erstes Objekt erstellen? on Tue, 12 Aug 2014 17:45:29 GMT]]></title><description><![CDATA[<p>0BackBONE0 schrieb:</p>
<blockquote>
<p>Ich weiß nur nicht ob das schlau ist.<br />
Ist das bei Objekten auch so?</p>
</blockquote>
<p>Ja, das ist nicht so schlau. Ähm Variablen sind auch Objekte. Alles was Speicher belegt, ist ein Objekt. Zu Objekten von einfachten Typen wie int sagt man halt meistens Variablen. Zu Objekten von Klassen sagt man meistens Instanz.</p>
<p>Wenn du was bewegen willst, sollte es eine Funktion geben, die dir die aktuelle Position liefert, also get_position(). Und dann als nächstes sollte es eine Funktion set_position geben, in die du die Werte reinsteckst, die get_position() geliefert hat und dabei eben einen Wert addierst. Im Endeffekt also set_position( get_position()+5) );</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2413150</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2413150</guid><dc:creator><![CDATA[out]]></dc:creator><pubDate>Tue, 12 Aug 2014 17:45:29 GMT</pubDate></item><item><title><![CDATA[Reply to [Gelöst] Verständnissfrage: An welcher Stelle erstes Objekt erstellen? on Tue, 12 Aug 2014 17:51:43 GMT]]></title><description><![CDATA[<p>Hab da immer noch ein Brett vorm Kopf.<br />
Wenn ich die Instanz im Callback erstelle kann ich auf die Methoden zugreifen, habe aber immer die selben Werte bei der Initialisierung.<br />
Wenn es auch nicht schlau ist die Instanz global zu haben, wo ist dann der richtige platz um keinen Out of Scope Fehler zu bekommen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2413152</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2413152</guid><dc:creator><![CDATA[0BackBONE0]]></dc:creator><pubDate>Tue, 12 Aug 2014 17:51:43 GMT</pubDate></item><item><title><![CDATA[Reply to [Gelöst] Verständnissfrage: An welcher Stelle erstes Objekt erstellen? on Tue, 12 Aug 2014 17:57:45 GMT]]></title><description><![CDATA[<p>Leg die Variable einfach als</p>
<pre><code class="language-cpp">static SimpleShape FirstShape(50,50);
</code></pre>
<p>an (so wird die Variable nur beim ersten Mal initialisiert und bei weiteren Aufrufen der Funktion behält sie ihren Wert).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2413155</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2413155</guid><dc:creator><![CDATA[Th69]]></dc:creator><pubDate>Tue, 12 Aug 2014 17:57:45 GMT</pubDate></item><item><title><![CDATA[Reply to [Gelöst] Verständnissfrage: An welcher Stelle erstes Objekt erstellen? on Tue, 12 Aug 2014 18:05:51 GMT]]></title><description><![CDATA[<p>Ich dachte bisher das Statische Variablen so ähnlich wie Konstanten sind.<br />
Vielen Dank an alle die sich die Zeit genommen haben mir zu Helfen.<br />
Vorallen Dingen auch so schnell!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2413156</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2413156</guid><dc:creator><![CDATA[0BackBONE0]]></dc:creator><pubDate>Tue, 12 Aug 2014 18:05:51 GMT</pubDate></item><item><title><![CDATA[Reply to [Gelöst] Verständnissfrage: An welcher Stelle erstes Objekt erstellen? on Tue, 12 Aug 2014 18:07:34 GMT]]></title><description><![CDATA[<p>Jop, das geht so nicht. Ich kenn mich mit WinAPI gar net aus, falls das WinAPI ist? Prinzipiell hab ich da sowas im Kopf.</p>
<pre><code>class Rect
{
private:
	int itsXPosition;
    int itsYPosition;
    int itsWidth;
    int itsHeight;
public:
	Rect() : itsXPosition(50), itsYPosition(50), itsWidth(200), itsHeight(200)
	{
	}

	void draw()
	{
		Rectangle(hdc,
                      itsXPosition,
                      itsYPosition,
                      itsWidth,
                      itsHeight);
        itsXPosition += 5;
        itsYPosition += 5;
	}
};

int main()
{
	Rect r;
	for(;;sleep(200))
		r.draw();
}
</code></pre>
<p>Das kompiliert so natürlich nicht, hab ich nur eben runtergetippt. Müsstest du anpassen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2413158</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2413158</guid><dc:creator><![CDATA[out]]></dc:creator><pubDate>Tue, 12 Aug 2014 18:07:34 GMT</pubDate></item></channel></rss>