본문 바로가기

카테고리 없음

new_delete and const_cast operator

#include "stdafx.h"

#include <iostream>

#include <string>


int *new_int()

{

return (int *)malloc(sizeof(int));

}


int *new_int(int n)

{

int *ptr = 0;


ptr = (int *)malloc(sizeof(int));

*ptr = n;


return ptr;

}


void delete_int(const int *ptr)

{

free(const_cast<int *>(ptr));


return;

}


int main()

{

using namespace std;


const int *ptr = 0;


ptr = new_int(10);


cout << *ptr << endl;


delete_int(ptr);


return 0;

}