CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2003
    Location
    London, UK
    Posts
    23

    Selecting a child class problem

    Hi,

    I have an abstract class called Integrator and two child classes, EmbedRK and Rungekutta. In my main.cc what I want to do is define a variable rk as the type that I want to use (either EmbedRK and Rungekutta) depending on a config file. I have the following:

    Code:
    const string integrator_type = config.read<string>( "Integrator" );
    cout << integrator_type << endl;
    if ( integrator_type == "embedrk" )        
    {                
         cout << "Integrator: Adaptive step RungeKutta" << endl;                
         const double startstep = config.read<double>( "StartStep" );                
         cout << startstep << endl;                
         EmbedRK rk(equation, startstep, tolerance);        
    }        
    else if ( integrator_type == "rungekutta" )        
    {                
          cout << "Integrator: RungeKutta" << endl;                
          Rungekutta rk(equation, tolerance);        
    }               
    else        
    {                
          cout << "Integrator not recognised .. exiting" << endl;                
          exit(1);        
    }
    The problem is that when I compile it, it complains when I use rk that 'rk' is not defined. I use it like this:
    Code:
    Model model_2(rk,equation);
    where in this case the function is declared as follows:
    Code:
    Model(const Integrator &, Equations *);
    Any help to sort this out would be great.

    Daniel Brewer

  2. #2
    Join Date
    Feb 2002
    Posts
    5,757
    One solution is RTTI. Pass the base case in the first parameter of model_2().

    Kuphryn

  3. #3
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    Your problem is that you are declaring "rk" inside a block statement. That means it immediately goes out of scope at the end of that block. Your only real solution is to use a pointer:
    Code:
    const string integrator_type = config.read<string>( "Integrator" );
    cout << integrator_type << endl;
    
    Integrator* rk;
    
    if ( integrator_type == "embedrk" )
    {                
         cout << "Integrator: Adaptive step RungeKutta" << endl;
         const double startstep = config.read<double>( "StartStep" );
         cout << startstep << endl;
         rk = new EmbedRK(equation, startstep, tolerance);
    }        
    else if ( integrator_type == "rungekutta" )
    {                
          cout << "Integrator: RungeKutta" << endl;
          rk = new Rungekutta(equation, tolerance);
    }
    else
    {
          cout << "Integrator not recognised .. exiting" << endl;
          exit(1);
    }
    
    Model model2(*rk, equation);
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured