IF 문 내에 객체를 선언하려는 다음 코드가 있습니다. IF로 선언하는 것만으로도 나머지 프로그램의 범위를 벗어난다는 것을 이해합니다. 그래서 포인터를 사용해야한다고 생각합니다. 제 문제는 사용자가 선택할 수있는 클래스가 기본 클래스에서 파생된다는 것입니다. 그래서이 기본 클래스를 기반으로 포인터를 만들 필요가 있다고 생각했습니다. 맞나요?외부에서 사용하기 위해 IF에서 파생 클래스를 선언하는 경우
내가 뭘하려고하는지 아래에서 보시라. 나는 올바른 볼 파크에 있습니까?
//create a stereo input object
StereoImage SO;
SO.open(File1,File2);
int StereoOption;
if (counter == 0) StereoOption = 1;
//think i need to create a pointer to the base class
SVAlgorithm *ptrSter;
//choose which method to use in stereo processing and set the base class pointer to derrived class
if (StereoOption == 1) {SVOpenCVBlockMatching DoStereo; cout << "Using option 1" << endl; *ptrSter = DoStereo;}
else if (StereoOption == 2) {SVOpenCVSemiGlobalBlockMatching DoStereo; cout << "Using option 2" << endl; *ptrSter = DoStereo;}
else if (StereoOption == 3) {SVNoMD DoStereo; cout << "Using option 3" << endl; *ptrSter = DoStereo;}
else if (StereoOption == 4) {SVCross DoStereo; cout << "Using option 4" << endl; *ptrSter = DoStereo;}
else if (StereoOption == 5) {SVAdaptDP DoStereo; cout << "Using option 5" << endl; *ptrSter = DoStereo;}
//set create DoStereo from the pointer
SVAlgorithm &DoStereo = *ptrSter;
//output matrix
Mat_<short> DispOut;
//load the disparity method params, compute it and show - works
DoStereo.loadParams();
DoStereo.compute(SO.getRawFrame(0),SO.getRawFrame(1),DispOut);
imshow("Disparity Map", DispOut*255);
다음 클래스는 모든 클래스 SVAlgorithm에서 파생 된 ... SVOpenCVBlockMatching, SVOpenCVSemiGlobalBlockMatching, SVNoMD, SVCross, SVAdaptDP.
SVAlgorithm *ptrSter = SVFactory(StereoOption);
여기서 당신이 올바른 방향으로 시작
, 당신이해야하지 그것을 역 참조 그래서. 스마트 포인터로 바꾸고'if' 내에 메모리를 할당하는 것을 권하고 싶습니다. – chris
무엇이 질문입니까? – Walter