1

I don't know, which is not correct, the copy constructor or the operator=. I tested with two "tombs", and the printer is working, but at the end of the program the compiler said "debug assertion failed".

#pragma once
#include <cstdio>
#include <cmath>
#include <iostream>
#include <cstdlib>

class tomb {
private:
    double *adat;
    int szam;
public:
    tomb(){
        adat = NULL;
        szam = 0;
    }
    tomb(const tomb &u) {
        adat = u.adat;
        szam = u.szam;
    };
    int meret()const {
        return szam;
    }
    ~tomb() {
        delete[] adat;
    }
    double & operator[](int n) { 
        return adat[n];
    }
    const double & operator[](int n)const {
        return adat[n];
    }
    const tomb &operator=(const tomb &a) {
        adat = a.adat;
        szam = a.szam;
        return *this;
    }
    tomb elso_valahany(int n) {

    }

    void push_back(const double &a) {
        double *tmp;
        tmp = new double[szam+1];

        for (int i = 0; i < szam; i++)
        {
            tmp[i] = adat[i];
        }

        tmp[szam] = a;
        delete[] adat;
        adat = tmp;
        ++szam;
    }
    void Kiir()const {
        for (int i = 0; i < szam; i++)
        {
            std::cout << adat[i] << "\n";
        }
    }
};
Zsembes
  • 11
  • 2

1 Answers1

0

As per the comments, I'll show you how to do a deep copy: every time you copy the class, you will not be copying just the pointer to the data, but the whole vector instead.

Also, for the sake of simplicity, I'll use std::vector:

#pragma once
#include <cstdio>
#include <cmath>
#include <iostream>
#include <cstdlib>
#include <vector>

class tomb {
private:
    std::vector<double> adat;
    int szam;
public:
    tomb(){
        szam = 0;
    }
    tomb(const tomb &u) : adat(u.adat), szam(u.szam)
    {
        adat = u.adat;
        szam = u.szam;
    };
    int meret() const {
        return szam;
    }
    ~tomb() {
    }
    double & operator[](int n) const { 
        return adat[n];
    }
    const double & operator[](int n) const {
        return adat[n];
    }
    tomb& operator=(const tomb &a) {
        adat = a.adat;
        szam = a.szam;
        return *this;
    }


tomb elso_valahany(int n) {

    }

    void push_back(const double &a) {
       adat.push_back(a);
        ++szam;
    }
    void Kiir()const {
        for (int i = 0; i < szam; i++)
        {
            std::cout << adat[i] << "\n";
        }
    }

I haven't compiled/tested, but it should be fine now, as the memory management is done by std copy constructors!

7raiden7
  • 302
  • 1
  • 4
  • 15
  • 1
    Worth adding a note that using `std::vector` makes this whole thing pretty much pointless. It does all the copying and resizing for you, so according to the [Rule of Zero](http://en.cppreference.com/w/cpp/language/rule_of_three) you're better off not implementing destructors, the copy constructor, or the assignment operator. – user4581301 May 07 '17 at 19:36
  • Sure, I partially agree, meaning that, if the role of this class is just to manage the raw data, then the use of std::vector itself solves it all. But if the code here is just a simplified version, and one doesn't want to manage the memory on his/her own than I think it's fine! – 7raiden7 May 07 '17 at 19:39