Conflicting declaration of objects. Need Solution Please
The Light and Fan are two different vendor classes and they are not derived from any base class.
I am trying to implement the Command design pattern but with generic implementation, so it should work in future with any new vendor class like Door, Window etc without much change in client code. I have thought about a Factory method but it will not work because it needs a Base class.
It is not an assignment; I am trying to learn the design patterns.
Light *myobj;
Fan *myobj;
int choice;
cout<<"Select Light (1): ";
cout<<"Select Fan (2): ";
cin>>choice;
if (choice ==1){
Light *myobj = new Light();
}
if (choice ==2){
Fan *myobj = new Fan();
}
Re: Conflicting declaration of objects. Need Solution Please
Originally Posted by fsixteen
Ok. then I have to put FlipUpCommand switchUp(*myobj); statement in each 'IF' which I do not want to do.
Then you could do something like
Code:
void *myobj = NULL;
Light *myLight;
Fan *myFan;
int choice;
cout<<"Select Light (1): ";
cout<<"Select Fan (2): ";
cin>>choice;
if (choice ==1)
{
Light *myLight = new Light();
myobj = (void*)myLight;
}
if (choice ==2)
{
Fan *myFan = new Fan();
myobj = (void*)myFan;
}
FlipUpCommand switchUp(*myobj);
Note that in such a case your FlipUpCommand class had to know what type of object (Light or Fan) was actually passed in and cast the void* pointer to corresponding class.
Re: Conflicting declaration of objects. Need Solution Please
Originally Posted by VictorN
Then you could do something like
Code:
void *myobj = NULL;
Light *myLight;
Fan *myFan;
int choice;
cout<<"Select Light (1): ";
cout<<"Select Fan (2): ";
cin>>choice;
if (choice ==1)
{
Light *myLight = new Light();
myobj = (void*)myLight;
}
if (choice ==2)
{
Fan *myFan = new Fan();
myobj = (void*)myFan;
}
FlipUpCommand switchUp(*myobj);
Note that in such a case your FlipUpCommand class had to know what type of object (Light or Fan) was actually passed in and cast the void* pointer to corresponding class.
Re: Conflicting declaration of objects. Need Solution Please
Originally Posted by fsixteen
Why the colors are not appearing in my code ?
You'd better have asked: "Why the code indentations are not appearing in my code ?"
And the answer would be very simple: just because you put unformatted code between the tags!
Note, that I didn't ask you to repost the code, just to edit the previous post adding code tags!
Re: Conflicting declaration of objects. Need Solution Please
Well, I have to tell you again (and it is the last time!):
Please, post properly formatted code with indentations and within Code tags!
And not just a couple of lines but your actual code (or code snippet) that we will be able to compile and test.
Note that the code you have posted in the post#12 has nothing to do with the one in your OP!
Bookmarks