-
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/
-
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;
}
-
no, you should use polymorphism.
-
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;
}
}
-
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