åçãªãã¸ã§ã¯ã
ãªãã¸ã§ã¯ããåçå¤æ°ã«ãããã¨ãã§ã¾ãã
- åçå¤æ°ãªã®ã§newæ¼ç®åã使ç¨ãã¾ãã
- ãnew <ã¯ã©ã¹>(å¼æ°..)ãã§ãã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ãåçå¤æ°ã¨ãã¦ä½æããããã®ãã¤ã³ã¿ãè¿ããã¾ãã
- ãã®ã¨ãã¯ã©ã¹ã®ã³ã³ã¹ãã©ã¯ã¿ãå®è¡ããã¾ãã
- ä¸è¦ã«ãªã£ãå¤æ°ã¯deleteã§åé¤ãã¾ãã
- ãã®ã¨ããã¹ãã©ã¯ã¿ãå¼ã³åºããã¾ãã
#include <iostream> // ã¯ã©ã¹å®ç¾© class Kitten { int age; char *name; public : Kitten( char *name, int age ); ~Kitten(); char *getName(); void setName( char *name ); void meow() { std::cout << name << std::endl; } }; Kitten::Kitten( char *name, int age ) { this->age = age; this->name = name; std::cout << name << " created." << std::endl; } Kitten::~Kitten( ) { std::cout << name << " destroyed." << std::endl; } char *Kitten::getName() { return name; } void Kitten::setName( char *name ) { this->name = name; } // ã¡ã¤ã³é¢æ° int main() { // åçã«ãªãã¸ã§ã¯ããçæã // ãªãã¸ã§ã¯ããä½æããããã®ãã¤ã³ã¿ãè¿ãããã // ãã®ã¨ãã³ã³ã¹ãã©ã¯ã¿ãå®è¡ãããã Kitten* kitten = new Kitten( "mii", 1 ); // ãã¤ã³ã¿ã示ããªãã¸ã§ã¯ãã®é¢æ°ãå¼ã³åºãã->ã使ãã kitten->meow(); kitten->setName( "tora" ); kitten->meow(); // åçå¤æ°ãç ´æ£ã // ãã¹ãã©ã¯ã¿ãå¼ã°ããã delete kitten; return 0; }
å®è¡çµæã§ãã
mii created. mii tora tora destroyed.