Cpp Styles 规范

Centents

#File name:

filename.cpp file
filename.h file

#Include:

#include "file.h"
#include <systemPath/file.h>

#Class header
Class header files should have

#ifndef CLASSNAME_H
#define CLASSNAME_H

...
#endif // CLASSNAME_H

#Class:

class DActionServer
{


public:
    DActionServer();
    int run(int port, char* argv[]);
    int run(int port);
    int signalServer();
    void test();
    void initDatabase();
    void selectDatabase(QString className, QString functionName);
    QByteArray getLowlevelSymbol(QString className, QString functionName);

     //Class Member
    int m_epollListeningFd;
    int m_epollConnectionsFd;
    int m_listeningFd;
    int m_numberOfRequests;

};

#class single inheritance:

class DAbsTime : public DValue
{
public:
    DAbsTime();
    void now();
    DAbsTime(int y, int m, int d, int h, int min, int s);
    void setDate(int y, int m, int d);
    void setTime(int h, int m, int s);
    QString encodeToString();
    void encodeFromString(QString paramString);
    QString getClassName();

    int m_year;
    int m_month;
    int m_day;
    int m_hour;
    int m_minute;
    int m_second;
};

#class multiple inheritance:

class DColor : public DValue, public QColor
{
      ......

};

#Struct:

struct DThreadData{
    long m_threadId;
    DActionServer * m_ptr;

};

#For:

for(int i = 0; i < size; i++)
{
    doSomeThing();
}

#Value

int m_size;
int m_userID;
int m_inputType

#Lambda

std::cout << [](int i) -> int { return std::abs(i); } (-2016);

#Function

/**
 * @brief read callback function for libcurl
 *
 * @param pointer of max size (size*nmemb) to write data to
 * @param size size parameter
 * @param nmemb memblock parameter
 * @param userdata pointer to user data to read data from
 *
 * @return (size * nmemb)
 */
size_t RestClient::readCallback(void *data, size_t size, size_t nmemb,
                            void *userdata)
{
  /** get upload struct */
  RestClient::upload_object* u;
  u = reinterpret_cast<RestClient::upload_object*>(userdata);
  /** set correct sizes */
  size_t curl_size = size * nmemb;
  size_t copy_size = (u->length < curl_size) ? u->length : curl_size;
  /** copy data to buffer */
  memcpy(data, u->data, copy_size);
  /** decrement length and increment data pointer */
  u->length -= copy_size;
  u->data += copy_size;
  /** return copied size */
  return copy_size;
}

#Typedef

typedef void (*DActionPoint)(DAction * action, DValue * value);

#Name Space

namespace YourNameSpace {
inline namespace Action {
  void DoSomething();
}
}
using namespace YourNameSpace;

发表评论

电子邮件地址不会被公开。 必填项已用*标注