동적 메모리 할당을 배우고 있습니다. 나는 '클래스 A'가 생성자에서 동적으로 할당 된 배열을 소유해야하는 다음 클래스를 가지고있다. 또한 복사 생성자와 소멸자를 수정해야합니다. 이것은 내가 지금까지 무엇을 가지고 ...생성자의 동적 메모리 할당
기본 생성자 동일한 유형의 지역 변수와 멤버 변수 "배열"이 무시된다 우선 들어#include <iostream>
#ifndef A_HH
#define A_HH
#include "B.hh"
class A {
public:
A() { B *array = new B[12];}
A(const A&) { /* Do not know what to put here..*/ }
~A() { delete[] array;}
private:
//B array[12] ; <- This is the array that I have to modify so it becomes dynamic.
B *array;
} ;
#endif
로를 제쳐두고,'#include '을 헤더 가드 안에 넣어야합니다. –
LiamT
당신은 [규칙 3] (http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three)에 익숙해 져야합니다 (또는 C++ 11, [rule of five] (http://stackoverflow.com/questions/4782757/rule-of-three-becomes-rule-of-five-with-c11)). 또한 [복사본 및 스왑 숙어] (http://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom)를 살펴볼 수도 있습니다. – Aconcagua