android实体类怎么创建构造函数,Android Binder 之不懂:C++ 构造函数后加冒号

今天分析android底层源码,大部分用C/C++写的,感觉好久没看C/C++了,很多都生疏了,看到好多不懂的还得重新百度。

在分析Android Binder时遇到一个类ProcessState.cpp,

**1、**其头文件为:/framework/base/include/binder/

#ifndef ANDROID_PROCESS_STATE_H

#define ANDROID_PROCESS_STATE_H

#include

#include

#include

#include

#include

// ---------------------------------------------------------------------------

namespace android {

// Global variables

extern int mArgC;

extern const char* const* mArgV;

extern int mArgLen;

class IPCThreadState;

class ProcessState : public virtual RefBase

{

public:

static sp self();

void setContextObject(const sp& object);

sp getContextObject(const sp& caller);

void setContextObject(const sp& object,

const String16& name);

sp getContextObject(const String16& name,

const sp& caller);

void startThreadPool();

typedef bool (*context_check_func)(const String16& name,

const sp& caller,

void* userData);

bool isContextManager(void) const;

bool becomeContextManager(

context_check_func checkFunc,

void* userData);

sp getStrongProxyForHandle(int32_t handle);

wp getWeakProxyForHandle(int32_t handle);

void expungeHandle(int32_t handle, IBinder* binder);

void setArgs(int argc, const char* const argv[]);

int getArgC() const;

const char* const* getArgV() const;

void setArgV0(const char* txt);

void spawnPooledThread(bool isMain);

private:

friend class IPCThreadState;

ProcessState();

~ProcessState();

ProcessState(const ProcessState& o);

ProcessState& operator=(const ProcessState& o);

struct handle_entry {

IBinder* binder;

RefBase::weakref_type* refs;

};

handle_entry* lookupHandleLocked(int32_t handle);

int mDriverFD;

void* mVMStart;

mutable Mutex mLock; // protects everything below.

VectormHandleToObject;

bool mManagesContexts;

context_check_func mBinderContextCheckFunc;

void* mBinderContextUserData;

KeyedVector >

mContexts;

String8 mRootDir;

bool mThreadPoolStarted;

volatile int32_t mThreadPoolSeq;

};

}; // namespace android

// ---------------------------------------------------------------------------

#endif // ANDROID_PROCESS_STATE_H

**2、**ProcessState.cpp:/framework/base/libs/binder/ 其构造函数为

ProcessState::ProcessState()

: mDriverFD(open_driver())

, mVMStart(MAP_FAILED)

, mManagesContexts(false)

, mBinderContextCheckFunc(NULL)

, mBinderContextUserData(NULL)

, mThreadPoolStarted(false)

, mThreadPoolSeq(1)

{

if (mDriverFD >= 0) {

// XXX Ideally, there should be a specific define for whether we

// have mmap (or whether we could possibly have the kernel module

// availabla).

#if !defined(HAVE_WIN32_IPC)

// mmap the binder, providing a chunk of virtual address space to receive transactions.

mVMStart = mmap(0, BINDER_VM_SIZE, PROT_READ, MAP_PRIVATE | MAP_NORESERVE, mDriverFD, 0);

if (mVMStart == MAP_FAILED) {

// *sigh*

LOGE("Using /dev/binder failed: unable to mmap transaction memory.\n");

close(mDriverFD);

mDriverFD = -1;

}

#else

mDriverFD = -1;

#endif

}

LOG_ALWAYS_FATAL_IF(mDriverFD < 0, "Binder driver could not be opened. Terminating.");

}

其实冒号后的内容是初始化成员列表,一般有三种情况:

1、对含有对象成员的对象进行初始化,例如,类line有两个私有对象成员startpoint、endpoint,line的构造函数写成:

line(int sx,int sy,int ex,int ey):startpoint(sx,sy),endpoint(ex,ey){……}

初始化时按照类定义中对象成员的顺序分别调用各自对象的构造函数,再执行自己的构造函数

2、对于不含对象成员的对象,初始化时也可以套用上面的格式,例如,类rectangle有两个数据成员length、width,其构造函数写成:

rectangle():length(1),width(2){}

rectangle(int x,int y):length(x),width(y){}

3、对父类进行初始化,例如, CDlgCalcDlg的父类是MFC类CDialog,其构造函数写为:

CDlgCalcDlg(CWnd* pParent ): CDialog(CDlgCalcDlg::IDD, pParent)

其中IDD是一个枚举元素,标志对话框模板的ID,使用初始化成员列表对对象进行初始化,有时是必须的,有时是出于提高效率的考虑

所以上面那个构造函数的意思就是对其数据成员或者对象成员进行初始化!要理解初始化的顺序哦!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值