FerhatA
FerhatA

Reputation: 69

Swapping Pointer Array (Pointer to Class) Elements

I created an pointer array that represents objects of Class called Sinif. Sinfi *sinif = new Sinif[5] As we know every element has an adres. In my home assignment ,For example I want to swap first element of sinif with second element with its adres. In Degistir Function It changes but only last written is running well. Let's say we want to change Sinif 0 with 1 it changes 0 to 1 but 1 stays as it was. Here is the photos for detailed example.

Before Swapping After Swapping

in here, First User enters Which element to move than where to move and the program Looks for it in every Sinif Array when it finds it The adreses are collected to temp and temp2 after that It assign it.

Sinif *temp; 
Sinif *temp2;

   void Yonetim::SinifDegistir() {
    char DegisecekSinif;
    char YeniYeri;
    cout << "Degisecek Sinif Adi: "; cin >> DegisecekSinif;
    cout << "Yeni Sinif Yerinin Adi: "; cin >> YeniYeri;
    for (int i = 0; i < okul->SayacGet(); i++)
    {
        if (DegisecekSinif == okul->sinif[i].sinif) {
            temp = okul->sinif + i;
            for (int j = 0; j < okul->SayacGet(); j++)
            {
                if (YeniYeri == okul->sinif[j].sinif) {
                    temp2 = okul->sinif + j;
                    Degis(i, j);
                }
            }
        }
    }
}
void Yonetim::Degis(int i, int j) {

    (okul->sinif[i]) = *temp2;
    (okul->sinif[j]) = *temp;
    TabloYazdirma();
}

Upvotes: 2

Views: 196

Answers (2)

FerhatA
FerhatA

Reputation: 69

I used swap(); function which was recommended in comments instead of trying to get every element's adres also I added bool variable called degisti in order to prohibit from turning back to as it was


void Yonetim::SinifDegistir() {
    char DegisecekSinif;
    char YeniYeri;
    bool degisti = false;
    cout << "Degisecek Sinif Adi: "; cin >> DegisecekSinif;
    cout << "Yeni Sinif Yerinin Adi: "; cin >> YeniYeri;
    for (int i = 0; i < okul->SayacGet(); i++)
    {
        if (DegisecekSinif == okul->sinif[i].sinif && !degisti) {
            temp = okul->sinif + i;
            for (int j = 0; j < okul->SayacGet(); j++)
            {
                if (YeniYeri == okul->sinif[j].sinif) {
                    temp2 = okul->sinif + j;
                    Degis(i, j,degisti);
                }
            }
        }
    }
}
void Yonetim::Degis(int i, int j, bool &degisti) {
    swap(okul->sinif[i], okul->sinif[j]); //changes 
    TabloYazdirma();
    degisti = true; //bool says it was swapped
}

Upvotes: 0

Goswin von Brederlow
Goswin von Brederlow

Reputation: 12332

Sinfi *sinif = new Sinif[5] creates an array of 5 Sinif instances in memory. Not pointers but the actual instances.

So when you later say:

(okul->sinif[i]) = *temp2;

What you do is copy the contents of the sinif from one to the other. Now both are the same. So the next line

(okul->sinif[j]) = *temp;

does nothing. *temp has already been overwritten with *temp2 and you are simply copying it back.

Upvotes: 1

Related Questions