Results 1 to 5 of 5

Thread: Classes in if statements

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2002
    Posts
    21

    Classes in if statements

    I want to declare a class if a certain condition is true so i have placed teh constructor in an if statement which in turn is inside a while loop. However i cannot access the class outside the if statement. Is there anyway around this. This is what the code looks like -

    void function()

    {
    While (reason)
    {
    if (something == true)
    {
    class newclass;
    //Break out of the loop;
    break;
    }
    }//end while
    newclass.function1();


    Any help would be appreciated/

  2. #2
    amac
    Guest
    You could do something like this...

    Code:
    void function( void )
    {
        MyClass * lpClass = NULL;
    
        while( 1 )
        {
            if( something == true )
            {
                lpClasss = new MyClass;
                break;
            }
        }
           
        if( lpClass != NULL )
        {
    
            lpClass->someMethod();
      
        }
    
        // Clean Up
        delete lpClass;
    
        return;
    
    }

  3. #3
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    no, you should use polymorphism.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  4. #4
    amac
    Guest
    In this case how could polymorphism be used?

    Based off of the information originally given?

    I guess it would also make sense to do this...

    Code:
    while( something == true )
    {
    
       if( somethingelse == true )
       {
             MyClass class;
    
             class.someMethod();
    
             break;
       }
    
    }

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    well if you look at the original case I'd do

    While (reason)
    if (something)
    {
    function1();
    reason=0;
    }
    (which is quite usless) but I thought polymorphism was what he asked for
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

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