GithubHelp home page GithubHelp logo

sya's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

sya's Issues

定义函数时, 参数顺序依次为: 输入参数, 然后是输出参数

来自Google C++ Style的建议:C/C++ 函数参数分为输入参数, 输出参数, 和输入/输出参数三种. 输入参数一般传值或传 const 引用, 输出参数或输入/输出参数则是非-const 指针. 对参数排序时, 将只输入的参数放在所有输出参数之前. 尤其是不要仅仅因为是新加的参数, 就把它放在最后; 即使是新加的只输入参数也要放在输出参数之前.

能用前置声明的地方尽量不使用 #include

Google C++ Style的建议:
当一个头文件被包含的同时也引入了新的依赖, 一旦该头文件被修改, 代码就会被重新编译. 如果这个头文件又包含了其他头文件, 这些头文件的任何改变都将导致所有包含了该头文件的代码被重新编译. 因此, 我们倾向于减少包含头文件, 尤其是在头文件中包含头文件.

使用前置声明可以显著减少需要包含的头文件数量. 举例说明: 如果头文件中用到类 File, 但不需要访问 File 类的声明, 头文件中只需前置声明 class File; 而无须 #include "file/base/file.h".
不允许访问类的定义的前提下, 我们在一个头文件中能对类 Foo 做哪些操作?

  • 我们可以将数据成员类型声明为 Foo * 或 Foo &.
  • 我们可以将函数参数 / 返回值的类型声明为 Foo (但不能定义实现).
  • 我们可以将静态数据成员的类型声明为 Foo, 因为静态数据成员的定义在类定义之外.

另外,这篇文章对上述几种情况也做了阐述:如何使用前置声明取代包括头文件

能不能得到每个函数的行号呢

我试了你的方法,可以获得函数名称什么的,但是却无法得到行号,请问有什么办法得到每个函数的起始位置呢?
谢谢。

只有当函数只有 10 行甚至更少时才将其定义为内联函数

Google C++ Style的建议:
当函数体比较小的时候, 内联该函数可以令目标代码更加高效. 对于存取函数以及其它函数体比较短, 性能关键的函数, 鼓励使用内联.
但有些例外情况:

  • 内联一个相当大的函数将戏剧性的增加代码大小
  • 谨慎对待析构函数, 析构函数往往比其表面看起来要更长, 因为有隐含的成员和基类析构函数被调用
  • 内联那些包含循环或 switch 语句的函数常常是得不偿失 (除非在大多数情况下, 这些循环或 switch 语句从不被执行)
  • 虚函数和递归函数就不会被正常内联

比较合理的#include顺序

一个比较合理的顺序:使用标准的头文件包含顺序可增强可读性, 避免隐藏依赖: C 库, C++ 库, 其他库的 .h, 本项目内的 .h

谨慎使用智能指针

根据Google C++ Style Guide中对智能指针的建议,尽量不要使用std::auto_ptr scoped_ptr * linked_ptr* 。即使真的要使用,也尽量使用 std::unique_ptr 或者 std::tr1::shared_ptr


因此,工具会检查代码中使用std::auto_ptr的情况,如果发现了使用,则给出警告信息。

示例

Bad Code

#include <iostream>
#include <memory>
using namespace std;

int main () {
  auto_ptr<int> p1 (new int);
  *p1.get()=10;

  auto_ptr<int> p2 (p1);  // auto_ptr in here

  cout << "p2 points to " << *p2 << "\n";
  // (p1 is now null-pointer auto_ptr)

  return 0;
}

Good Code

#include <iostream>
#include "boost/shared_ptr.hpp" // include "boost/shared_ptr.hpp"
using namespace std;

int main () {
  auto_ptr<int> p1 (new int);
  *p1.get()=10;

  boost::shared_ptr<int> p2 (p1);  // boost::shared_ptr is OK

  cout << "p2 points to " << *p2 << "\n";

  return 0;
}

会造成缓冲区溢出的函数

以下这些函数会造成缓冲区溢出(buffer overflow):

以下这些是Linux下的函数

  • strcpy
  • strcat
  • sprintf
  • vsprintf
  • gets
  • realpath
  • getopt
  • getpass
  • streadd
  • strecpy
  • strtrns
  • wcscat

以下这些是Windows下的函数

  • lstrcat
  • StrCatBuff
  • _tcscat
  • _ftcscat
  • strncat
  • StrNCat
  • strcpy
  • wcscpy
  • lstrcpy
  • _tcscpy
  • _ftcscpy
  • Strncpy
  • gets
  • _getws
  • _getts
  • sprintf
  • swprintf
  • wsprintf
  • wnsprintf
  • _stprintf
  • _snprintf
  • _snwprintf
  • _sntprintf
  • vsprintf
  • vswprintf
  • wvsprintf
  • wvnsprintf
  • _vstprintf
  • _vsnprintf
  • _vsnwprintf
  • _vsntprintf
  • Strlen

如何获取一个完成C/C++工程对象 ICProject

一直好奇基于CDT部分的API的样例代码是基础一个单独cpp file进行解析AST获取一个TranslationUnit,但是如果说除了头文件和cpp源码,其他源码之间也互相调用。在Eclipse CDT这个IDE里面肯定能通过找定义点 调用图这些功能。我感觉必然dom模型里面肯定也能存在这种关系,这个用分别创建TranslationUnit应该做不到吧。。怎么能获取这样一个完整工程结构呢?

头文件包含头文件该如何解析?

请教一个问题:

头文件a.h内容如下:
#include "b.h"
#pragma once 
#define DLL_EXPORT __declspec(dllexport) 
extern "C" DLL_EXPORT int One(int a, int b);
头文件b.h内容如下:
#pragma once 
#define DLL_EXPORT __declspec(dllexport) 
extern "C" DLL_EXPORT int Two(int c, int d);

此时,解析头文件a.h,如何能获得One(int a, int b)和Two(int c, int d)两个函数申明?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.