代码之家  ›  专栏  ›  技术社区  ›  Michael F

重新学习C++和TyEuFF问题

  •  0
  • Michael F  · 技术社区  · 14 年前

    所以,自从我用C++完成一个大型项目以来,已经有5、6年了,大约半年以来,我最后一次不得不处理一般的C++程序和C++运行时。我决定要重新学习这门语言,主要是因为它可能与即将到来的工作项目非常相关。

    我一直主要使用C和Python,现在我甚至对下面的for循环语法都不放心:

    for( int i(0); i != n; ++i) {}

    尽管我承认这并不难理解。

    由于我了解到该语言和库中有许多附加内容,当然还有习语、模式和样式,我想征求您对最新的优秀资源的意见。我想避免使用那些“这是一个有很多额外功能的C”的教程。我仍然有我正在学习的“C++编程语言”的强制性拷贝,但我真的不知道下一步该集中在哪里。设计模式?模板和STL/Boost?还有别的吗?

    还有一个更具体的问题 typedef s。具体如下:

    typedef Type& TypeRef;

    作为API的一部分提供不透明类型时通常被认为是良好的实践?有一个有意义的名字,就是。这和图书馆采用的方法有什么相似之处吗 数据包捕获函数库

    提前谢谢。

    3 回复  |  直到 14 年前
        1
  •  4
  •   Community CDub    7 年前

    为了回答您的具体问题,typedef隐藏了一个事实,即某个东西是指针或引用,这总是一个坏主意-在C中也是如此。例如,考虑不透明类型的文件-仍然必须显式地创建文件指针才能使用它。

    至于书籍,请参阅 The Definitive C++ Book Guide and List

        2
  •  3
  •   bshields    14 年前

    我从不喜欢用引用或指针类型来定义类型。要有效地隐藏类型是指针还是引用这一事实是不可能的,因为它们对客户机具有不同的调用语义,所以这只会在我的经验中导致混淆。

        3
  •  0
  •   Noah Watkins    14 年前

    Linux内核对C采用的typedef策略是最好的。基本上策略是不使用typedef,除非您正在创建一个新的类型抽象。这在底层类型在体系结构中不同,但内核用户需要一个公共类型的情况下非常有用。例如 u64型

    typedef u64 unsigned long long;
    

    也许,用C++重新定位的最好方法是编写一个一次性的程序来执行特定的功能。例如,编写一个模板,在Boost中使用一个库,创建一些线程,分配一些内存。关键不是 学习 所有这些东西,重点是看和它,这样你就不会喘息的空气以后,当它的表演时间。

    以下是Linux内核typedef标准,摘自 http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.33.y.git;a=blob_plain;f=Documentation/CodingStyle;hb=HEAD

      Chapter 5: Typedefs
    

    请不要使用“vps\t”之类的东西。

    使用typedef 一

    副总裁;

    在源头上,它意味着什么?

    结构虚拟容器*a;

    你真的可以分辨出“a”是什么。

    很多人认为typedef 仅适用于:

    typedef积极用于 隐藏

     Example: "pte_t" etc. opaque objects that you can only access using
     the proper accessor functions.
    
     NOTE! Opaqueness and "accessor functions" are not good in themselves.
     The reason we have them for things like pte_t etc. is that there
     really is absolutely _zero_ portably accessible information there.
    

    抽象 帮助 避免混淆 无论是“int”还是“long”。

     u8/u16/u32 are perfectly fine typedefs, although they fit into
     category (d) better than here.
    
     NOTE! Again - there needs to be a _reason_ for this. If something is
     "unsigned long", then there's no reason to do
    

     but if there is a clear reason for why it under certain circumstances
     might be an "unsigned int" and under other configurations might be
     "unsigned long", then by all means go ahead and use a typedef.
    

    (c) 当你用稀疏来表示 创建 新的 类型

    (d) 与相同的新类型 标准C99类型,在某些情况下

     Although it would only take a short amount of time for the eyes and
     brain to become accustomed to the standard types like 'uint32_t',
     some people object to their use anyway.
    
     Therefore, the Linux-specific 'u8/u16/u32/u64' types and their
     signed equivalents which are identical to standard types are
     permitted -- although they are not mandatory in new code of your
     own.
    
     When editing existing code which already uses one or the other set
     of types, you should conform to the existing choices in that code.
    

    (e) 在用户空间中使用的安全类型。

     In certain structures which are visible to userspace, we cannot
     require C99 types and cannot use the 'u32' form above. Thus, we
     use __u32 and similar types in all structures which are shared
     with userspace.
    

    也许还有其他情况,但是 基本上规则应该是永远不要 你用过typedef吗

    通常,指针或结构 这些元素可以合理地 从未 是 类型定义。