我在C ++中使用多重继承,并通过显式调用基本方法来扩展基本方法。假定以下层次结构:
Creature
/ \
Swimmer Flier
\ /
Duck
对应于
class Creature
{
public:
virtual void print()
{
std::cout << "I'm a creature" << std::endl;
}
};
class Swimmer : public virtual Creature
{
public:
void print()
{
Creature::print();
std::cout << "I can swim" << std::endl;
}
};
class Flier : public virtual Creature
{
public:
void print()
{
Creature::print();
std::cout << "I can fly" << std::endl;
}
};
class Duck : public Flier, public Swimmer
{
public:
void print()
{
Flier::print();
Swimmer::print();
std::cout << "I'm a duck" << std::endl;
}
};
现在这带来了一个问题-调用鸭子的print
方法将调用其各自的基本方法,所有这些继而又调用Creature::print()
方法,因此最终会被调用两次-
I'm a creature
I can fly
I'm a creature
I can swim
I'm a duck
我想找到一种方法来确保基本方法仅被调用一次。与虚拟继承的工作方式类似(在第一次调用时调用基本构造函数,然后仅在来自其他派生类的后续调用中才为其分配指针)。
是否有内置的方法可以做到这一点,还是我们需要自己实现?
如果是,您将如何处理?
该问题并非特定于打印。我想知道是否存在一种扩展基本方法和功能的机制,同时保持调用顺序并避免出现钻石问题。
我现在知道,最突出的解决方案是添加辅助方法,但我只是想知道是否存在“更清洁”的方法。
答案 0 :(得分:50)
这很可能是XY问题。但是...只是不要打两次。
#include <iostream>
class Creature
{
public:
virtual void identify()
{
std::cout << "I'm a creature" << std::endl;
}
};
class Swimmer : public virtual Creature
{
public:
virtual void identify() override
{
Creature::identify();
tell_ability();
std::cout << "I'm a swimmer\n";
}
virtual void tell_ability()
{
std::cout << "I can swim\n";
}
};
class Flier : public virtual Creature
{
public:
virtual void identify() override
{
Creature::identify();
tell_ability();
std::cout << "I'm a flier\n";
}
virtual void tell_ability()
{
std::cout << "I can fly\n";
}
};
class Duck : public Flier, public Swimmer
{
public:
virtual void tell_ability() override
{
Flier::tell_ability();
Swimmer::tell_ability();
}
virtual void identify() override
{
Creature::identify();
tell_ability();
std::cout << "I'm a duck\n";
}
};
int main()
{
Creature c;
c.identify();
std::cout << "------------------\n";
Swimmer s;
s.identify();
std::cout << "------------------\n";
Flier f;
f.identify();
std::cout << "------------------\n";
Duck d;
d.identify();
std::cout << "------------------\n";
}
I'm a creature
------------------
I'm a creature
I can swim
I'm a swimmer
------------------
I'm a creature
I can fly
I'm a flier
------------------
I'm a creature
I can fly
I can swim
I'm a duck
------------------
答案 1 :(得分:22)
我们可以让基类跟踪属性:
select count(*)
from ibc_offer
where DEACTIVATION_DTTM > date('31.12.9999 23:00:00','DD.MM.YYYY HH:MI:SS')
and DEACTIVATION_DTTM < date('31.12.9999 23:59:59','DD.MM.YYYY HH:MI:SS');
同样,如果不只是打印,而是函数调用,那么我们可以让基类跟踪函数:
#include <iostream>
#include <string>
#include <vector>
using namespace std::string_literals;
class Creature
{
public:
std::string const attribute{"I'm a creature"s};
std::vector<std::string> attributes{attribute};
virtual void print()
{
for (auto& i : attributes)
std::cout << i << std::endl;
}
};
class Swimmer : public virtual Creature
{
public:
Swimmer() { attributes.push_back(attribute); }
std::string const attribute{"I can swim"s};
};
class Flier : public virtual Creature
{
public:
Flier() { attributes.push_back(attribute); }
std::string const attribute{"I can fly"s};
};
class Duck : public Flier, public Swimmer
{
public:
Duck() { attributes.push_back(attribute); }
std::string const attribute{"I'm a duck"s};
};
int main()
{
Duck d;
d.print();
}
答案 2 :(得分:8)
一种简单的方法是创建一堆帮助程序类,以模仿主层次结构的继承结构,并在其构造函数中进行所有打印。
struct CreaturePrinter {
CreaturePrinter() {
std::cout << "I'm a creature\n";
}
};
struct FlierPrinter: virtual CreaturePrinter ...
struct SwimmerPrinter: virtual CreaturePrinter ...
struct DuckPrinter: FlierPrinter, SwimmerPrinter ...
然后,主层次结构中的每个打印方法仅创建相应的帮助器类。没有手动链接。
为便于维护,您可以将每个打印机类嵌套在其对应的主类中。
自然,在大多数实际情况下,您希望将对主对象的引用作为参数传递给其辅助函数的构造函数。
答案 3 :(得分:5)
您对print
方法的显式调用构成了问题的症结所在。
一种解决方法是丢弃print
呼叫,并用say代替
void queue(std::set<std::string>& data)
,然后将打印消息累积到set
中。然后,层次结构中的那些函数被调用一次以上都没关系。
然后,您可以在Creature
中以单个方法来实现打印集。
如果要保留打印顺序,则需要用另一个遵守插入顺序并拒绝重复项的容器替换set
。
答案 4 :(得分:5)
如果要使用该中级方法,请不要调用基类方法。最简单最简单的方法是提取其他方法,然后重新实现Print
很容易。
class Creature
{
public:
virtual void print()
{
std::cout << "I'm a creature" << std::endl;
}
};
class Swimmer : public virtual Creature
{
public:
void print()
{
Creature::print();
detailPrint();
}
void detailPrint()
{
std::cout << "I can swim" << std::endl;
}
};
class Flier : public virtual Creature
{
public:
void print()
{
Creature::print();
detailPrint();
}
void detailPrint()
{
std::cout << "I can fly" << std::endl;
}
};
class Duck : public Flier, public Swimmer
{
public:
void print()
{
Creature::Print();
Flier::detailPrint();
Swimmer::detailPrint();
detailPrint();
}
void detailPrint()
{
std::cout << "I'm a duck" << std::endl;
}
};
没有细节,您的实际问题是什么,很难找到更好的解决方案。
答案 5 :(得分:4)
使用:
template<typename Base, typename Derived>
bool is_dominant_descendant(Derived * x) {
return std::abs(
std::distance(
static_cast<char*>(static_cast<void*>(x)),
static_cast<char*>(static_cast<void*>(dynamic_cast<Base*>(x)))
)
) <= sizeof(Derived);
};
class Creature
{
public:
virtual void print()
{
std::cout << "I'm a creature" << std::endl;
}
};
class Walker : public virtual Creature
{
public:
void print()
{
if (is_dominant_descendant<Creature>(this))
Creature::print();
std::cout << "I can walk" << std::endl;
}
};
class Swimmer : public virtual Creature
{
public:
void print()
{
if (is_dominant_descendant<Creature>(this))
Creature::print();
std::cout << "I can swim" << std::endl;
}
};
class Flier : public virtual Creature
{
public:
void print()
{
if (is_dominant_descendant<Creature>(this))
Creature::print();
std::cout << "I can fly" << std::endl;
}
};
class Duck : public Flier, public Swimmer, public Walker
{
public:
void print()
{
Walker::print();
Swimmer::print();
Flier::print();
std::cout << "I'm a duck" << std::endl;
}
};
对于Visual Studio 2015,输出为:
I'm a creature
I can walk
I can swim
I can fly
I'm a duck
但是is_dominant_descendant
没有可移植的定义。我希望这是一个标准概念。
答案 6 :(得分:2)
您要在函数级别上请求类似继承的内容,该级别会自动调用继承的函数,并且只会添加更多代码。您还希望像类继承一样以虚拟方式完成此操作。伪语法:
class Swimmer : public virtual Creature
{
public:
// Virtually inherit from Creature::print and extend it by another line of code
void print() : virtual Creature::print()
{
std::cout << "I can swim" << std::endl;
}
};
class Flier : public virtual Creature
{
public:
// Virtually inherit from Creature::print and extend it by another line of code
void print() : virtual Creature::print()
{
std::cout << "I can fly" << std::endl;
}
};
class Duck : public Flier, public Swimmer
{
public:
// Inherit from both prints. As they were created using "virtual function inheritance",
// this will "mix" them just like in virtual class inheritance
void print() : Flier::print(), Swimmer::print()
{
std::cout << "I'm a duck" << std::endl;
}
};
所以您的问题的答案
有一些内置的方法吗?
是否。这样的东西在C ++中不存在。另外,我不知道有其他类似这样的语言。但这是一个有趣的主意...