class 내의 static 함수와 변수

박호준·2021년 9월 1일
0

class 내의 static 함수를 사용하면
객체를 선언하는 메소드가 아니라 namespace처럼 클래스명을 선언하고 사용할 수 잇다. 대신 this 포인터 사용 불가능

ex)

class Fixed
{
	public :
		Fixed(void);
		Fixed(Fixed const &other);
		~Fixed(void);
		Fixed &operator = (Fixed const &other);
		int	getRawBits(void) const;
		void	setRawBits(int const raw);

	private :
		int	value;
		static const int	fractional_bits = 8;
};

int main()
{
	Fixed a;
	Fixed const b( Fixed( 5.05f ) * Fixed( 2 ) );
    
	std::cout << Fixed::max( a, b ) << std::endl; //static 함수선언
	std::cout << a.max( a, b ) << std::endl;//static 함수 미선언
                                                //(부적절함)
    
 }

static 변수 :: 동일한 객체 내에서 공유하는 변수
( 객체 내에서 사용하는 전역변수라고 생각하면 좋음)

profile
hopark

0개의 댓글