基类函数的this指针和派生类函数的this指针
来源:互联网 发布:淘宝客怎样推广产品 编辑:程序博客网 时间:2024/05/23 15:03
在研究live555流程代码时,因为遇到对于基类RTSPServer在函数createNewClientConnection中,创建RTSPClientConnection时,采用的是new RTSPClientConnection(*this, clientSocket, clientAddr)方法创建的RTSPClientConnection,而在live media server中DynamicRTSPServer继承自RTSPServer,而主程序中只创建了DynamicRTSPServer对象;百思不得解,在此处创建RTSPClientConnection时,给其传递的是this指针的内容,而在其构造函数中,代码如下:
RTSPServer::RTSPClientConnection::RTSPClientConnection(RTSPServer& ourServer, int clientSocket, struct sockaddr_in clientAddr) : fOurServer(ourServer), fIsActive(True), fClientInputSocket(clientSocket), fClientOutputSocket(clientSocket), fClientAddr(clientAddr), fRecursionCount(0), fOurSessionCookie(NULL) { // Add ourself to our 'client connections' table: fOurServer.fClientConnections->Add((char const*)this, this); // Arrange to handle incoming requests: resetRequestBuffer(); envir().taskScheduler().setBackgroundHandling(fClientInputSocket, SOCKET_READABLE|SOCKET_EXCEPTION,(TaskScheduler::BackgroundHandlerProc*)&incomingRequestHandler, this);}
this指针的传递和构造函数的初始化列表都显示fOurServer赋值为RTSPServer的this指针,而在handleCmd_DESCRIBE函数中
ServerMediaSession* session = fOurServer.lookupServerMediaSession(urlTotalSuffix);
的调用为什么能够调到DynamicRTSPServer::lookupServerMediaSession(char const* streamName)函数呢,在此不得以对this指针在基类和派生类的赋值进行了研究,结论表明:在派生类继承基类的时候,派生类初始化的时候调用基类的构造函数,随后调用派生类的构造函数,基类构造函数的this指针指向的是派生类对象,因为其this指针的值和派生类this指针的值相同,一下为我自己写的测试代码:
//MyTest.h文件#ifndef _MY_TEST_H#define _MY_TEST_Hclass BaseTest{public:BaseTest(int fun);virtual ~BaseTest();static BaseTest* CreateNew(int fun);void show();virtual void getInfo();private:int num;};class DeriveTest: public BaseTest{public:DeriveTest(int num, int func);virtual ~DeriveTest();static DeriveTest* CreateNew(int num,int func);void show();virtual void getInfo();private:int fun;};#endif
//MyTest.cpp文件#include <iostream>#include "MyTest.h"using namespace std;BaseTest::BaseTest(int num):num(num){cout<<"the point base is:"<<this<<endl;}BaseTest::~BaseTest(){}BaseTest* BaseTest::CreateNew(int num){return new BaseTest(num);}void BaseTest::show(){cout<<"this is BaseTest!"<<endl;getInfo();}void BaseTest::getInfo(){cout<<"the point is:"<<this<<endl;}DeriveTest::DeriveTest(int num, int func):BaseTest(num),fun(func){cout<<"the point derive is:"<<this<<endl;}DeriveTest::~DeriveTest(){}DeriveTest* DeriveTest::CreateNew(int num, int func){return new DeriveTest(num, func);}void DeriveTest::show(){cout<<"this is DeriveTest!"<<endl;}void DeriveTest::getInfo(){cout<<"the point1 is:"<<this<<endl;}
//测试main函数文件#include "MyTest.h"int main(){int num = 5;int fun = 10;BaseTest *myTest = DeriveTest::CreateNew(5,10);return 0;}
测试结果显示为:
the point base is:00032BC8
the point derive is:00032BC8
通过剪短的测试,明白了在初始化时赋值的this指针的作用,也明白了live555在内部调用和设计的复杂性,加油,得进一步努力分析代码了。
- 基类函数的this指针和派生类函数的this指针
- 派生类中基类成员函数的this指针
- 类/指针/函数/this
- 一般函数指针和类的成员函数指针 (this指针)
- 类的继承与派生、多态、this指针、函数模板与类模板
- C++中通过派生类调用第二基类的普通成员函数时this指针的调整
- 当基类被继承后this指针将代表派生类的this指针
- 类的this指针
- this指针在类的成员函数中的传递
- 类中函数的一个隐含参数--this指针
- this指针指向当前调用对象,只是用于类的非静态成员函数。静态函数和全局函数 无当前对象,无this指针。
- this指针和函数调用
- 成员函数和this指针
- 构造函数和this指针
- this指针和构造函数
- this指针和常函数
- 关于基类的this和派生类的this
- 成员函数隐含的this指针
- How to debug mobile safari in iOS
- kd树的java实现
- Linux下网络流量实时监控工具大全
- Qt5类之QLine and QLineF
- 分享一套 python 试题
- 基类函数的this指针和派生类函数的this指针
- linux svn安装和配置
- Oracle RAC学习笔记:基本概念及入门 01
- 解决“Connection to https://dl-ssl.google.com refused”问题
- mysql表分区
- Oracle RAC学习笔记:基本概念及入门 02
- Guava源码分析——ServiceManager
- 32位和64位数据类型
- listView隔行设置颜色重写SimpleAdapter