게시판 즐겨찾기
편집
드래그 앤 드롭으로
즐겨찾기 아이콘 위치 수정이 가능합니다.
c++ 연산자 오버로딩에 관한 기초적인 질문임다!
게시물ID : freeboard_337254짧은주소 복사하기
작성자 : coolwine
추천 : 0
조회수 : 350회
댓글수 : 4개
등록시간 : 2009/04/25 21:02:49
Ary3=Ary1+Ary2;   //객체 더하기 객체를 객체에 넣어라
대충 이런 문장을 실행하게 하는 함수입니다. 근데 실행시에 메모리 에러가 뜨네요! ㅋㅋ
아마....복사생성쪽에서 문제가 발생하는거 같은데 정확히 모르겠네요!
도와주심쇼!


#include <iostream>
#include <ctime>
#include <cstring>
using namespace std;

class CArry{
private:
int *ary;
int count;
public:
CArry(int a=0):count(a){ //생성자
ary=new int[count];
for(int i=0;i<count;i++)
ary[i]=rand()%10;
}
~CArry(){delete [] ary;} //소멸자

CArry(const CArry &a){ //복사생성자
count=a.count;
ary=new int[count];
for(int i=0; i<count;i++)
ary[i]=a.ary[i];
}

CArry &operator+(CArry a){ //+연산자 
CArry P;
int i;
P.ary=new int[count+a.count];
for(i=0;i<count;i++)
P.ary[i]=ary[i];
for(i=count;count<a.count;i++)
P.ary[i]=a.ary[i];
return P;
}

CArry &operator=(CArry a){ //=연산자
count=a.count;
delete [] ary;
ary=new int[count];
for(int i=0; i<count;i++)
ary[i]=a.ary[i];
return (*this);
}

friend ostream &operator<<(ostream &out, const CArry &a);
};

ostream &operator<<(ostream &out, const CArry &a){
for(int i=0;i<a.count;i++)
out<<a.ary[i]<<"\t";
return out;
}



int main()
{
srand((unsigned)time(NULL));
CArry Ary1(3);
CArry Ary2(5);
CArry Ary3(7);
cout<<Ary1<<endl;
cout<<Ary2<<endl;
cout<<Ary3<<endl;
Ary3=Ary1+Ary2;
cout<<Ary1<<endl;
cout<<Ary2<<endl;
cout<<Ary3<<endl;
return 0;
}
전체 추천리스트 보기
새로운 댓글이 없습니다.
새로운 댓글 확인하기
글쓰기
◀뒤로가기
PC버전
맨위로▲
공지 운영 자료창고 청소년보호