Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8499699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:44:18+00:00 2026-06-11T00:44:18+00:00

I have the following code where I want to declare an object inside an

  • 0

I have the following code where I want to declare an object inside an IF statement. I understand that just declaring it inside an IF means that it’s outside the scope of the rest of the program. So I think I need to use a pointer.
My problem is that the classes that I want the user to choose from are derived from a base class. So I figured I probably need to create a pointer based on this base class… That sound about right?

See below for what I’m try to do, am I in the right ball park?

//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);

The following classes are all derived from the class SVAlgorithm…
SVOpenCVBlockMatching,
SVOpenCVSemiGlobalBlockMatching,
SVNoMD,
SVCross,
SVAdaptDP.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T00:44:19+00:00Added an answer on June 11, 2026 at 12:44 am

    No, your code is wrong. Pointer has absolutely no effect on object lifetime.
    So this line SVAlgorithm &DoStereo = *ptrSter; gives you Undefined Behavior because you try to dereference the pointer to destroyed object.

    Another possible problem is uninitialized ptrSter and StereoOption variables. Looks like here is possible case when ptrSter will not be assigned with proper object and its dereference will lead to UB too.

    You need to use dynamic storage with new statement and store result in some pointer. It’s highly recommended to use smart pointer instead of raw for this purpose, e.g. std::auto_ptr for C++03 or std::unique_ptr for C++11.

    Example:

     std::unique_ptr<SVAlgorithm> ptrSter;
    
     if (StereoOption == 1) { ptrSter.reset(new SVOpenCVBlockMatching()); }
     else if (StereoOption == 2) { ptrSter.reset(new SVOpenCVSemiGlobalBlockMatching()); }
     else if (StereoOption == 3) { ptrSter.reset(new SVNoMD()); } 
     else if (StereoOption == 4) { ptrSter.reset(new SVCross()); } 
     else if (StereoOption == 5) { ptrSter.reset(new SVAdaptDP()); }
    
     if(!ptrSter)
     {
        assert(false);
        return;
     }
    
     SVAlgorithm &DoStereo = *ptrSter;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code and i want to pass additional parameter in JSON.getJSONfromURL() method
I have the following code I want to run, but the problem is $this->type
I want to know is below code correct ? I have following code which
I have the following code: // Dictionary which I want to optimize Dictionary<string, MyClass>
I have the following php code which I want to add a delay too:
Hi guys I have the following code and and I want it to last
I have the following code. What I want to do is count the frequency
I have the following code, which I want to load values on a selection
I have the following code: foreach (var control in this.Controls) { } I want
I have the following code where after a bool is true I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.