CPP 개념들

Seungsoo Lee·2022년 7월 26일
0

C Cpp

목록 보기
1/4

무작위임

1. vector initialization with array

https://stackoverflow.com/a/2236227/17522808

static const int arr[] = {16,2,77,29};
vector<int> vec (arr, arr + sizeof(arr) / sizeof(arr[0]) );
  • cpp00/ex02/tests.cpp
	typedef std::vector<Account::t>							  accounts_t;

	int	const				amounts[]	= { 42, 54, 957, 432, 1234, 0, 754, 16576 };
	size_t const			amounts_size( sizeof(amounts) / sizeof(int) );
	accounts_t				accounts( amounts, amounts + amounts_size );

2. function pointer array

https://docs.microsoft.com/ko-kr/troubleshoot/developer/visualstudio/cpp/language-compilers/declare-pointers-to-functions

void test1();
void test2();            /*  Prototypes */
void test3();

/* array with three functions */
void (*functptr[])() = { test1, test2, test3 } ;

만약 클레스의 함수들이라면,
hpp file

#ifndef HARL_CLASS_H
# define HARL_CLASS_H

class Harl
{
	private:
    	void test1( void );
        void test2( void );
        void test3( void );
    public:
    	Harl();
        ~Harl();
};

#endif

cpp file

#include "Harl.cpp"

int main()
{    
    void (Harl::*funcptr[]) (void ) { &Harl::test1, &Harl::test2, &Harl::test3 }
    for (int i = 0; i < 3; i++)
    {
		*funcptr[i]();
	}
}

0개의 댓글