• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            eryar

            PipeCAD - Plant Piping Design Software.
            RvmTranslator - Translate AVEVA RVM to OBJ, glTF, etc.
            posts - 603, comments - 590, trackbacks - 0, articles - 0

            Hide Implementation Classes

            Posted on 2013-09-28 22:32 eryar 閱讀(3605) 評(píng)論(8)  編輯 收藏 引用

            Hide Implementation Classes

            eryar@163.com

            摘要:很多程序員都用過(guò)private來(lái)隱藏函數(shù)和成員變量,實(shí)際上有些類也是可以被隱藏起來(lái)的。本文是對(duì)《API Design for C++》中2.2.5的翻譯,若不不當(dāng)之處,歡迎指出。

            關(guān)鍵字:API Design for C++, Hide Classes

            除了可以隱藏類的變量與方法之外,還可以隱藏任意單純實(shí)現(xiàn)細(xì)節(jié)的類。很多程序員都用過(guò)隱藏方法和變量,但是好多也好像忘記了并不是所有的類都是公有的。實(shí)際上,有些類只在你的實(shí)現(xiàn)中需要,而不應(yīng)該作為公開(kāi)的接口暴露出來(lái)。

            例如,考慮一個(gè)簡(jiǎn)單的煙花(Fireworks)類:一個(gè)接口可以用來(lái)指定煙花在屏幕上位置,控制煙花的顏色、速度和煙花顆粒(particle)的數(shù)量。明顯地,這個(gè)API就需要對(duì)煙花的每個(gè)顆粒進(jìn)行追蹤,以便于在每幀更新顆粒的位置。這暗示著需要引入一個(gè)用來(lái)保存每個(gè)顆粒狀態(tài)的類FireParticle,但是這個(gè)API的用戶并不需要訪問(wèn)這個(gè)類,它只在實(shí)現(xiàn)API時(shí)才需要。這樣的類就可以設(shè)置為私有(private),即作為類Fireworks私有的部分。代碼如下所示:

             

             1 /// -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: t -*-
             2 ///
             3 /// \file   fireworks.h
             4 /// \author Martin Reddy
             5 /// \brief  An illustration of using private classes.
             6 ///
             7 /// Copyright (c) 2010, Martin Reddy. All rights reserved.
             8 /// Distributed under the X11/MIT License. See LICENSE.txt.
             9 /// See http://APIBook.com/ for the latest version.
            10 ///
            11 
            12 #ifndef FIREWORKS_H
            13 #define FIREWORKS_H
            14 
            15 #include <vector>
            16 
            17 namespace apibook {
            18 
            19 ///
            20 /// A simple fireworks particle system, used to demonstrate
            21 /// the use of private classes to hide implementation state.
            22 ///
            23 class Fireworks
            24 {
            25 public:
            26     Fireworks();
            27 
            28     /// Set the (x, y) origin of the fireworks effect
            29     void SetOrigin(double x, double y);
            30     /// Set the RGB color (0..1) for each particle
            31     void SetColor(float r, float g, float b);
            32     /// Set the gravity acting on each particle (meters/sec)
            33     void SetGravity(float g);
            34     /// Set the speed of the particle simulation
            35     void SetSpeed(float s);
            36     /// Set the number of particles in the simulation
            37     void SetNumberOfParticles(int num);
            38 
            39     /// Start (or continue) the simulation
            40     void Start();
            41     /// Stop the simulation
            42     void Stop();
            43     /// Advance the simulation by dt seconds 
            44     void NextFrame(float dt);
            45 
            46 private:
            47     // FireParticle represents internal hidden state
            48     // (You could also forward declare this class and
            49     // only provide the definition in the .cpp file.)
            50     class FireParticle
            51     {
            52     public:
            53         double mX, mY;
            54         double mVelocityX, mVelocityY;
            55         double mAccelerationX, mAccelerationY;
            56         double mLifeTime;
            57     };
            58 
            59     double mOriginX, mOriginY;
            60     float mRed, mGreen, mBlue;
            61     float mGravity;
            62     float mSpeed;
            63     bool mIsActive;
            64     std::vector<FireParticle *> mParticles;
            65 };
            66 
            67 }
            68 
            69 #endif
            70 

            注意到在類FireParticle中我并沒(méi)有使用getter/setter。只要你愿意,當(dāng)然也可以這樣做。但是也不是非常必要這么做,因?yàn)楣械慕涌谑遣荒茉L問(wèn)這個(gè)類的。有些工程師也比較喜歡使用struct來(lái)代替class,to reflect that the structure is a Plain Old Data(POD) type。

            當(dāng)然,你可能也想過(guò)隱藏類FireParticle,即在頭文件中也不出現(xiàn)。我將在下一節(jié)中講到怎樣來(lái)實(shí)現(xiàn)。

             

            Feedback

            # re: Hide Implementation Classes  回復(fù)  更多評(píng)論   

            2013-09-29 10:58 by 春秋十二月
            這里嵌套類FireParticle的變量并沒(méi)有被隱藏

            # re: Hide Implementation Classes  回復(fù)  更多評(píng)論   

            2013-09-29 17:46 by eryar
            如果給了頭文件,是可以看到源代碼,但是即使看到代碼,在程序的其他地方也不能定義一個(gè)FireParticle類的對(duì)象了。
            我是這么理解的。。。@ 春秋十二月

            # re: Hide Implementation Classes[未登錄](méi)  回復(fù)  更多評(píng)論   

            2013-09-30 02:03 by 春秋十二月
            @eryar
            雖為私有,但暴露了它的定義,比如一個(gè)非正常情況#define private public,那么sizeof(Fireworks::FireParticle)是定義了的。

            # re: Hide Implementation Classes  回復(fù)  更多評(píng)論   

            2013-09-30 17:44 by eryar
            #define private public這招太狠了……
            這樣的話FireParticle是沒(méi)有達(dá)到隱藏的效果。
            那本書里面這里的隱藏是相對(duì)于private而言的,
            @春秋十二月

            # re: Hide Implementation Classes  回復(fù)  更多評(píng)論   

            2013-10-06 20:11 by ccsdu2009
            你這個(gè)不如impl!

            # re: Hide Implementation Classes  回復(fù)  更多評(píng)論   

            2013-10-09 19:15 by eryar
            什么是impl?
            還請(qǐng)指教
            @ccsdu2009

            # re: Hide Implementation Classes  回復(fù)  更多評(píng)論   

            2013-10-29 11:18 by Jayden Shui
            應(yīng)該是pimpl吧。該書3.1.1.

            # re: Hide Implementation Classes  回復(fù)  更多評(píng)論   

            2013-10-29 18:38 by eryar
            哦,謝謝。。。
            :-)
            @Jayden Shui

            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理


            最新久久免费视频| 亚洲一区中文字幕久久| 亚洲国产精品无码久久98| 久久精品国产亚洲77777| 99久久伊人精品综合观看| 欧美一区二区久久精品| 久久国产精品成人片免费| 久久久这里有精品中文字幕| 人妻精品久久无码专区精东影业| 久久精品国产WWW456C0M| 亚洲熟妇无码另类久久久| 青青草国产精品久久| 亚洲欧美成人综合久久久| 国产精品免费久久久久电影网| 香蕉久久夜色精品升级完成| 久久婷婷五月综合成人D啪| 久久99国产精品久久99果冻传媒| 香蕉久久久久久狠狠色| 国产成人精品综合久久久| 久久人人爽人人爽人人片av高请| 日本高清无卡码一区二区久久 | 国产农村妇女毛片精品久久| 亚洲日韩中文无码久久| 欧美久久亚洲精品| 久久久久亚洲精品无码网址| 91精品国产高清久久久久久国产嫩草| 久久综合给合久久狠狠狠97色| 久久天天躁狠狠躁夜夜2020| 国产成人精品久久亚洲高清不卡| 国产精品美女久久久久网| 无码人妻久久久一区二区三区 | 国产成人精品久久一区二区三区av | 99国产欧美久久久精品蜜芽| 麻豆AV一区二区三区久久| 精品一二三区久久aaa片| 7777精品伊人久久久大香线蕉| 午夜精品久久久久久久无码| 久久亚洲国产精品五月天婷| 久久久WWW成人| 狠狠色丁香久久婷婷综合图片| 久久综合久久综合亚洲|