|
-
Mar 27th, 2002, 04:28 AM
#1
Thread Starter
Junior Member
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/
-
Mar 27th, 2002, 07:01 AM
#2
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;
}
-
Mar 27th, 2002, 07:01 AM
#3
transcendental analytic
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.
-
Mar 27th, 2002, 07:25 AM
#4
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;
}
}
-
Mar 27th, 2002, 08:30 AM
#5
transcendental analytic
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|