class A
{
public:
static void func() {
m_a = 1;
}
int m_a;
};
10 个解决方案
#1
static成员函数只能访问static成员变量
--------------
而且这个规定已经足够任何在这一方面的应用,楼主是不是有些设计思想没有想通?
--------------
而且这个规定已经足够任何在这一方面的应用,楼主是不是有些设计思想没有想通?
#2
没碰到过,也不会。
#3
你为什么人那么做?
#4
应该是找不到办法的
#5
因为static函数里编译器不会自动隐式添加参数this指针,所以只有显示指定一个。不过这么做真的没什么意思。
#include<iostream>
using namespace std;
class A{
public:
A( int val = 0 ): pa( this ), _val( val ){}
A *pa;
int val() const { return _val; }
void val( int v ) { _val = v; }
static void static_val( A*, int );
private:
int _val;
};
void A::static_val( A *pa, int m ){ //pa是this指针的替代
pa->_val = m;
}
int main()
{
A a( 5 );
cout << a.val() << endl;
A::static_val( &a, 10 );
cout << a.val() << endl;
return 1;
}
#include<iostream>
using namespace std;
class A{
public:
A( int val = 0 ): pa( this ), _val( val ){}
A *pa;
int val() const { return _val; }
void val( int v ) { _val = v; }
static void static_val( A*, int );
private:
int _val;
};
void A::static_val( A *pa, int m ){ //pa是this指针的替代
pa->_val = m;
}
int main()
{
A a( 5 );
cout << a.val() << endl;
A::static_val( &a, 10 );
cout << a.val() << endl;
return 1;
}
#6
不好意思,上面写错了点
void A::static_val( A *p, int m ){
p->pa->_val = m;
}
void A::static_val( A *p, int m ){
p->pa->_val = m;
}
#7
静态成员函数调用非静态数据成员意义不大
不过使静态成员函数实现多态调用有时候是会用到的
具体就是比如说创建一个自己的线程类
不过这样也可以访问非静态数据成员滴......
不过使静态成员函数实现多态调用有时候是会用到的
具体就是比如说创建一个自己的线程类
不过这样也可以访问非静态数据成员滴......
#8
只能传一个实例进去,
class A
{
public:
static void func(A self) {
self.m_a = 1;
}
int m_a;
};
class A
{
public:
static void func(A self) {
self.m_a = 1;
}
int m_a;
};
#9
#pragma once
typedef unsigned(__stdcall *pThreadFunc)(void*);
#define smBeginThread(psa,cbStack,pfnStartAddr,pvParam, \
dwCreate,pdwThreadId) \
(HANDLE)_beginthreadex(psa,(unsigned int)cbStack, \
(pThreadFunc)pfnStartAddr,(void*)pvParam,dwCreate,(unsigned*)pdwThreadId)
class CMyThread
{
private:
//禁止拷贝和将对象赋值给其他对象
CMyThread(CMyThread&);
CMyThread & operator =(CMyThread&);
private:
static unsigned int __stdcall ThreadProxy( void *pvParam );
protected:
virtual int Run(void *pvParam)=0;
~CMyThread(void); //使对象只能从堆中创建
protected:
HANDLE _hThread;
public:
enum _DESTROY_TYPE{_dtWAIT=0, //阻塞等待线程直到线程退出
_dtTRY, //如果线程已经退出,则删除,否则立刻返回
_dtTERMINATE}; //强制关闭线程
public:
CMyThread(void);
int Create(LPVOID pvParam,BOOL autoDelete=TRUE
,DWORD dwCreate=CREATE_SUSPENDED
,LPSECURITY_ATTRIBUTES psa=NULL,SIZE_T cbStack=0
,LPDWORD pdwThreadId=NULL);
/*******************************省略***********************/
};
#include <Windows.h>
#include <process.h>
#include ".\mythread.h"
#ifndef ASSERT
#include <cassert>
#define ASSERT(s) assert(s)
#endif
namespace
{
class _ThreadProxy
{
friend CMyThread;
_ThreadProxy(_ThreadProxy&);
_ThreadProxy& operator =(_ThreadProxy);
private:
CMyThread* _pThis;
void *_pvParam;
BOOL _AutoDelete;
_ThreadProxy(CMyThread*thread,void *pvParam,BOOL autoDelete)
:_pThis(thread)
,_pvParam(pvParam)
,_AutoDelete(autoDelete)
{
}
};
}
CMyThread::CMyThread(void)
:_hThread(0)
{
}
CMyThread::~CMyThread(void)
{
if (_hThread)
{
CloseHandle(_hThread);
}
_hThread=NULL;
}
unsigned int CMyThread::ThreadProxy( void *pvParam )
{
_ThreadProxy *proxy=(_ThreadProxy*)pvParam;
ASSERT(proxy);
CMyThread*pThis=proxy->_pThis;
void *param=proxy->_pvParam;
int bAutoDelete=proxy->_AutoDelete;
delete proxy;
int iRet=pThis->Run(param);
if (bAutoDelete)
{
delete pThis;
}
return iRet;
}
int CMyThread::Create(LPVOID pvParam,BOOL autoDelete/* =TRUE */
,DWORD dwCreate/* =CREATE_SUSPENDED */,LPSECURITY_ATTRIBUTES
psa/* =NULL */,SIZE_T cbStack/* =0 */,LPDWORD pdwThreadId/* =NULL */)
{
_hThread=smBeginThread(psa,cbStack,ThreadProxy,
(new _ThreadProxy(this,pvParam,autoDelete)) ,dwCreate,pdwThreadId);
ASSERT(_hThread);
return _hThread?0:-1;
}
/********************省略*******************************************/
typedef unsigned(__stdcall *pThreadFunc)(void*);
#define smBeginThread(psa,cbStack,pfnStartAddr,pvParam, \
dwCreate,pdwThreadId) \
(HANDLE)_beginthreadex(psa,(unsigned int)cbStack, \
(pThreadFunc)pfnStartAddr,(void*)pvParam,dwCreate,(unsigned*)pdwThreadId)
class CMyThread
{
private:
//禁止拷贝和将对象赋值给其他对象
CMyThread(CMyThread&);
CMyThread & operator =(CMyThread&);
private:
static unsigned int __stdcall ThreadProxy( void *pvParam );
protected:
virtual int Run(void *pvParam)=0;
~CMyThread(void); //使对象只能从堆中创建
protected:
HANDLE _hThread;
public:
enum _DESTROY_TYPE{_dtWAIT=0, //阻塞等待线程直到线程退出
_dtTRY, //如果线程已经退出,则删除,否则立刻返回
_dtTERMINATE}; //强制关闭线程
public:
CMyThread(void);
int Create(LPVOID pvParam,BOOL autoDelete=TRUE
,DWORD dwCreate=CREATE_SUSPENDED
,LPSECURITY_ATTRIBUTES psa=NULL,SIZE_T cbStack=0
,LPDWORD pdwThreadId=NULL);
/*******************************省略***********************/
};
#include <Windows.h>
#include <process.h>
#include ".\mythread.h"
#ifndef ASSERT
#include <cassert>
#define ASSERT(s) assert(s)
#endif
namespace
{
class _ThreadProxy
{
friend CMyThread;
_ThreadProxy(_ThreadProxy&);
_ThreadProxy& operator =(_ThreadProxy);
private:
CMyThread* _pThis;
void *_pvParam;
BOOL _AutoDelete;
_ThreadProxy(CMyThread*thread,void *pvParam,BOOL autoDelete)
:_pThis(thread)
,_pvParam(pvParam)
,_AutoDelete(autoDelete)
{
}
};
}
CMyThread::CMyThread(void)
:_hThread(0)
{
}
CMyThread::~CMyThread(void)
{
if (_hThread)
{
CloseHandle(_hThread);
}
_hThread=NULL;
}
unsigned int CMyThread::ThreadProxy( void *pvParam )
{
_ThreadProxy *proxy=(_ThreadProxy*)pvParam;
ASSERT(proxy);
CMyThread*pThis=proxy->_pThis;
void *param=proxy->_pvParam;
int bAutoDelete=proxy->_AutoDelete;
delete proxy;
int iRet=pThis->Run(param);
if (bAutoDelete)
{
delete pThis;
}
return iRet;
}
int CMyThread::Create(LPVOID pvParam,BOOL autoDelete/* =TRUE */
,DWORD dwCreate/* =CREATE_SUSPENDED */,LPSECURITY_ATTRIBUTES
psa/* =NULL */,SIZE_T cbStack/* =0 */,LPDWORD pdwThreadId/* =NULL */)
{
_hThread=smBeginThread(psa,cbStack,ThreadProxy,
(new _ThreadProxy(this,pvParam,autoDelete)) ,dwCreate,pdwThreadId);
ASSERT(_hThread);
return _hThread?0:-1;
}
/********************省略*******************************************/
#10
class A
{
public:
static void func(A& a) {
a.m_a = 1;
}
int m_a;
};
{
public:
static void func(A& a) {
a.m_a = 1;
}
int m_a;
};
#1
static成员函数只能访问static成员变量
--------------
而且这个规定已经足够任何在这一方面的应用,楼主是不是有些设计思想没有想通?
--------------
而且这个规定已经足够任何在这一方面的应用,楼主是不是有些设计思想没有想通?
#2
没碰到过,也不会。
#3
你为什么人那么做?
#4
应该是找不到办法的
#5
因为static函数里编译器不会自动隐式添加参数this指针,所以只有显示指定一个。不过这么做真的没什么意思。
#include<iostream>
using namespace std;
class A{
public:
A( int val = 0 ): pa( this ), _val( val ){}
A *pa;
int val() const { return _val; }
void val( int v ) { _val = v; }
static void static_val( A*, int );
private:
int _val;
};
void A::static_val( A *pa, int m ){ //pa是this指针的替代
pa->_val = m;
}
int main()
{
A a( 5 );
cout << a.val() << endl;
A::static_val( &a, 10 );
cout << a.val() << endl;
return 1;
}
#include<iostream>
using namespace std;
class A{
public:
A( int val = 0 ): pa( this ), _val( val ){}
A *pa;
int val() const { return _val; }
void val( int v ) { _val = v; }
static void static_val( A*, int );
private:
int _val;
};
void A::static_val( A *pa, int m ){ //pa是this指针的替代
pa->_val = m;
}
int main()
{
A a( 5 );
cout << a.val() << endl;
A::static_val( &a, 10 );
cout << a.val() << endl;
return 1;
}
#6
不好意思,上面写错了点
void A::static_val( A *p, int m ){
p->pa->_val = m;
}
void A::static_val( A *p, int m ){
p->pa->_val = m;
}
#7
静态成员函数调用非静态数据成员意义不大
不过使静态成员函数实现多态调用有时候是会用到的
具体就是比如说创建一个自己的线程类
不过这样也可以访问非静态数据成员滴......
不过使静态成员函数实现多态调用有时候是会用到的
具体就是比如说创建一个自己的线程类
不过这样也可以访问非静态数据成员滴......
#8
只能传一个实例进去,
class A
{
public:
static void func(A self) {
self.m_a = 1;
}
int m_a;
};
class A
{
public:
static void func(A self) {
self.m_a = 1;
}
int m_a;
};
#9
#pragma once
typedef unsigned(__stdcall *pThreadFunc)(void*);
#define smBeginThread(psa,cbStack,pfnStartAddr,pvParam, \
dwCreate,pdwThreadId) \
(HANDLE)_beginthreadex(psa,(unsigned int)cbStack, \
(pThreadFunc)pfnStartAddr,(void*)pvParam,dwCreate,(unsigned*)pdwThreadId)
class CMyThread
{
private:
//禁止拷贝和将对象赋值给其他对象
CMyThread(CMyThread&);
CMyThread & operator =(CMyThread&);
private:
static unsigned int __stdcall ThreadProxy( void *pvParam );
protected:
virtual int Run(void *pvParam)=0;
~CMyThread(void); //使对象只能从堆中创建
protected:
HANDLE _hThread;
public:
enum _DESTROY_TYPE{_dtWAIT=0, //阻塞等待线程直到线程退出
_dtTRY, //如果线程已经退出,则删除,否则立刻返回
_dtTERMINATE}; //强制关闭线程
public:
CMyThread(void);
int Create(LPVOID pvParam,BOOL autoDelete=TRUE
,DWORD dwCreate=CREATE_SUSPENDED
,LPSECURITY_ATTRIBUTES psa=NULL,SIZE_T cbStack=0
,LPDWORD pdwThreadId=NULL);
/*******************************省略***********************/
};
#include <Windows.h>
#include <process.h>
#include ".\mythread.h"
#ifndef ASSERT
#include <cassert>
#define ASSERT(s) assert(s)
#endif
namespace
{
class _ThreadProxy
{
friend CMyThread;
_ThreadProxy(_ThreadProxy&);
_ThreadProxy& operator =(_ThreadProxy);
private:
CMyThread* _pThis;
void *_pvParam;
BOOL _AutoDelete;
_ThreadProxy(CMyThread*thread,void *pvParam,BOOL autoDelete)
:_pThis(thread)
,_pvParam(pvParam)
,_AutoDelete(autoDelete)
{
}
};
}
CMyThread::CMyThread(void)
:_hThread(0)
{
}
CMyThread::~CMyThread(void)
{
if (_hThread)
{
CloseHandle(_hThread);
}
_hThread=NULL;
}
unsigned int CMyThread::ThreadProxy( void *pvParam )
{
_ThreadProxy *proxy=(_ThreadProxy*)pvParam;
ASSERT(proxy);
CMyThread*pThis=proxy->_pThis;
void *param=proxy->_pvParam;
int bAutoDelete=proxy->_AutoDelete;
delete proxy;
int iRet=pThis->Run(param);
if (bAutoDelete)
{
delete pThis;
}
return iRet;
}
int CMyThread::Create(LPVOID pvParam,BOOL autoDelete/* =TRUE */
,DWORD dwCreate/* =CREATE_SUSPENDED */,LPSECURITY_ATTRIBUTES
psa/* =NULL */,SIZE_T cbStack/* =0 */,LPDWORD pdwThreadId/* =NULL */)
{
_hThread=smBeginThread(psa,cbStack,ThreadProxy,
(new _ThreadProxy(this,pvParam,autoDelete)) ,dwCreate,pdwThreadId);
ASSERT(_hThread);
return _hThread?0:-1;
}
/********************省略*******************************************/
typedef unsigned(__stdcall *pThreadFunc)(void*);
#define smBeginThread(psa,cbStack,pfnStartAddr,pvParam, \
dwCreate,pdwThreadId) \
(HANDLE)_beginthreadex(psa,(unsigned int)cbStack, \
(pThreadFunc)pfnStartAddr,(void*)pvParam,dwCreate,(unsigned*)pdwThreadId)
class CMyThread
{
private:
//禁止拷贝和将对象赋值给其他对象
CMyThread(CMyThread&);
CMyThread & operator =(CMyThread&);
private:
static unsigned int __stdcall ThreadProxy( void *pvParam );
protected:
virtual int Run(void *pvParam)=0;
~CMyThread(void); //使对象只能从堆中创建
protected:
HANDLE _hThread;
public:
enum _DESTROY_TYPE{_dtWAIT=0, //阻塞等待线程直到线程退出
_dtTRY, //如果线程已经退出,则删除,否则立刻返回
_dtTERMINATE}; //强制关闭线程
public:
CMyThread(void);
int Create(LPVOID pvParam,BOOL autoDelete=TRUE
,DWORD dwCreate=CREATE_SUSPENDED
,LPSECURITY_ATTRIBUTES psa=NULL,SIZE_T cbStack=0
,LPDWORD pdwThreadId=NULL);
/*******************************省略***********************/
};
#include <Windows.h>
#include <process.h>
#include ".\mythread.h"
#ifndef ASSERT
#include <cassert>
#define ASSERT(s) assert(s)
#endif
namespace
{
class _ThreadProxy
{
friend CMyThread;
_ThreadProxy(_ThreadProxy&);
_ThreadProxy& operator =(_ThreadProxy);
private:
CMyThread* _pThis;
void *_pvParam;
BOOL _AutoDelete;
_ThreadProxy(CMyThread*thread,void *pvParam,BOOL autoDelete)
:_pThis(thread)
,_pvParam(pvParam)
,_AutoDelete(autoDelete)
{
}
};
}
CMyThread::CMyThread(void)
:_hThread(0)
{
}
CMyThread::~CMyThread(void)
{
if (_hThread)
{
CloseHandle(_hThread);
}
_hThread=NULL;
}
unsigned int CMyThread::ThreadProxy( void *pvParam )
{
_ThreadProxy *proxy=(_ThreadProxy*)pvParam;
ASSERT(proxy);
CMyThread*pThis=proxy->_pThis;
void *param=proxy->_pvParam;
int bAutoDelete=proxy->_AutoDelete;
delete proxy;
int iRet=pThis->Run(param);
if (bAutoDelete)
{
delete pThis;
}
return iRet;
}
int CMyThread::Create(LPVOID pvParam,BOOL autoDelete/* =TRUE */
,DWORD dwCreate/* =CREATE_SUSPENDED */,LPSECURITY_ATTRIBUTES
psa/* =NULL */,SIZE_T cbStack/* =0 */,LPDWORD pdwThreadId/* =NULL */)
{
_hThread=smBeginThread(psa,cbStack,ThreadProxy,
(new _ThreadProxy(this,pvParam,autoDelete)) ,dwCreate,pdwThreadId);
ASSERT(_hThread);
return _hThread?0:-1;
}
/********************省略*******************************************/
#10
class A
{
public:
static void func(A& a) {
a.m_a = 1;
}
int m_a;
};
{
public:
static void func(A& a) {
a.m_a = 1;
}
int m_a;
};