{"id":417,"date":"2023-02-25T11:33:07","date_gmt":"2023-02-25T03:33:07","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=417"},"modified":"2023-04-29T21:26:49","modified_gmt":"2023-04-29T13:26:49","slug":"android-ndk-basic-cpp-namespaces-class-quote","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/android-ndk-basic-cpp-namespaces-class-quote\/","title":{"rendered":"Android NDK\u57fa\u784017\uff1aC++_\u547d\u540d\u7a7a\u95f4_\u7c7b_\u5f15\u7528"},"content":{"rendered":"<p>C\u4e0eC++\u7684\u5173\u7cfb<\/p>\n<ul>\n<li>1.C++\u53ef\u4ee5\u4e0eC\u4ee3\u7801\u8fdb\u884c\u6df7\u7f16<\/li>\n<li>2.C++\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u8bed\u8a00\u3001C\u9762\u5411\u8fc7\u7a0b\u7684\u7f16\u7a0b<\/li>\n<li>3.C++\u5bf9C\u7684\u4e00\u4e2a\u589e\u5f3a<\/li>\n<\/ul>\n<p><!-- more --><\/p>\n<h2>\u547d\u540d\u7a7a\u95f4<\/h2>\n<pre><code class=\"language-c\">#include &lt;stdlib.h&gt;\n#include &lt;iostream&gt;\n\n\/\/\u6807\u51c6\u547d\u540d\u7a7a\u95f4(\u5305\u542b\u5f88\u591a\u6807\u51c6\u7684\u5b9a\u4e49)\nusing namespace std;\n\/\/\u547d\u540d\u7a7a\u95f4\u7c7b\u4f3c\u4e8eJava\u4e2d\u5305\uff08\u5f52\u7c7b\uff09\n\n\/\/\u81ea\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\nnamespace NSP_A {\n    int a = 9;\n\n    struct Teacher t;\n    {\n        char name[20];\n        int age;\n    };\n\n    struct Student {\n        char name[20];\n        int age;\n    };\n}\n\nnamespace NSP_B {\n    int a = 12;\n\n    \/\/\u547d\u540d\u7a7a\u95f4\u5d4c\u5957\n    namespace NSP_C {\n        int c = 90;        \n    }\n}\n\nvoid main() {\n    printf(&quot;Hello World\\n&quot;);\n    \/\/\u8fd0\u7b97\u7b26\u91cd\u8f7d\n    \/\/std::cout &lt;&lt; &quot;this is c plus plus&quot; &lt;&lt; std::endl;\n    cout &lt;&lt; &quot;this is c plus plus&quot; &lt;&lt; endl;\n    \/\/\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\n    \/\/::\u8bbf\u95ee\u4fee\u9970\u7b26\n    cout &lt;&lt; NSP_A::a &lt;&lt; endl;\n    cout &lt;&lt; NSP_B::a &lt;&lt; endl;\n\n    cout &lt;&lt; NSP_B::NSP_C::c &lt;&lt; endl;\n\n    struct NSP_A::Teacher t;\n    t.age = 25;\n\n    \/\/\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u7ed3\u6784\u4f53\n    using NSP_A::Student;\n    Student s;\n    s.age = 10;\n\n    system(&quot;pause&quot;);\n}<\/code><\/pre>\n<h2>\u7c7b\u548c\u7ed3\u6784\u4f53<\/h2>\n<pre><code class=\"language-c\">\/\/\u7c7b\n#define PI 3.14\n\n\/\/\u5706\nclass MyCircle {\n\/\/\u5c5e\u6027\uff08\u5171\u7528\u6743\u9650\u8bbf\u95ee\u4fee\u9970\u7b26\uff09\nprivate:\n    double r;\n    double s;\n\npublic:\n    void setR(double r) {\n        this-&gt;r = r;\n    }\n\n    \/\/\u83b7\u53d6\u9762\u79ef\n    double getS() {\n        return PI * r * r;\n    }\n};\n\nvoid main() {\n    MyCircle c;\n    c.setR(4);\n\n    cout &lt;&lt; &quot;\u5706\u7684\u9762\u79ef\uff1a&quot; &lt;&lt; c.getS() &lt;&lt; endl;\n\n    system(&quot;pause&quot;);\n}<\/code><\/pre>\n<pre><code class=\"language-c\">\/\/\u7ed3\u6784\u4f53\nstruct MyTeacher {\npublic:\n    char name[20];\n    int age;\npublic:\n    void say() {\n        cout &lt;&lt; this-&gt;age &lt;&lt; &quot;\u5c81&quot; &lt;&lt; endl;\n    }\n};\n\nvoid main() {\n    MyTeacher t;  \/\/struct\u5173\u952e\u5b57\u53ef\u4ee5\u7701\u7565\n    t.age = 10;\n    t.say();\n    system(&quot;pause&quot;);\n}<\/code><\/pre>\n<h2>\u5e03\u5c14\u7c7b\u578b<\/h2>\n<p>C\u8bed\u8a00\u6ca1\u6709\u5e03\u5c14\u7c7b\u578b\uff0c\u5728C++\u4e2d\u624d\u6709<\/p>\n<pre><code class=\"language-c\">\/\/\u5e03\u5c14\u7c7b\u578b\nvoid main() {\n    \/\/bool isSingle = true;\n    bool isSingle = 17;\n    \/\/false -17\n\n    if (isSingle) {\n        cout &lt;&lt; &quot;\u5355\u8eab&quot; &lt;&lt; endl;\n        cout &lt;&lt; sizeof(bool) &lt;&lt; endl;\n    } else {\n        cout &lt;&lt; &quot;\u6709\u5bf9\u8c61&quot; &lt;&lt; endl;\n    }\n\n    int a = 10, b = 20;\n    ((a &gt; b) ? a : b) = 30;  \/\/\u4e09\u5143\u8fd0\u7b97\u7b26\u53ef\u4ee5\u6210\u4e3a\u5de6\u503c\uff0c\u8fd9\u5728C\/Java\u4e2d\u662f\u4e0d\u53ef\u884c\u7684\n    cout &lt;&lt; b &lt;&lt; endl;\n\n    system(&quot;pause&quot;);\n}<\/code><\/pre>\n<h2>\u5f15\u7528<\/h2>\n<h3>\u5f15\u7528\uff1a\u5185\u5b58\u7a7a\u95f4\u5730\u5740\u7684\u522b\u540d<\/h3>\n<pre><code class=\"language-c\">void main() {\n    \/\/\u53d8\u91cf\u540d-\u95e8\u724c\u53f7(\u5185\u5b58\u7a7a\u95f40x00001\u7684\u522b\u540d\uff0c\u53ef\u4e0d\u53ef\u4ee5\u6709\u591a\u4e2a\u540d\u5b57\uff1f)\n    int a = 10;\n    \/\/b\u5c31\u8fd9\u4e2a\u5185\u5b58\u7a7a\u95f4\u53e6\u5916\u4e00\u4e2a\u522b\u540d\n    \/\/&amp; C++\u4e2d\u7684\u5f15\u7528\n    int &amp;b = a;    \n    cout &lt;&lt; b &lt;&lt; endl;\n\n    system(&quot;pause&quot;);\n}<\/code><\/pre>\n<pre><code class=\"language-c\">\/\/\u6307\u9488\u503c\u4ea4\u6362\nvoid swap_1(int *a, int *b) {\n    int c = 0;\n    c = *a;\n    *a = *b;\n    *b = c;\n}\n\n\/\/\u5f15\u7528\u503c\u4ea4\u6362\nvoid swap_2(int &amp;a, int &amp;b) {\n    int c = 0;\n    c = a;\n    a = b;\n    b = c;\n}\n\nvoid main() {\n    int x = 10;\n    int y = 20;\n\n    printf(&quot;%d,%d\\n&quot;, x, y);\n    \/\/swap_1(&amp;x, &amp;y);\n    \/\/a\u6210\u4e86x\u7684\u522b\u540d\uff0cb\u6210\u4e86y\u7684\u522b\u540d\n    swap_2(x, y);\n    printf(&quot;%d,%d\\n&quot;, x, y);\n\n    system(&quot;pause&quot;);\n}<\/code><\/pre>\n<h3>\u5f15\u7528\u7684\u4e3b\u8981\u529f\u80fd\uff1a\u4f5c\u4e3a\u51fd\u6570\u7684\u53c2\u6570\u6216\u8fd4\u56de\u503c<\/h3>\n<pre><code class=\"language-c\">\/\/1.\u5355\u7eaf\u7ed9\u53d8\u91cf\u53d6\u522b\u540d\u6ca1\u6709\u4efb\u4f55\u610f\u4e49\uff0c\u4f5c\u4e3a\u51fd\u6570\u53c2\u6570\u4f20\u9012\uff0c\u80fd\u4fdd\u8bc1\u53c2\u6570\u4f20\u9012\u8fc7\u7a0b\u4e2d\u4e0d\u4ea7\u751f\u526f\u672c\n\/\/2.\u5f15\u7528\u53ef\u4ee5\u76f4\u63a5\u64cd\u4f5c\u53d8\u91cf\uff0c\u6307\u9488\u8981\u901a\u8fc7\u53d6\u503c(*p)\uff0c\u95f4\u63a5\u64cd\u4f5c\u53d8\u91cf\uff0c\u6307\u9488\u7684\u53ef\u8bfb\u6027\u5dee\n\nstruct Teacher {\n    char* name;\n    int age;\n};\n\nvoid print1(Teacher &amp;t) {\n    cout &lt;&lt; t.name &lt;&lt; &quot;,&quot; &lt;&lt; t.age &lt;&lt; endl;\n    t.age = 21;\n}\n\nvoid print2(Teacher *t) {\n    cout &lt;&lt; t-&gt;name &lt;&lt; &quot;,&quot; &lt;&lt; t-&gt;age &lt;&lt; endl;\n    \/\/(*t).name \n}\n\n\/\/\u5f15\u7528\u7684\u4e3b\u8981\u529f\u80fd\uff1a\u4f5c\u4e3a\u51fd\u6570\u7684\u53c2\u6570\u6216\u8fd4\u56de\u503c\nvoid main() {\n    Teacher t;\n    t.name = &quot;Joe&quot;;\n    t.age = 20;\n\n    print1(t);\n    print2(&amp;t);\n\n    system(&quot;pause&quot;);\n}<\/code><\/pre>\n<h3>\u6307\u9488\u7684\u5f15\u7528\uff0c\u4ee3\u66ff\u4e8c\u7ea7\u6307\u9488<\/h3>\n<pre><code class=\"language-c\">\/\/\u6307\u9488\u7684\u5f15\u7528\uff0c\u4ee3\u66ff\u4e8c\u7ea7\u6307\u9488\nstruct Teacher {\n    char* name;\n    int age;\n};\n\nvoid getTeacher(Teacher **p) {\n    Teacher *tmp = (Teacher*) malloc(sizeof(Teacher));\n    tmp-&gt;age = 20;\n    *p = tmp;\n}\n\n\/\/Teacher* &amp;p = (Teacher **p)\nvoid getTeacher(Teacher* &amp;p) {\n    p = (Teacher*) malloc(sizeof(Teacher));\n    p-&gt;age = 20;\n}\n\nvoid main() {\n    Teacher *t = NULL;\n    getTeacher(&amp;t);\n\n    system(&quot;pause&quot;);\n}<\/code><\/pre>\n<h3>\u6307\u9488\u5e38\u91cf\u4e0e\u5e38\u91cf\u6307\u9488<\/h3>\n<pre><code class=\"language-c\">void main() {\n    \/\/\u6307\u9488\u5e38\u91cf\uff0c\u6307\u9488\u7684\u5e38\u91cf\uff0c\u4e0d\u6539\u53d8\u5730\u5740\u7684\u6307\u9488\uff0c\u4f46\u662f\u53ef\u4ee5\u4fee\u6539\u5b83\u6307\u5411\u7684\u5185\u5bb9\n    int a = 2, b = 3;\n    int const *p1 = &amp;a;\n    \/\/p1 = &amp;b;  \/\/NO\n    *p1 = 4;\n\n    \/\/\u5e38\u91cf\u6307\u9488\uff0c\u6307\u5411\u5e38\u91cf\u7684\u6307\u9488\uff0c\u5185\u5bb9\u4e0d\u80fd\u4fee\u6539\n    const int *p2 = &amp;a;\n    p2 = &amp;b;\n    \/\/*p2 = 9;  \/\/NO\n}<\/code><\/pre>\n<h3>\u5e38\u5f15\u7528<\/h3>\n<pre><code class=\"language-c\">\/*\n\/\/\u5e38\u5f15\u7528\u7c7b\u4f3c\u4e8ejava\u4e2dfinal\nvoid printf(const int &amp;a) {\n    cout &lt;&lt; a &lt;&lt; endl;    \n}\n\nvoid main() {    \n    \/\/const int a;\n    \/\/\u5f15\u7528\u5fc5\u987b\u8981\u6709\u503c\uff0c\u4e0d\u80fd\u4e3a\u7a7a\n    \/\/int &amp;a = NULL;\n\n    \/\/\u5e38\u5f15\u7528\n    int a = 10, b = 9;\n    const int &amp;c = a;\n\n    \/\/\u5b57\u9762\u91cf\n    const int &amp;d = 70;\n\n    \/\/c = b;\n    printf(c);\n\n    system(&quot;pause&quot;);\n}<\/code><\/pre>\n<pre><code class=\"language-c\">struct Teacher {\n    char name[20];\n    int age;\n};\n\nvoid print1(Teacher *t) {\n    if (t != NULL) {\n        cout &lt;&lt; t-&gt;name &lt;&lt; &quot;,&quot; &lt;&lt; t-&gt;age &lt;&lt; endl;\n    }\n}\n\nvoid print2(Teacher &amp;t) {\n    cout &lt;&lt; t.name &lt;&lt; &quot;,&quot; &lt;&lt; t.age &lt;&lt; endl;\n    t.age = 21;\n}\n\nvoid main() {\n    Teacher t;\n\n    Teacher *p = NULL;\n    \/\/\u62a5\u9519\uff0c\u9632\u6b62\u4e0d\u62a5\u9519\uff0c\u8fdb\u884c\u975e\u7a7a\u5224\u65ad\n    print1(p);\n\n    \/\/\u5f15\u7528\u4e0d\u80fd\u4e3a\u7a7a\uff0c\u6ca1\u6cd5\u4f20\u8fdb\u53bb\n    Teacher &amp;t = NULL;\n    print2(t);\n\n    system(&quot;pause&quot;);\n}<\/code><\/pre>\n<h3>\u5f15\u7528\u7684\u5927\u5c0f<\/h3>\n<pre><code class=\"language-c\">struct Teacher {\n    char name[20];\n    int age;\n};\n\nvoid main() {\n    Teacher t;\n\n    Teacher &amp;t1 = t;\n    Teacher *p = &amp;t;\n\n    cout &lt;&lt; sizeof(t1) &lt;&lt; endl;  \/\/24\n    cout &lt;&lt; sizeof(p) &lt;&lt; endl;  \/\/4\n    system(&quot;pause&quot;);\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>C\u4e0eC++\u7684\u5173\u7cfb 1.C++\u53ef\u4ee5\u4e0eC\u4ee3\u7801\u8fdb\u884c\u6df7\u7f16 2.C++\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u8bed\u8a00\u3001C\u9762\u5411\u8fc7\u7a0b\u7684\u7f16\u7a0b 3.C++\u5bf9C [&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-417","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\/417","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=417"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/417\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=417"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=417"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=417"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}