Kopierkonstruktor mit Zeigern



  • Warum geht das hier nicht:

    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    class Event
    {
        public:
        Event(int a_, int b_, int c_);
        Event(const Event &rhs);
        ~Event();
    
        private:
        int *m_a;
        int *m_b;
        int *m_c;
    };
    
    Event::Event(int a_, int b_, int c_)
    :m_a(new int(a_)),
     m_b(new int(b_)),
     m_c(new int(c_))
    {
    
    }
    
    Event::Event(const Event &rhs)
    :m_a(new int(rhs.*m_a)), //<-- !
     m_b(new int(rhs.*m_b)), //<-- !
     m_c(new int(rhs.*m_c))  //<-- !
    {
    
    }
    
    Event::~Event()
    {
        delete m_a;
        delete m_b;
        delete m_c;
    }
    
    int main()
    {
    
        return 0;
    }
    

    Fehlt da eine "( )" um das "*" oder ka....was ich noch machen könnte.

    MfG
    Stromberg


  • Mod

    *rhs.m_a etc.

    mehrere rohe Zeiger in einer Klasse sind in der Regel keine gute Idee. Das ist recht schwer exception-sicher zu bekommen.



  • Ist auch nur ein Übungs Beispiel. Warum gehts jetzt nicht mehr:

    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    class Event
    {
        public:
        Event(int a_, int b_, int c_);
        Event(const Event &rhs);
        ~Event();
        const Event & operator= (const Event & rhs);
        void swap_obj(Event & object);
    
        private:
        int *m_a;
        int *m_b;
        int *m_c;
    };
    
    Event::Event(int a_, int b_, int c_)
    :m_a(new int(a_)),
     m_b(new int(b_)),
     m_c(new int(c_))
    {
    
    }
    
    Event::Event(const Event &rhs)
    :m_a(new int(*rhs.m_a)),
     m_b(new int(*rhs.m_b)),
     m_c(new int(*rhs.m_c))
    {
    
    }
    
    Event::~Event()
    {
        delete m_a;
        delete m_b;
        delete m_c;
    }
    
    const Event & Event::operator= (const Event & rhs)
    {
        Event temp(rhs);
        swap_obj(temp);
    }
    
    void swap_obj(Event & object)
    {
        swap(m_a,object.m_a); //<--!
        swap(m_b,object.m_b); //<--!
        swap(m_c,object.m_c); //<--!
    }
    
    int main()
    {
    
        return 0;
    }
    

    Compiling: C:\MinGW\Andi\test.cpp
    C:\MinGW\Andi\test.cpp: In function void swap_obj(Event&)': C:\\MinGW\\Andi\\test.cpp:52: error:m_a' undeclared (first use this function)
    C:\MinGW\Andi\test.cpp:52: error: (Each undeclared identifier is reported only once for each function it appears in.)
    C:\MinGW\Andi\test.cpp:15: error: int*Event::m_a' is private C:\\MinGW\\Andi\\test.cpp:52: error: within this context C:\\MinGW\\Andi\\test.cpp:53: error:m_b' undeclared (first use this function)
    C:\MinGW\Andi\test.cpp:16: error: int*Event::m_b' is private C:\\MinGW\\Andi\\test.cpp:53: error: within this context C:\\MinGW\\Andi\\test.cpp:54: error:m_c' undeclared (first use this function)
    C:\MinGW\Andi\test.cpp:17: error: `int*Event::m_c' is private
    C:\MinGW\Andi\test.cpp:54: error: within this context
    Process terminated with status 1 (0 minutes, 0 seconds)
    10 errors, 0 warnings

    MfG
    Stromberg



  • void swap_obj(Event & object)
    {
        swap(m_a,object.m_a); //<--!
        swap(m_b,object.m_b); //<--!
        swap(m_c,object.m_c); //<--!
    }
    

    Weil du bei der Funktion die Klasse vergessen hast 😃

    void Event::swap_obj(Event & object)
    {
        swap(m_a,object.m_a); //<--!
        swap(m_b,object.m_b); //<--!
        swap(m_c,object.m_c); //<--!
    }
    

    So sollte es gehen.
    rya.



  • Oh man wie ichs doch hasse wenn mir so peinlich Fehler passieren, und ichs dann auch noch hier poste! man man man....is glaub einfach zu spät für mich.
    Gute Nacht.
    Stromberg



  • Gute Nacht Stromberg :).
    Und mach dir nix draus, das passiert anderen auch. Einfach liegen lassen und am nächsten Tag nochmal draufkucken, wenns so spät is.^^
    rya.


Anmelden zum Antworten