Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

請問一下c++的解構函數

0 views
Skip to first unread message

名為勇氣通往希望

unread,
Feb 16, 2002, 12:48:15 PM2/16/02
to
※ 引述《gomi...@bbs.yuntech.edu.tw (捨得)》之銘言:
: 我寫了一個簡單的類別,用來接受輸入的字串我用了new來分配記憶體
: 儲存字串,但是不知道為什麼在我的解構函數裡面我用delete [] pStr
: compile 沒問題,但是執行時就會出現程式錯誤無法解決,找了好久也
: 找不到為什麼,我用的是vc6.0來編譯。
: 請知道問題的人救救偶吧><....麻煩大家了 以下是我的程式碼~
: -----------------------<mystring.h>
: #ifndef MYSTRING_H
: #define MYSTRING_H
: class Mystring
: {
: public:
: Mystring(const char *str);
: ~Mystring();
: void ShowStr();
: private:
: int StrLength;
: char *pStr;
: };
: #endif
: ----------------------<mystring.cpp>
: #include "mystring.h"
: #include <iostream>
: #include <cstdlib>
: Mystring::Mystring(const char *str)
: {
: StrLength=strlen(str);
: pStr=new char[StrLength]; ........是這邊要用StrLength+1嗎..猜的.....
: strcpy(pStr,str);
: }
: void Mystring::ShowString()
: {
: cout <<"The Str is " << pStr<< endl;
: }
: Mystring::~Mystring()
: {
: delete[] pStr;
: cout <<"free mystring allocate space!";
: }









--
[1;31mO [1;32mr [1;33mi [1;34mg [1;35mi [1;36mn [1;31m: [1;36m<不良牛牧場> [1;33mzoo.ee.ntu.edu.tw [1;32m(140.112.18.36) [m
[1;32mWelcome to SimFarm BBS [1;36m-- [m [1;31mFrom : [ [mhhhh.Dorm12.NCTU.edu.tw [1;31m] [m

新生活

unread,
Feb 18, 2002, 3:33:53 AM2/18/02
to
※ 引述《flyingh...@zoo.ee.ntu.edu.tw (名為勇氣通往希望)》之銘言:

> : ----------------------<mystring.cpp>
> : #include "mystring.h"
> : #include <iostream>
> : #include <cstdlib>
> : Mystring::Mystring(const char *str)
> : {
> : StrLength=strlen(str);
> : pStr=new char[StrLength]; ........是這邊要用StrLength+1嗎..猜的.....
> : strcpy(pStr,str);
> : }
對丫,對丫~~~你猜中囉^^..cc

--
[0m [32;1m一張口開蓮花香; [33m一雙手勤作善事;
[0m [31;1m 一顆心有情有意; [37m一輩子歡喜自在。B [0m
--
[m※ Origin: 雲林科技大學藍天使 <bbs.yuntech.edu.tw> [From: 140.125.203.155]

0 new messages