Du bist nicht angemeldet.

Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!

Werbeanzeige

grek40

Alter Hase

Beiträge: 1 491

Wohnort: Dresden

  • Private Nachricht senden

31

28.09.2007, 20:02

Get / Set Funktionen sind keine Sünden - damit hält man sich zugleich die Möglichkeit offen, andere Variablen o.ä. ebenfalls zu bearbeiten, wenn diese direkt im Zusammenhang mit der abgefragten / geänderten Var stehen.

Was schonwieder stilmäßig weniger eindeutig ist, was ich aber trotzdem manchmal verwende, wenn ich es bequem haben will und eine Var jederzeit unproblematisch geändert werden darf ist folgendes:

C-/C++-Quelltext

1
2
3
4
5
6
7
class X;
{
public:
  int& Y()  {return _y;}
private:
  int _y;
}


Anmerkung:
Auf den Grundaufbau einer Klasse (ctor, dtor,...) hab ich jetzt mal verzichtet^^


// €dit @ properties

Ich glaub das gehört nur in den C++ CLI Bereich

// €dit 2

Wenn ich wirklich Kontrolle haben will und nich einfach ne Referenz raus schmeiße dann bin ich trotzdem zu faul, jedes mal get und set zu schreiben - das sieht dann so aus (bei Bedarf mit weiterem Code rundrum):

C-/C++-Quelltext

1
2
3
4
5
6
7
8
class X;
{
public:
  void Y(int y)  {_y=y;}
  int  Y()  {return _y;}
private:
  int _y;
}

32

28.09.2007, 20:11

Ehm das ist nicht nur stilistisch ekelhaft sondern untergräbt förmlich den Sinn von private. Dann kannst du es auch direkt lassen.

C-/C++-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
template <typename T>
class foo
{
    T m_data;

public:
    foo(T const& data) : m_data(data) {}

public:
    T const& get() const { return m_data; }
    void set(T const& data) { m_data = data; }
};
... wobei man das bei primitiven Typen ohne Referenz macht:

C-/C++-Quelltext

1
2
3
4
5
6
7
8
9
10
11
class foo
{
    int m_data;

public:
    foo(int data) : m_data(data) {}

public:
    int get() const { return m_data; }
    void set(int data) { m_data = data; }
};
... Es bringt performancetechnisch eher Nachteile primitive Datentypen per Referenz zurückzugeben. Einfaches Kopieren ist da i.O. ...
Devil Entertainment :: Your education is our inspiration
Der Spieleprogrammierer :: Community Magazin
Merlin - A Legend awakes :: You are a dedicated C++ (DirectX) programmer and you have ability to work in a team? Contact us!
Siedler II.5 RttR :: The old settlers-style is comming back!

Also known as (D)Evil

33

28.09.2007, 20:12

jop hatte auch erst data(T const&), data() die Funktionen genannt. Ist allerdings wichtig das man aufpasst das die Variable dann nicht auch noch data heißt :D
Devil Entertainment :: Your education is our inspiration
Der Spieleprogrammierer :: Community Magazin
Merlin - A Legend awakes :: You are a dedicated C++ (DirectX) programmer and you have ability to work in a team? Contact us!
Siedler II.5 RttR :: The old settlers-style is comming back!

Also known as (D)Evil

grek40

Alter Hase

Beiträge: 1 491

Wohnort: Dresden

  • Private Nachricht senden

34

28.09.2007, 20:26

Zitat von »"Deviloper"«

Ehm das ist nicht nur stilistisch ekelhaft sondern untergräbt förmlich den Sinn von private. Dann kannst du es auch direkt lassen.

Ich find es schon einen Unterschied, da so der Zugriff von der Var selbst getrennt ist - muss allerdings zustimmen, dass diese Trennung eher symbolisch ist als dass es wirklich nen Unterschied macht.

Hab ja extra im 2. Edit noch nen 'normales' Beispiel gebracht^^

$nooc

Alter Hase

  • »$nooc« ist der Autor dieses Themas

Beiträge: 873

Wohnort: Österreich / Kärnten

Beruf: Schüler

  • Private Nachricht senden

35

28.09.2007, 21:11

also wenn ich das jetz so versteh..

wenn ich ne private variable habe.. kann ich ohne bedenken eine referenz über eine funktion zurückgeben, ist das richtig? bei primitiven typen kopiert man sie einfach auf den stack.. ansonst referenz..

