{"id":418,"date":"2023-02-25T11:34:19","date_gmt":"2023-02-25T03:34:19","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=418"},"modified":"2023-04-29T21:26:25","modified_gmt":"2023-04-29T13:26:25","slug":"android-ndk-basic-cpp-constructor-destructor-function-copy-constructor","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/android-ndk-basic-cpp-constructor-destructor-function-copy-constructor\/","title":{"rendered":"Android NDK\u57fa\u784018\uff1aC++_\u6784\u9020\u51fd\u6570_\u6790\u6784\u51fd\u6570_\u62f7\u8d1d\u6784\u9020\u51fd\u6570"},"content":{"rendered":"<h2>\u51fd\u6570<\/h2>\n<h3>\u51fd\u6570\u9ed8\u8ba4\u53c2\u6570<\/h3>\n<p><!-- more --><\/p>\n<pre><code class=\"language-cpp\">\/\/\u51fd\u6570\u9ed8\u8ba4\u53c2\u6570\nvoid print(int x, int y = 6, int z = 8) {\n    cout &lt;&lt; x &lt;&lt; endl;\n}\n\n\/\/\u91cd\u8f7d\nvoid print(int x, bool ret) {\n    cout &lt;&lt; x &lt;&lt; endl;\n}\n\nvoid main() {\n    print(20);\n\n    system(&quot;pause&quot;);\n}<\/code><\/pre>\n<h3>\u53ef\u53d8\u53c2\u6570<\/h3>\n<pre><code class=\"language-cpp\">#define _CRT_SECURE_NO_WARNINGS\n#include &lt;stdlib.h&gt;\n#include &lt;iostream&gt;\n#include &lt;stdarg.h&gt;\n\n\/\/\u53ef\u53d8\u53c2\u6570\n\/\/Java\u53ef\u53d8\u53c2\u6570\uff1aint...\nvoid func(int i, ...) {\n    \/\/\u53ef\u53d8\u53c2\u6570\u6307\u9488\n    va_list args_p;\n    \/\/\u5f00\u59cb\u8bfb\u53d6\u53ef\u53d8\u53c2\u6570\uff0ci\u662f\u6700\u540e\u4e00\u4e2a\u56fa\u5b9a\u53c2\u6570\n    va_start(args_p, i);\n    int a = va_arg(args_p, int);\n    char b = va_arg(args_p, char);\n    int c = va_arg(args_p, int);\n    cout &lt;&lt; a &lt;&lt; endl;\n    cout &lt;&lt; b &lt;&lt; endl;\n    cout &lt;&lt; c &lt;&lt; endl;\n    \/\/\u7ed3\u675f\n    va_end(args_p);\n}\n\nvoid main() {\n    func(9, 20, &#039;b&#039;, 30);\n\n    system(&quot;pause&quot;);\n}<\/code><\/pre>\n<pre><code class=\"language-cpp\">\/\/\u5faa\u73af\u8bfb\u53d6\u53ef\u53d8\u53c2\u6570\nvoid func(int i, ...) {\n    \/\/\u53ef\u53d8\u53c2\u6570\u6307\u9488\n    va_list args_p;\n    \/\/\u5f00\u59cb\u8bfb\u53d6\u53ef\u53d8\u53c2\u6570\uff0ci\u662f\u6700\u540e\u4e00\u4e2a\u56fa\u5b9a\u53c2\u6570\n    va_start(args_p, i);\n    int value;\n    while (1) {\n        value = va_arg(args_p, int);\n        if (value &lt;= 0) {\n            break;\n        }\n        cout &lt;&lt; value &lt;&lt; endl;\n    }\n\n    \/\/\u7ed3\u675f\n    va_end(args_p);\n}\n\nvoid main() {\n    func(10, 1, 2, 3);\n\n    system(&quot;pause&quot;);\n}<\/code><\/pre>\n<h2>C++\u7c7b<\/h2>\n<h3>C++\u7c7b\u7684\u666e\u904d\u5199\u6cd5<\/h3>\n<pre><code class=\"language-cpp\">#include &quot;MyTeacher.h&quot;\n\nvoid main() {\n    MyTeacher t;\n    t1.name = &quot;Jack&quot;;\n    t1.age = 20;\n\n    cout &lt;&lt; t.getName() &lt;&lt; endl;\n\n    system(&quot;pause&quot;);\n}<\/code><\/pre>\n<pre><code class=\"language-cpp\">#pragma once\n\nclass MyTeacher {\npublic:\n    int age;\n    char* name;\npublic:\n    void setAge(int age);\n    int getAge();\n    void setName(char* name);\n    char* getName();\n};<\/code><\/pre>\n<pre><code class=\"language-cpp\">#include &quot;MyTeacher.h&quot;\n\nvoid MyTeacher::setAge(int age) {\n    this-&gt;age = age;\n}\n\nint MyTeacher::getAge(){\n    return this-&gt;age;\n}\n\nvoid MyTeacher::setName(char* name) {\n    this-&gt;name = name;\n}\n\nchar* MyTeacher::getName() {\n    return this-&gt;name;\n}<\/code><\/pre>\n<h3>\u6784\u9020\u51fd\u6570<\/h3>\n<pre><code class=\"language-cpp\">class Teacher {\nprivate:\n    char *name;\n    int age;\npublic:\n    \/\/\u65e0\u53c2\u6784\u9020\u51fd\u6570\uff08\u91cd\u5199\u65e0\u53c2\u6784\u9020\u51fd\u6570\uff0c\u5c31\u4f1a\u8986\u76d6\u9ed8\u8ba4\u7684\u65e0\u53c2\u6784\u9020\u51fd\u6570\uff09\n    Teacher() {\n        cout &lt;&lt; &quot;\u65e0\u53c2\u6784\u9020\u51fd\u6570&quot; &lt;&lt; endl;\n    }\n\n    \/\/\u6709\u53c2\u6784\u9020\u51fd\u6570\n    Teacher(char *name, int age) {\n        this-&gt;name = name;\n        this-&gt;age = age;\n        cout &lt;&lt; &quot;\u6709\u53c2\u6784\u9020\u51fd\u6570&quot; &lt;&lt; endl;\n    }    \n};\n\nvoid main() {\n    \/\/Teacher t1;\n    Teacher t2(&quot;Joe&quot;, 20);\n\n    \/\/\u53e6\u5916\u4e00\u79cd\u8c03\u7528\u65b9\u5f0f\n    Teacher t3 = Teacher(&quot;Joe&quot;, 20);\n\n    system(&quot;pause&quot;);\n}<\/code><\/pre>\n<h3>\u6790\u6784\u51fd\u6570<\/h3>\n<pre><code class=\"language-cpp\">\/\/\u6790\u6784\u51fd\u6570\nclass Teacher {\nprivate:\n    char *name;\n    int age;\npublic:\n    \/\/\u65e0\u53c2\u6784\u9020\u51fd\u6570\u8d4b\u9ed8\u8ba4\u503c\n    Teacher() {\n        this-&gt;name = (char*)malloc(100);\n        strcpy(name, &quot;AndroidiOS.cc&quot;);\n        age = 20;\n        cout &lt;&lt; &quot;\u65e0\u53c2\u6784\u9020\u51fd\u6570&quot; &lt;&lt; endl;\n    }\n    \/\/\u6790\u6784\u51fd\u6570\n    \/\/\u5f53\u5bf9\u8c61\u8981\u88ab\u7cfb\u7edf\u91ca\u653e\u65f6\uff0c\u6790\u6784\u51fd\u6570\u88ab\u8c03\u7528\n    \/\/\u4f5c\u7528\uff1a\u5584\u540e\u5904\u7406\n    ~Teacher() {\n        cout &lt;&lt; &quot;\u6790\u6784\u51fd\u6570&quot; &lt;&lt; endl;\n        \/\/\u91ca\u653e\u5185\u5b58\n        free(this-&gt;name);\n    }\n};\n\nvoid func() {\n    Teacher t;  \/\/func\u51fd\u6570\u6267\u884c\u5b8c\u6bd5\u8c03\u7528\u6790\u6784\u51fd\u6570\n}\n\nvoid main(){\n    func();\n\n    system(&quot;pause&quot;);\n}<\/code><\/pre>\n<h3>\u62f7\u8d1d\u6784\u9020\u51fd\u6570<\/h3>\n<pre><code class=\"language-cpp\">\/\/\u62f7\u8d1d\u6784\u9020\u51fd\u6570\nclass Teacher {\nprivate:\n    char *name;\n    int age;\npublic:\n    \/\/\u6709\u53c2\u6784\u9020\u51fd\u6570\u4f1a\u8986\u76d6\u9ed8\u8ba4\u7684\u6784\u9020\u51fd\u6570\n    Teacher(char *name, int age){\n        this-&gt;name = name;\n        this-&gt;age = age;\n        cout &lt;&lt; &quot;\u6709\u53c2\u6784\u9020\u51fd\u6570&quot; &lt;&lt; endl;\n    }\n\n    \/\/\u62f7\u8d1d\u6784\u9020\u51fd\u6570\uff08\u503c\u62f7\u8d1d\uff09\n    \/\/\u9ed8\u8ba4\u62f7\u8d1d\u6784\u9020\u51fd\u6570\uff0c\u5c31\u662f\u503c\u62f7\u8d1d\n    Teacher(const Teacher &amp;obj) {\n        this-&gt;name = obj.name;\n        this-&gt;age = obj.age;\n        cout &lt;&lt; &quot;\u62f7\u8d1d\u6784\u9020\u51fd\u6570&quot; &lt;&lt; endl;\n    }\n\n    void print() {\n        cout &lt;&lt; name &lt;&lt; &quot;,&quot; &lt;&lt; age &lt;&lt; endl;\n    }\n};\n\nTeacher func(Teacher t) {\n    t.print();\n    return t;\n}\n\nvoid main() {\n    Teacher t1(&quot;Joe&quot;, 20);\n\n    \/\/\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u88ab\u8c03\u7528\u7684\u573a\u666f\n    \/\/1.\u58f0\u660e\u65f6\u8d4b\u503c\n    \/\/Teacher t2 = t1;\n    \/\/t2.print();\n    \/\/2.\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\uff0c\u5b9e\u53c2\u7ed9\u5f62\u53c2\u8d4b\u503c\n    func(t1);\n    \/\/3.\u4f5c\u4e3a\u51fd\u6570\u8fd4\u56de\u503c\u8fd4\u56de\uff0c\u7ed9\u53d8\u91cf\u521d\u59cb\u5316\u8d4b\u503c\n    \/\/Teacher t3 = func(t1);\n\n    \/\/\u8fd9\u91cc\u4e0d\u4f1a\u88ab\u8c03\u7528\n    \/\/Teacher t1 ;\n    \/\/Teacher t2;\n    \/\/t1 = t2;\n\n    system(&quot;pause&quot;);\n}<\/code><\/pre>\n<h3>\u6d45\u62f7\u8d1d\uff08\u503c\u62f7\u8d1d\uff09\u95ee\u9898<\/h3>\n<pre><code class=\"language-cpp\">\/\/\u6d45\u62f7\u8d1d\uff08\u503c\u62f7\u8d1d\uff09\u95ee\u9898\nclass Teacher {\nprivate:\n    char *name;\n    int age;\npublic:\n    Teacher(char *name, int age) {\n        this-&gt;name = (char*)malloc(100);\n        strcpy(this-&gt;name, name);\n        this-&gt;age = age;\n        cout &lt;&lt; &quot;\u6709\u53c2\u6784\u9020\u51fd\u6570&quot; &lt;&lt; endl;\n    }\n\n    ~Teacher() {\n        cout &lt;&lt; &quot;\u6790\u6784\u51fd\u6570&quot; &lt;&lt; endl;\n        \/\/\u91ca\u653e\u5185\u5b58\n        free(this-&gt;name);\n    }\n\n    void print() {\n        cout &lt;&lt; name &lt;&lt; &quot;,&quot; &lt;&lt; age &lt;&lt; endl;\n    }\n};\n\nvoid func() {\n    Teacher t1(&quot;Joe&quot;, 20);\n\n    Teacher t2 = t1;\n    t2.print();\n}\n\nvoid main() {\n    func();\n\n    system(&quot;pause&quot;);\n}<\/code><\/pre>\n<h3>\u6df1\u62f7\u8d1d<\/h3>\n<ul>\n<li>\u6d45\u62f7\u8d1d\uff08\u503c\u62f7\u8d1d\uff09\uff1a\u62f7\u8d1d\u7684\u662f\u6307\u9488\u5730\u5740<\/li>\n<li>\u6df1\u62f7\u8d1d\uff1a\u62f7\u8d1d\u7684\u662f\u6307\u9488\u6307\u5411\u7684\u5806\u5185\u5b58\u7a7a\u95f4\u6570\u636e\u5185\u5bb9<\/li>\n<\/ul>\n<pre><code class=\"language-cpp\">\/\/\u6df1\u62f7\u8d1d\nclass Teacher {\nprivate:\n    char *name;\n    int age;\npublic:\n    Teacher(char *name, int age) {\n        int len = strlen(name);\n        this-&gt;name = (char*) malloc(len+1);\n        strcpy(this-&gt;name, name);\n        this-&gt;age = age;\n        cout &lt;&lt; &quot;\u6709\u53c2\u6784\u9020\u51fd\u6570&quot; &lt;&lt; endl;\n    }\n\n    ~Teacher() {\n        cout &lt;&lt; &quot;\u6790\u6784&quot; &lt;&lt; endl;\n        \/\/\u91ca\u653e\u5185\u5b58\n        free(this-&gt;name);\n    }\n\n    \/\/\u6df1\u62f7\u8d1d\n    Teacher(const Teacher &amp;obj) {\n        \/\/\u590d\u5236name\u5c5e\u6027\n        int len = strlen(obj.name);\n        this-&gt;name = (char*) malloc(len+1);\n        strcpy(this-&gt;name, obj.name);\n        this-&gt;age = obj.age;\n    }\n\n    void print() {\n        cout &lt;&lt; name &lt;&lt; &quot;,&quot; &lt;&lt; age &lt;&lt; endl;\n    }\n};\n\nvoid func() {\n    Teacher t1(&quot;Joe&quot;, 20);\n\n    Teacher t2 = t1;\n    t2.print();\n}\n\nvoid main() {\n    func();\n\n    system(&quot;pause&quot;);\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u51fd\u6570 \u51fd\u6570\u9ed8\u8ba4\u53c2\u6570 \/\/\u51fd\u6570\u9ed8\u8ba4\u53c2\u6570 void print(int x, int y = 6, int z = [&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-418","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\/418","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=418"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/418\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=418"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=418"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=418"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}