Coder Social home page Coder Social logo

facepp_cpp_sdk's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

facepp_cpp_sdk's Issues

c++规范

示例1

// FaceppApi.hpp

#include <stdio.h>  //  [1]

class FaceppApi 
{
protected:
    const char * key; // Api_key         [2]
    const char * secret; // Api_secret
};

[1] 一般使用 #include <cstdio> 代替 #include <stdio.h>

The ".h" headers dump all their names into the global namespace, whereas the newer forms keep their names in namespace std. Therefore, the newer forms are the preferred forms for all uses except for C++ programs which are intended to be strictly compatible with C.

@see Standard C++ C-Library Subset Header Strategy

[2] 在类中使用指针的危险性

FaceppApi 中使用指针为浅复制,当使用常量指针转递参数给构造函数没有问题,作为 C++ 的开发者会选择 std::string ,为了适配库,可能以 std::string.c_str() 形式传递参数给构造函数,往往出现参数已经失效,引用了一个无效的指针。

示例2

// Utils/CurlPost.hpp

#ifndef CurlPost_hpp
#define CurlPost_hpp

#include <iostream>
#include <map>

using namespace std;  // [3]

class CurlPost 
{
    public :
    void doPost(const char * URL, map<const char* , const char*> params);  // [4]
};

#endif /* CurlPost_hpp */

[3] 在头文件中引入命名空间 std 容易引发名字冲突

难以保证我们在开发过程中不效仿标准库的命名规则,在头文件中引入命名空间 std,只要包含了它,后续所有代码都表明使用命名空间内的代码,万一自己命名了相同的标识符会产生冲突。

[4] 与类成员无关的函数可以独立

在类 CurlPost 中的 doPost 并没有使用类的任何数据,这个方法导致在调用处创建一个临时对象,将它声明为静态函数(CurlPost::doPost(...))或命名空间(curlpost::doPost(...))内的自由函数,免于创建临时对象。

[5] 建议: 库加上自己的命名空间,免于与用户代码冲突。

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.