also ist

C-/C++-Quelltext

1
2
3
4
5
6
7
8
9
10
class window
{
private:
    HWND m_hwnd;
public: 
    HWND GetHwnd (void) { return m_hwnd; }

        // oder von mir aus:

    HWND& GetHwnd (void) { return m_hwnd; }
}


kein blödsinn?


ich versteh eines nicht bei deinem code devil, nämlich const vor und nach dem funktionskopf.. O_o

C-/C++-Quelltext

1
2
3
T const& get() const { return m_data; }

// = Datentyp constRef Funktionsname() const { return m_data; }
Am Anfang der Weisheit steht die eigene Erkenntnis, dass man selbst nichts weiß! - Sokrates

36

28.09.2007, 21:40

Ehm wenn du eine Referenz zurück gibst, kann man den Inhalt der Membervariable verändern. D.h. kann die Funktion keine const-Funktion sein. Somit kannst du sie auch nicht für ein const Objekt benutzen.

C-/C++-Quelltext

1
void foo(const bar& inst) { std::cout << inst.get_handle() << std::endl; }
würde somit fehlschlagen.

Wenn man eine Kopie zurück gibt, kann die Funktion auch const. sein, da die Kopie nichts an dem Wert der Original-Variable verändern kann(ok doch über nen gaaanz miesen hack schon :D ) ... damit veränderst du nichts am Objekt und obiger Code klappt :)
=>

C-/C++-Quelltext

1
2
3
4
5
6
7
8
9
class Window
{
    ::HWND m_hWnd;

public:
    ::HWND get_handle() const  { return m_hWnd; }
    // oder von mir aus:

    ::HWND const & get_handle() const { return m_hWnd; }
};
:)
Devil Entertainment :: Your education is our inspiration
Der Spieleprogrammierer :: Community Magazin
Merlin - A Legend awakes :: You are a dedicated C++ (DirectX) programmer and you have ability to work in a team? Contact us!
Siedler II.5 RttR :: The old settlers-style is comming back!

Also known as (D)Evil

$nooc

Alter Hase

  • »$nooc« ist der Autor dieses Themas

Beiträge: 873

Wohnort: Österreich / Kärnten

Beruf: Schüler

  • Private Nachricht senden

37

29.09.2007, 10:50

so.. ich hab jetzt was zusammengeschrieben... :D


Forms.hpp

C-/C++-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#if !defined (_FORMS_HPP_)
#define _FORMS_HPP_

    // i n c l u d e s

#include <windows.h>

namespace Forms
{

    
     // class Window

     

    class Window
    {
    protected:
            // m e m b e r _ v a r i a b l e s

        HWND        m_hWndWindow;
        HWND        m_hWndParent;
        HMENU       m_hMenu;

        HINSTANCE   m_hInstance;
        LPCWSTR     m_WindowName;
    
            // m e m b e r _  f u n c t i o n s

        void    Close   (void);
    
    public:
            // m e m b e r _ f u n ct i o n s

                Window      (void);
                ~Window     (void) { Close(); }
        int     Create      (const WNDCLASSEX& wc, DWORD dwExStyle, LPCWSTR lpWindowName, DWORD dwStyle, int X, int Y, int nWidth, int nHeight, HWND hWndParent, HMENU hMenu, LPVOID lpParam);
        void    Show        (int nCmdShow);
    
            // g e t

        const HWND          GetHandle       (void) { return m_hWndWindow; }
        const HWND          GetParent       (void) { return m_hWndParent; }
        const HMENU         GethMenu        (void) { return m_hMenu; }
        const HINSTANCE     GethInstance    (void) { return m_hInstance; }
        const LPCWSTR       GetName         (void) { return m_WindowName; }
    }; // class WINDOW


    
     // class button

     

    class Button
    {
    protected:
            // m e m b e r _ v a r i a b l e s

        HWND        m_hWndButton;
        HWND        m_hWndParent;

        HINSTANCE   m_hInstance;
        LPCWSTR     m_ButtonName;


    public:
            // m e m b e r _ f u n c t i o n s

                Button      (void);
                ~Button     (void);
        int     Create      (DWORD dwExStyle, LPCWSTR lpClassName, LPCWSTR lpBtnText, DWORD dwStyle, int X, int Y, int nWidth, int nHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance, LPVOID lpParam);

            // g e t

