• 카테고리

    질문 & 답변
  • 세부 분야

    프로그래밍 언어

  • 해결 여부

    미해결

클래스 리턴값 관련 질문

22.01.01 21:05 작성 조회수 131

0

안녕하세요! 연습문제 구현하다가 궁금한 점이 생겨 질문드립니다.

클래스를 리턴값으로 받는 멤버변수를 여러개 만들다보니 실수하여 &(참조)를 빼먹으니까 retunr 0;을 만나기도 전이었는데 멤버함수가 리터값을 받자마자 바로 소멸자를 호출하더라구요.

&를 넣으면 문제없이 빌드됩니다.  왜 바로 소멸자를 호출하는지 이해가 안됩니다ㅠㅠ 

< &(참조)를 붙인 상황>

IntArray& remove(const int & ix) // arr[3]

{

//1 10 3 5 7 9

//1 10 3 7 9

 

for (int a = ix; a < m_length - 1; a++)

m_data[a] = m_data[a + 1];

resize(m_length - 1);

 

return *this;

}

 

< &(참조)가 없는 상황>

IntArray remove(const int & ix) // arr[3]

{

//1 10 3 5 7 9

//1 10 3 7 9

 

for (int a = ix; a < m_length - 1; a++)

m_data[a] = m_data[a + 1];

resize(m_length - 1);

 

        return *this;

}

답변 1

답변을 작성해보세요.

1

강민철님의 프로필

강민철

2022.01.02

안녕하세요 :)

아래처럼 

참조자 없이도 연습문제 구현은 가능합니다.

#include <iostream>

class IntArray {
  int * m_data = nullptr;
  unsigned m_length = 0;

  public:
    IntArray(unsigned length = 0) {
      initialize(length);
    }

  IntArray(const std::initializer_list < int > & list) {
      initialize(list.size());
      int count = 0;
      for (auto & e: list)
        m_data[count++] = e;
    }

    ~IntArray() {
      delete[] m_data;
    }

  void initialize(unsigned length) {
    m_length = length;
    if (m_length > 0)
      m_data = new int[m_length];
  }

  void reset() {
    if (m_data) {
      delete[] m_data;
      m_data = nullptr;
    }
    m_length = 0;
  }

  void resize(const unsigned & length) {
    if (length == 0)
      reset();
    else if (m_length < length) {
      int * new_m_data = new int[length];
      for (unsigned i = 0; i < m_length; ++i)
        new_m_data[i] = m_data[i];
      delete[] m_data;
      m_data = new_m_data;
    }
    m_length = length;
  }

  void insertBefore(const int & value,
    const int & ix) {
    resize(m_length + 1);
    for (int i = m_length - 2; i >= ix; --i)
      m_data[i + 1] = m_data[i];
    m_data[ix] = value;
  }

  void remove(const int & ix) {
    for (int i = ix; i < m_length - 1; ++i)
      m_data[i] = m_data[i + 1];
    --m_length;
    if (m_length <= 0)
      reset();
  }

  void push_back(const int & value) {
    insertBefore(value, m_length);
  }

  friend std::ostream & operator << (std::ostream & out,
    const IntArray & arr) {
    for (unsigned i = 0; i < arr.m_length; ++i)
      out << arr.m_data[i] << ' ';
    return out;
  }
};

int main() {
  using namespace std;

  IntArray my_arr { 1, 3, 5, 7, 9 };
  cout << my_arr << endl;

  my_arr.insertBefore(10, 1);
  cout << my_arr << endl;

  my_arr.remove(3);
  cout << my_arr << endl;

  my_arr.push_back(13);
  cout << my_arr << endl;
}

 

문제가 발생한 전체 소스코드를 첨부해주시면 보다 명확하게 문제를 말씀드리겠습니다 :)

감사합니다. 

새해 복 많이 받으세요 :)

 

twinlove11님의 프로필

twinlove11

질문자

2022.01.02

#include <iostream>

#include <array>

using namespace std;

 

class IntArray

{

private:

int m_length = 0;

int *m_data = nullptr;

 

public:

IntArray(const int length)

: m_length(length)

{

m_data = new int[length];

}

 

IntArray(const std::initializer_list<int> &list)

: IntArray(list.size())

{

int count = 0;

for (auto & element : list)

{

m_data[count] = element;

++count;

}

 

}

 

void reset()

{

delete[] this->m_data;

this->m_length = 0;

}

 

IntArray& resize(const int & length_in)

{

if (m_length == length_in)

{

return *this;

}

else if (m_length < length_in) 

{

int *temp = new int[length_in];

 

for (int a = 0; a < m_length; ++a)

temp[a] = m_data[a];

for (int a = m_length; a < length_in; ++a)

temp[a] = 0;

 

m_length = length_in;

delete[] m_data;

m_data = temp;

}

else 

{

m_length = length_in;

int *temp = new int[length_in];

for (int i = 0; i < m_length; ++i)

{

temp[i] = m_data[i];

}

 

m_length = length_in;

delete[] m_data;

m_data = temp;

}

return *this;

}

 

IntArray& insertBefore(const int & value, const int & ix)

{

resize(m_length + 1);

 

for (int a = m_length - 1; a > ix; a--)

{

m_data[a] = m_data[a - 1];

}

for (int a = 0; a < m_length; ++a)

{

cout << m_data[a] << " ";

}

 

m_data[ix] = value;

 

return *this;

}

//*********************

IntArray remove(const int & ix)

{

for (int a = ix; a < m_length - 1; a++)

m_data[a] = m_data[a + 1];

resize(m_length - 1);

 

return *this;

}

IntArray& push_back(const int & ix)  

{

resize(m_length + 1);

m_data[m_length - 1] = ix;

 

return *this;

}

 

friend ostream& operator << (ostream & out, const IntArray & int_arr)

{

for (int i = 0; i < int_arr.m_length; i++)

out << int_arr.m_data[i] << " ";

 

return out;

}

 

~IntArray()

{

delete[] m_data;

}

 

};

 

int main()

{

IntArray my_arr{ 1,3,5,7,9 };

cout << my_arr << endl;

my_arr.insertBefore(10, 1);

cout << my_arr << endl;

my_arr.remove(3);

cout << my_arr << endl;

my_arr.push_back(13);

cout << my_arr << endl;

 

return 0;

}

IntArray로 리턴값을 받는 부분(//******** 으로 표시한 코드) 보시면 참조자 없이 디버깅 해보면  return *this 가 끝나면 소멸자를 호출하는데 참조자가 있을 경우엔 소멸자가 호출이 안되고 정상적으로 잘 작동합니다. 혹시 어떤 원리인지 알 수 있을까요??

강민철님의 프로필

강민철

2022.01.03

작성해주신 위 코드의 경우, 참조자가 있어야 소멸자가 호출됩니다.

참조자 리턴하는 것은 연산을 수행하는 객체 그 자체를 리턴하는 것이고,

참조자 없이 리턴하는 것은 객체의 사본을 리턴합니다.

참조자 없이 리턴한 객체는 소멸자의 대상이 아닙니다.

 

즉, 참조자로 리턴하셔야 소멸자의 대상이 되기 때문에

참조자로 리턴하셔야 해당 객체에 대한 소멸자가 호출 됩니다.

(insertBefore, push_back에서 참조자를 빼면 빌드 오류가 발생하는 것과 같은 이유입니다.)

 

추가 질문이 있으시다면 편하게 댓글 달아주세요! :)

감사합니다.