玩转C科技.NET

从学会做人开始认识这个世界!http://tinyurl.com/volnet http://bit.ly/KMzi2

导航

<2009年7月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

统计

公告

Microsoft MVP!
follow me!!!
    follow me on Twitter
    Subscribe to this feed
    Contact volnet online!

    MSN群MyMSDN技术讨论群
    群号:www.msdn@hotmail.com
    Windows Live Alerts
    LiveMessenger:
    <My Library>

    很久没闪了,闪一下睡觉

    与我联系

    搜索

     

    常用链接

    留言簿

    我参与的团队

    我的标签

    随笔分类(145)

    随笔档案(128)

    文章分类(15)

    文章档案(15)

    相册

    家园建设

    最新随笔

    积分与排名

    • 积分 - 210526
    • 排名 - 217

    最新评论

    阅读排行榜

    评论排行榜

    60天内阅读排行

    用预编译指令符避免多文件工程中重复定义的问题

    1、用预编译指令符可以避免在多文件工程中调用文件的时候可能出现的重复定义的现象。

    比如:

    Main.cpp

    #include “Animal.h”

    #include “Fish.h”

    ……

     

    Animal.h

    class Animal()

    {

                      

    }

     

    Fish.h

    #include “Animal.h”

    class Fish():public Animal

    {

            

    }

     

    因此在调用Main.cpp的时候先运行

    1#include “Animal.h”      复制Animal.h过来

    class Animal()

    {

                               

    }

     

    2#include “Fish.h” 复制Fish.h过来

    #include “Animal.h”     复制Animal.h过来

    class Animal()

    {

                                        

    }

     

    class Fish():public Animal

    {

                      

    }

     

    ……

    因此最后的文件应该是形如:

    class Animal()

    {

                               

    }

    class Animal()

    {

                               

    }

    class Fish():public Animal

    {

                      

    }

    因此重复定义了类

    class Animal()

    {

                               

    }

    是显而易见的。

    这时候引入预编译指令符的方法来避免这样的现象来发生。

    假设在最后的文件中我们来补充预编译指令符的方法:

     

                       #ifndef      ABCD       //如果没有定义ABCD,否则转向endif

                       #define      ABCD       //那么就定义ABCD

    class Animal()

    {

                               

    }

    #endif

     

    #ifndef      ABCD       //如果没有定义ABCD,否则转向endif

                       #define      ABCD       //那么就定义ABCD

    class Animal()

    {

                               

    }

    #endif

     

    class Fish():public Animal

    {

                      

    }

    添加蓝色部分就可以避免重复定义了。

    因此可以在多文本文件中做如下修改:

    Main.cpp

    #include “Animal.h”

    #include “Fish.h”

    ……

     

    Animal.h

    #ifndef      ABCD       //如果没有定义ABCD,否则转向endif

    #define      ABCD       //那么就定义ABCD

    class Animal()

    {

                      

    }

    #endif

     

    Fish.h

    #include “Animal.h”

    #ifndef      ABCD       //如果没有定义ABCD,否则转向endif

    #define      ABCD       //那么就定义ABCD

    class Fish():public Animal

    {

            

    }

    #endif

    这样当Main.cpp在编译的时候就会忽略重复的定义而避免错误的产生了。

    posted on 2008-07-18 20:31 volnet(可以叫我大V) 阅读(472) 评论(0)  编辑 收藏 所属分类: VC++/C++

    发表评论

    昵称: [登录] [注册]

    主页:

    邮箱:(仅博主可见)

    评论内容:

      登录  注册

    [使用Ctrl+Enter键快速提交评论]

    0 472791




    相关文章:

    相关链接:
    使用Live Messenger联系我
    关闭