        const HWND  GethWnd     (void) { return m_hWndButton; }
        const HWND  GetParent   (void) { return m_hWndParent; }
    }; // class Button

} // namespace Forms


#endif


CWindow.cpp

C-/C++-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
    // i n c l u d e s

#include "Forms.hpp"

using namespace Forms;

// class CWindow

    // m e m b e r _ f u n c t i on s

Window::Window (void)
: m_hWndWindow(NULL), m_hWndParent(NULL), m_hMenu(NULL)
{} // Window::WINDOW()


int Window::Create(const WNDCLASSEX& wc, DWORD dwExStyle, LPCWSTR lpWindowName, DWORD dwStyle, int X, int Y, int nWidth, int nHeight, HWND hWndParent, HMENU hMenu, LPVOID lpParam)
{
    m_WindowName = lpWindowName;
    m_hInstance = wc.hInstance;

    if(!RegisterClassEx(&wc))
    { return 1; }

    m_hWndWindow = CreateWindowEx(
        dwExStyle,
        wc.lpszClassName,
        m_WindowName,
        dwStyle,
        X, Y,
        nWidth, nHeight,
        hWndParent,
        hMenu,
        wc.hInstance, 
        lpParam);

    if(!m_hWndWindow)
    { return 1; }

    ShowWindow(m_hWndWindow, SW_HIDE);
    UpdateWindow(m_hWndWindow);

    return 0;
} // Window::Create()


void Window::Show (int nCmdShow)
{
    ShowWindow(m_hWndWindow, nCmdShow);
    //UpdateWindow(m_hWndWindow);

} // Window::Show()


void Window::Close (void)
{
    UnregisterClass(m_WindowName, m_hInstance);
    PostQuitMessage(0);
} // Window::Close()


CButton.cpp

C-/C++-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
    // i n c l u d e s

#include "Forms.hpp"

using namespace Forms;

// class CButton

Button::Button(void)
: m_hWndButton(NULL), m_hWndParent(NULL)
{}

Button::~Button(void)
{}

int Button::Create (DWORD dwExStyle, LPCWSTR lpClassName, LPCWSTR lpBtnText, DWORD dwStyle, int X, int Y, int nWidth, int nHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance, LPVOID lpParam)
{
    m_hWndParent    = hWndParent;
    m_hInstance     = hInstance;

    m_hWndButton = CreateWindowEx(
        dwExStyle, 
        lpClassName, 
        lpBtnText, 
        dwStyle, 
        X, Y, 
        nWidth, nHeight, 
        m_hWndParent, 
        hMenu, 
        m_hInstance, 
        lpParam);

    return 0;
} // Create()




was sagt ihr?
ich hätte da schon ne frage.. zb ob der mit den Get- Funktionen ok ist? und ob man die nicht inline machen würde, da sie so klein sind..
Am Anfang der Weisheit steht die eigene Erkenntnis, dass man selbst nichts weiß! - Sokrates

$nooc

Alter Hase

  • »$nooc« ist der Autor dieses Themas

Beiträge: 873

Wohnort: Österreich / Kärnten

Beruf: Schüler

  • Private Nachricht senden

38

30.09.2007, 11:43

keine kritik oder so? :?
Am Anfang der Weisheit steht die eigene Erkenntnis, dass man selbst nichts weiß! - Sokrates

grek40

Alter Hase

Beiträge: 1 491

Wohnort: Dresden

  • Private Nachricht senden

39

30.09.2007, 13:01

Wie kommst du darauf, dass deine get-Funktionen nicht inline wären??

$nooc

Alter Hase

  • »$nooc« ist der Autor dieses Themas

Beiträge: 873

Wohnort: Österreich / Kärnten

Beruf: Schüler

  • Private Nachricht senden

40

30.09.2007, 17:12

sie sind ja nicht als inline deklariert...

C-/C++-Quelltext

1
2
3
4
5
6
// g e t

        const HWND            GetHandle        (void) { return m_hWndWindow; }
        const HWND            GetParent        (void) { return m_hWndParent; }
        const HMENU            GethMenu        (void) { return m_hMenu; }
        const HINSTANCE        GethInstance    (void) { return m_hInstance; }
        const LPCWSTR        GetName            (void) { return
Am Anfang der Weisheit steht die eigene Erkenntnis, dass man selbst nichts weiß! - Sokrates

Werbeanzeige