-
General Class
have to declare a variable(class) depending on some info I get from a file, something like this:
Code:
/*************************/
(...)
if (what_I_read == '1')
Class1 system();
else
Class2 system();
(...)
system.start();
system.stop();
/**************************/
So I want the variable 'system' to be something different depending on certain conditions, but I get a compiling error because 'system' isn't declared. How can I do something like that? Maybe another Template Class???
-
You're pretty much looking at polymorphism with this problem:
Code:
// Oversimplified example
class Base {};
class Derived1: public Base {};
class Derived2: public Base {};
Base *system;
if (something)
system = new Derived1();
else
system = new Derived2();
system->start();
system->stop();
-
Also, just so you know, the following code piece does not create an instance of class SomeClass, it declares a function returning an instance of class SomeClass:
Code:
SomeClass a();
a.method(); // This is wrong, 'a' is a function not an instance of SomeClass
This is the correct way to create an instance of a class with the default constructor
Code:
SomeClass b;
b.method(); // This works, because now 'b' is indeed an instance of SomeClass
Just thought you'd like to know that, seeing as how you are declaring functions in your own example...
-
and
Quote:
'system' isn't declared.
you system var is declared in 2 diferent scopes, which are the if-block and else-block.
outside them, system doesn't exist. you have to declare it outside the ' if - else ' like Prelude showed.