728x90
반응형
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
/* malloc & free *//*
char* MakeStrAdr(int len)
{
char* str = (char*)malloc(sizeof(char) * len);
return str;
}
*/
/* new & delete *//*
char* MakeStrAdr(int len)
{
char* str = new char[len];
return str;
}
*/
class Simple
{
public:
Simple()
{
cout << "Simple Constructor" << endl;
}
int a = 10;
};
int main(void)
{
/* malloc *//*
char* str = MakeStrAdr(20);
strcpy(str, "I am malloc");
cout << str << endl;
free(str);
*/
/* new *//*
char* str = MakeStrAdr(20);
strcpy(str, "I am new");
cout << str << endl;
delete []str;
*/
cout << "case 1: ";
Simple* sp1 = new Simple;
cout << sp1->a << endl; // Access operator ->
/* Reference */
Simple& clone = *sp1;
cout << clone.a << endl; // Access operator .
cout << "case 2: ";
Simple* sp2 = (Simple*)malloc(sizeof(Simple) * 1);
cout << endl << "end of main" << endl;
delete sp1;
free(sp2);
return 0;
}
728x90
반응형
'Language > C & C++' 카테고리의 다른 글
[C++] Constructor & Destructor (0) | 2022.10.17 |
---|---|
[C++]How to create two Class-based object(*, &) (0) | 2022.10.17 |
[C++] Related to Bool datatype (0) | 2022.10.17 |
[C++] Related to Namespace (0) | 2022.10.17 |
[C++] Related to Inline (0) | 2022.10.17 |