c++ 属性类外只读技巧

 

类内通过去const来实行修改内容

 

 

 

#include <stdio.h>



class A
{
public:
	A():m_width(0), m_height(0)
	{
	}
	void create()
	{

        *(const_cast<int*> (&m_width)) = 10;

        *(const_cast<int*> (&m_height)) = 100;

	}

public:
	const int 	m_width;
	const int	m_height;
};




int main()
{
	A a;
	a.create();
	printf("%d %d\n", a.m_width, a.m_height);
	a.m_width = 10;
}

  

 

 

posted @ 2012-05-09 09:40  flowskyac  阅读(573)  评论(0编辑  收藏  举报