[an error occurred while processing this directive]
[an error occurred while processing this directive]using namespace std;
class hoge{
int i;
public:
void print(){i = 2; std::cout << (this->i);}
};
main(){
hoge h;
h.print();
}
class A {
public:
void m(void){
delete(this);
}
}
int main(void){
A *ap = new A();
ap->m();
}
とすると、apの指すオブジェクトはdeleteされる…が、apにはメモリ番地が入り続ける。
普通、メソッド読んだら中身が破壊されてる…とは思わないよね?
class hoge{
public:
hoge(void);
~hoge(void);
}
...
int main(void){
hoge* hp = new hoge();
delete(hp);
}
class A; class A_extream : public A;のとき、
A *a = new A_Extream();
if(typeid(*a ) == typeid(A_Extream)){
...
みたいに使える。
#include <iostream>
#include <sstream>
const int buf_len = 100;
int main(void){
std::string data("hoge\nfuga\n\nnero\n");
std::istringstream in(data);
char buf[buf_len];
for(int i = 0;; i++){
void *ret = in.getline(buf, buf_len);
if(ret == NULL) break;
std::cout << i << "th : " << buf << std::endl;
}
}
改行文字は切り落とされます。