我有以下代码,我想在 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。