问题描述
我有3种类型的对象:bob1,bob2和bob3。每个对象都是由一个唯一ID标识的,该函数由函数getId()返回。
所有bobs都是BaseBob类的后代,它是一个抽象类
有虚函数getId()。
然后我有一个函数在bob遇到时打印一个特殊的字符串
另一个bob(bobs的所有组合都必须满足,因为它们是b
精神分裂症他们也可以满足自己):
void meet_bob(BaseBob b1,BaseBob b2){
if(b1.getType == 1&& b2.getType == 1)
{
cout<< bob1对bob1说嗨 << endl;
}
else if(b1.getType == 1&& b2.getType == 2)
{
cout<< bob1对bob2说嗨 << endl;
}
else if(b1.getType == 1&& b2.getType == 3)
{
cout<< bob1对hi bob3说 << endl;
}
else if(b1.getType == 2&& b2.getType == 2)
{
cout<< bob2对bob2说嗨 << endl;
}
else if(b1.getType == 2&& b2.getType == 3)
{
cout<< bob2对bob3说 <<结束;
}
...
...
}
这不太好看。如果我想使用一个开关,我仍然需要
为外部开关中的每个外壳做一个开关,然后我有一个带嵌套开关的设计
(其中甚至更丑陋。
有没有更好的方法来处理这种情况或者使用嵌套交换机的b $ b是最好的解决方案?
I have 3 types of objects: bob1, bob2 and bob3. Each object is
identified by a unique ID which gets returned by the function getId().
All bobs are descendants from class BaseBob which is an abstract class
that has the virtual function getId().
I then have a function that prints a special string when a bob meets
another bob (all combinations of bobs has to meet and since the are
schizophrenic they can also meet themselves):
void meet_bob(BaseBob b1, BaseBob b2){
if (b1.getType == 1 && b2.getType == 1)
{
cout << "bob1 says hi to bob1" << endl;
}
else if (b1.getType == 1 && b2.getType == 2)
{
cout << "bob1 says hi to bob2" << endl;
}
else if (b1.getType == 1 && b2.getType == 3)
{
cout << "bob1 says hi to bob3" << endl;
}
else if (b1.getType == 2 && b2.getType == 2)
{
cout << "bob2 says hi to bob2" << endl;
}
else if (b1.getType == 2 && b2.getType == 3)
{
cout << "bob2 says hi to bob3" << endl;
}
...
...
}
This is not pretty. If I want to use a switch instead I still have to
make a switch for each case in the outer switch and then I have a design
with nested switches instead (which is even uglier).
Is there some better way to treat this kind of situation or is the use
of nested switches the best solution?
推荐答案
嗯... getId()没有出现在你的代码中......
Um... getId() doesn''t appear in your code...
如果BaseBob是抽象的,你就不能通过值传递它,因为
你不能实例化它。
If BaseBob is abstract, you can''t pass it by value, since
you can''t instantiate it.
我猜你想要什么,但是怎么样:
void meet_bob(BaseBob * pb1,BaseBob * pb2 ){$ / $
cout<< "鲍勃" << pb1-> getId()<< "嗨说鲍勃 << pb2-> getId()<<结束;
...
}
假设一个合理的运算符<<返回类型为getId()。
-
Lionel B
I''m guessing at what you want, but how about something like:
void meet_bob(BaseBob* pb1, BaseBob* pb2){
cout << "bob" << pb1->getId() << " says hi to bob" << pb2->getId() << endl;
...
}
assuming a sensible operator << for the return type of getId().
--
Lionel B
嗯... getId()没有出现在你的代码中......
Um... getId() doesn''t appear in your code...
如果BaseBob是抽象的,你不能通过价值来传递它,因为
你不能实例化它。
If BaseBob is abstract, you can''t pass it by value, since
you can''t instantiate it.
我猜你想要什么,但是怎么样:
void meet_bob (BaseBob * pb1,BaseBob * pb2){
I''m guessing at what you want, but how about something like:
void meet_bob(BaseBob* pb1, BaseBob* pb2){
void meet_bob(const BaseBob& b1,const BaseBob& b2){
void meet_bob(const BaseBob& b1, const BaseBob& b2) {
cout<< "鲍勃" << b1.getType()<< "嗨说鲍勃 << b2.getType()
<<结束;
-
Erik Wikstr?m
cout << "bob" << b1.getType() << " says hi to bob" << b2.getType()
<< end;
--
Erik Wikstr?m
嗯... getId()没有出现在你的代码中......
Um... getId() doesn''t appear in your code...
如果BaseBob是抽象的,你不能按值传递它,因为
你不能实例化它。
If BaseBob is abstract, you can''t pass it by value, since
you can''t instantiate it.
我是猜测你想要什么,但是怎么样:
void meet_bob(BaseBob * pb1,BaseBob * pb2){
cout< ;< "鲍勃" << pb1-> getId()<< "嗨说鲍勃 << pb2-> getId()<<结束;
...
}
假设一个合理的运算符<<对于返回类型的getId()。
I''m guessing at what you want, but how about something like:
void meet_bob(BaseBob* pb1, BaseBob* pb2){
cout << "bob" << pb1->getId() << " says hi to bob" << pb2->getId() << endl;
...
}
assuming a sensible operator << for the return type of getId().
我更喜欢一般的方法来优化大量的
嵌套& ;如果的"或开关。在每种情况下我都可能想要做一些事情
将来打印字符串会更复杂。
处理大型字符串时是否有嵌套交换机的替代方案
组合数量?我正在考虑一个树形结构,但我确定如果c ++支持这个结构。
Well I was more looking for at general way to optimize a large number of
nested "if''s" or "switches". In each case I might want to do something
more complex that print a string in the future.
Are there alternatives to nested switches when dealing with a large
number of combinations? I was thinking about a tree structure but am nit
sure if that is supported in c++.
这篇关于避免嵌套“if's”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!