{"id":419,"date":"2023-02-25T11:35:17","date_gmt":"2023-02-25T03:35:17","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=419"},"modified":"2023-04-29T21:26:11","modified_gmt":"2023-04-29T13:26:11","slug":"android-ndk-basic-cpp-initialize-constructor-properties-new-delete-static-member-this-pointer","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/android-ndk-basic-cpp-initialize-constructor-properties-new-delete-static-member-this-pointer\/","title":{"rendered":"Android NDK\u57fa\u784019\uff1aC++_\u6784\u9020\u51fd\u6570\u5c5e\u6027\u521d\u59cb\u5316_new(delete)_\u9759\u6001\u6210\u5458_this\u6307\u9488"},"content":{"rendered":"<h2>\u6784\u9020\u51fd\u6570\u5c5e\u6027\u521d\u59cb\u5316<\/h2>\n<p><!-- more --><\/p>\n<pre><code class=\"language-cpp\">\/\/\u6784\u9020\u51fd\u6570\u7684\u5c5e\u6027\u521d\u59cb\u5316\u5217\u8868\nclass Teacher {\nprivate:\n    char* name;\npublic:\n    Teacher(char* name) {\n        this-&gt;name = name;\n        cout &lt;&lt; &quot;Teacher\u6709\u53c2\u6784\u9020\u51fd\u6570&quot; &lt;&lt; endl;\n    }\n\n    ~Teacher() {\n        cout &lt;&lt; &quot;Teacher\u6790\u6784\u51fd\u6570&quot; &lt;&lt; endl;\n    }\n\n    char* getName() {\n        return this-&gt;name;\n    }\n};\n\nclass Student {\nprivate:\n    int id;\n    \/\/\u5c5e\u6027\u5bf9\u8c61\n    \/\/Teacher t = Teacher(&quot;Joe&quot;);\n    Teacher t1;\n    Teacher t2;\npublic:\n    Student(int id, char *t1_name, char* t2_name) : t1(t1_name), t2(t2_name) {\n        this-&gt;id = id;\n        cout &lt;&lt; &quot;Student\u6709\u53c2\u6784\u9020\u51fd\u6570&quot; &lt;&lt; endl;\n    }\n\n    void print() {\n        cout &lt;&lt; id &lt;&lt; &quot;,&quot; &lt;&lt; t1.getName() &lt;&lt;&quot;,&quot; &lt;&lt; t2.getName() &lt;&lt; endl;\n    }\n\n    ~Student() {\n        cout &lt;&lt; &quot;Student\u6790\u6784\u51fd\u6570&quot; &lt;&lt; endl;\n    }\n};\n\nvoid func() {\n    \/\/Teacher\u6784\u9020 -&gt; Student\u6784\u9020 -&gt; Student\u6790\u6784 -&gt; Teacher\u6790\u6784\n    Student s1(10, &quot;Miss Zhao&quot;, &quot;Mr Qian&quot;);\n    \/\/Student s2(20, &quot;Mr Sun&quot;, &quot;Miss Li&quot;);\n    s1.print();\n    \/\/s2.print();\n}\n\nvoid main() {\n    func();\n\n    system(&quot;pause&quot;);\n}<\/code><\/pre>\n<h2>new(delete)\u52a8\u6001\u5185\u5b58\u5206\u914d<\/h2>\n<ul>\n<li>C++ \u901a\u8fc7new(delete)\u52a8\u6001\u5185\u5b58\u5206\u914d<\/li>\n<li>C \u901a\u8fc7malloc(free)\u52a8\u6001\u5185\u5b58\u5206\u914d<\/li>\n<\/ul>\n<pre><code class=\"language-cpp\">class Teacher {\nprivate:\n    char* name;\npublic:\n    Teacher(char* name) {\n        this-&gt;name = name;\n        cout &lt;&lt; &quot;Teacher\u6709\u53c2\u6784\u9020\u51fd\u6570&quot; &lt;&lt; endl;\n    }\n\n    ~Teacher() {\n        cout &lt;&lt; &quot;Teacher\u6790\u6784\u51fd\u6570&quot; &lt;&lt; endl;\n    }\n\n    void setName(char* name) {\n        this-&gt;name = name;\n    }\n\n    char* getName() {\n        return this-&gt;name;\n    }\n};\n\nvoid func() {\n    \/\/C++    \n    \/\/\u4f1a\u8c03\u7528\u6784\u9020\u548c\u6790\u6784\u51fd\u6570\n    Teacher *t1 = new Teacher(&quot;Joe&quot;);\n    cout &lt;&lt; t1-&gt;getName() &lt;&lt; endl;\n    \/\/\u91ca\u653e\n    delete t1;\n\n    \/\/C\n    \/\/\u4e0d\u4f1a\u8c03\u7528\u6784\u9020\u548c\u6790\u6784\u51fd\u6570\n    \/\/Teacher *t2 = (Teacher*) malloc(sizeof(Teacher));\n    \/\/t2-&gt;setName(&quot;Joe&quot;);\n    \/\/free(t2);\n}\n\nvoid main() {\n    func();\n    \/\/\u6570\u7ec4\u7c7b\u578b\n    \/\/C\n    \/\/int *p1 = (int*) malloc(sizeof(int) * 10);\n    \/\/p1[0] = 1;\n    \/\/free(p1);\n\n    \/\/C++\n    int *p2 = new int[10];\n    p2[0] = 2;\n    \/\/\u91ca\u653e\u6570\u7ec4 []\n    delete[] p2;\n\n    system(&quot;pause&quot;);\n}<\/code><\/pre>\n<h2>static \u9759\u6001\u5c5e\u6027\u548c\u65b9\u6cd5<\/h2>\n<pre><code class=\"language-cpp\">\/\/static \u9759\u6001\u5c5e\u6027\u548c\u65b9\u6cd5\nclass Teacher {\npublic:\n    char* name;\n    \/\/\u8ba1\u6570\u5668\n    static int total;\npublic:\n    Teacher(char* name) {\n        this-&gt;name = name;        \n        cout &lt;&lt; &quot;Teacher\u6709\u53c2\u6784\u9020\u51fd\u6570&quot; &lt;&lt; endl;\n    }\n\n    ~Teacher() {\n        cout &lt;&lt; &quot;Teacher\u6790\u6784\u51fd\u6570&quot; &lt;&lt; endl;\n    }\n\n    void setName(char* name) {\n        this-&gt;name = name;\n    }\n\n    char* getName() {\n        return this-&gt;name;\n    }\n\n    \/\/\u8ba1\u6570\uff0c\u9759\u6001\u51fd\u6570\n    static void count() {\n        total++;        \n        cout &lt;&lt; total &lt;&lt; endl;\n    }\n};\n\n\/\/\u9759\u6001\u5c5e\u6027\u521d\u59cb\u5316\u8d4b\u503c\nint Teacher::total = 9;\n\nvoid main() {\n    Teacher::total++;\n    cout &lt;&lt; Teacher::total &lt;&lt; endl;\n    \/\/\u76f4\u63a5\u901a\u8fc7\u7c7b\u540d\u8bbf\u95ee\n    Teacher::count();\n\n    \/\/\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5bf9\u8c61\u540d\u8bbf\u95ee\n    Teacher t(&quot;Joe&quot;);\n    t.count();\n\n    system(&quot;pause&quot;);\n}<\/code><\/pre>\n<h2>this\u6307\u9488<\/h2>\n<pre><code class=\"language-cpp\">\/\/this\uff0c\u5f53\u524d\u5bf9\u8c61\u7684\u6307\u9488\n\/\/\u51fd\u6570\u662f\u5171\u4eab\u7684\uff0c\u5fc5\u987b\u8981\u6709\u80fd\u591f\u6807\u8bc6\u5f53\u524d\u5bf9\u8c61\u662f\u8c01\u7684\u529e\u6cd5\nclass Teacher {\nprivate:\n    char* name;\n    int age;\npublic:\n    Teacher(char* name,int age) {\n        this-&gt;name = name;\n        this-&gt;age = age;\n        cout &lt;&lt; &quot;Teacher\u6709\u53c2\u6784\u9020\u51fd\u6570&quot; &lt;&lt; endl;\n    }\n\n    ~Teacher() {\n        cout &lt;&lt; &quot;Teacher\u6790\u6784\u51fd\u6570&quot; &lt;&lt; endl;\n    }\n\n    \/\/\u5e38\u51fd\u6570\uff0c\u4fee\u9970\u7684\u662fthis\n    \/\/\u65e2\u4e0d\u80fd\u6539\u53d8\u6307\u9488\u7684\u503c\uff0c\u53c8\u4e0d\u80fd\u6539\u53d8\u6307\u9488\u6307\u5411\u7684\u5185\u5bb9\n    \/\/const Teacher* const this\n    void print() const {\n        printf(&quot;%#x\\n&quot;, this);\n        \/\/\u4e0d\u80fd\u6539\u53d8\u5c5e\u6027\u7684\u503c\n        \/\/this-&gt;name = &quot;Mr Ye&quot;;\n        \/\/\u4e0d\u80fd\u6539\u53d8this\u6307\u9488\u7684\u503c\n        \/\/this = (Teacher*)0x000008;\n        cout &lt;&lt; this-&gt;name &lt;&lt; &quot;,&quot; &lt;&lt; this-&gt;age &lt;&lt; endl;\n    }\n\n    void normal_print() {        \n        cout &lt;&lt; this-&gt;name &lt;&lt; &quot;,&quot; &lt;&lt; this-&gt;age &lt;&lt; endl;        \n    }\n};\n\nvoid main() {\n    Teacher t1(&quot;Joe&quot;, 20);\n    const Teacher t2(&quot;Ann&quot;, 18);\n    \/\/t2.normal_print(); \u5e38\u91cf\u5bf9\u8c61\u53ea\u80fd\u8c03\u7528\u5e38\u91cf\u51fd\u6570\uff0c\u4e0d\u80fd\u8c03\u7528\u975e\u5e38\u91cf\u51fd\u6570\n    \/\/\u5e38\u51fd\u6570\uff0c\u5f53\u524d\u5bf9\u8c61\u4e0d\u80fd\u88ab\u4fee\u6539\uff0c\u9632\u6b62\u6570\u636e\u6210\u5458\u88ab\u975e\u6cd5\u8bbf\u95ee\n    printf(&quot;%#x\\n&quot;, &amp;t1);\n    t1.print();\n\n    printf(&quot;%#x\\n&quot;, &amp;t2);\n    t2.print();\n\n    system(&quot;pause&quot;);\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6784\u9020\u51fd\u6570\u5c5e\u6027\u521d\u59cb\u5316 \/\/\u6784\u9020\u51fd\u6570\u7684\u5c5e\u6027\u521d\u59cb\u5316\u5217\u8868 class Teacher { private: char* [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-419","post","type-post","status-publish","format-standard","hentry","category-android-ndk"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/419","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=419"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/419\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=419"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=419"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}