简体   繁体   中英

Copy constructor overloading an operator

I have an assignment for class and i received a header file and need to implement the methods of the class. I'm having trouble with implementing the copy constructor and overloading the = operator. Here is some of the header file:

class MontaznaKuca
{
private:
    double kvadratura;
    string bojaFasade;
    string vlasnik;
    bool postojiVrt;
    Kuhinja* kuhinja;
    vector<Soba*> sobe; //Soba is a seperate class defined in another header 
public:
    MontaznaKuca(void);
    MontaznaKuca(double kvadratura, string bojaFasade, string imaVrt);
    MontaznaKuca(const MontaznaKuca& kuca);
    ~MontaznaKuca(void);

    MontaznaKuca& operator=(const MontaznaKuca& kuca);
...

My attempt ate the copy constructor and = operator overloading

MontaznaKuca::MontaznaKuca(const MontaznaKuca& kuca) : sobe(kuca.sobe.size())
{
    kvadratura = kuca.kvadratura;
    bojaFasade = kuca.bojaFasade;
    vlasnik = kuca.vlasnik;
    postojiVrt = kuca.postojiVrt;
    kuhinja = new Kuhinja;
    kuhinja = kuca.kuhinja;
    for (size_t i = 0; i < kuca.sobe.size(); ++i)
        sobe[i] = new Soba(*kuca.sobe[i]);

}

MontaznaKuca& MontaznaKuca::operator=(const MontaznaKuca& kuca) : sobe(kuca.sobe.size())
{
    kvadratura = kuca.kvadratura;
    bojaFasade = kuca.bojaFasade;
    vlasnik = kuca.vlasnik;
    postojiVrt = kuca.postojiVrt;
    kuhinja = new Kuhinja;
    kuhinja = kuca.kuhinja;
    for (size_t i = 0; i < kuca.sobe.size(); ++i)
        sobe[i] = new Soba(*kuca.sobe[i]);
    return *this;
}

I did this by studying some code on the web and visual studio gives me an error

: constructor initializer lists are only allowed on constructor definitions

Can someone explain to me how to do these two functions, because i am completely lost.

The compiler has told you the error: constructor initializer lists are only allowed on constructor definitions.
You're implementing the copy constructor, so constructor initializer lists is not allowed, and it's only allowed on constructor while you try to use it on overloading the assignment operator. I'd recommend you to read these posts: Copy constructor and = operator overload in C++: is a common function possible? What is the copy-and-swap idiom?

MontaznaKuca::MontaznaKuca(const MontaznaKuca& kuca)
{
    kvadratura = kuca.kvadratura;
    bojaFasade = kuca.bojaFasade;
    vlasnik = kuca.vlasnik;
    postojiVrt = kuca.postojiVrt;
    kuhinja = new Kuhinja;
    kuhinja = kuca.kuhinja;

    for (size_t i = 0; i < kuca.sobe.size(); ++i)
        sobe.push_back(new Soba(*kuca.sobe[i]) ); 
}

MontaznaKuca& MontaznaKuca::operator=(const MontaznaKuca& kuca)
{
    // copy-swap idiom is a better approach.
    kvadratura = kuca.kvadratura;
    bojaFasade = kuca.bojaFasade;
    vlasnik = kuca.vlasnik;
    postojiVrt = kuca.postojiVrt;
    delete kuhinja; //deallocate old memory
    kuhinja = new Kuhinja;
    kuhinja = kuca.kuhinja;
    // before allocating new memory you need deallocate old memory
    ...
    for (size_t i = 0; i < kuca.sobe.size(); ++i)
        sobe.push_back(new Soba(*kuca.sobe[i]) );
    return *this;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM