当类使用多重继承时,如何安全地设计移动构造函数?
请考虑以下情形:
struct T { };
struct U { };
struct X : public T, public U
{
X(X&& other)
: T(std::move(other))
, U(std::move(other)) // already moved?!
{
}
};
Run Code Online (Sandbox Code Playgroud)
有没有办法移动 - 构建T和U安全?
c++ multiple-inheritance move-constructor move-semantics c++11
MSVC, Clang and GCC disagree on this code:
struct Base { int x; };
struct Der1 : public Base {};
struct Der2 : public Base {};
struct AllDer : public Der1, public Der2 {
void foo() {
Der1::Base::x = 5;
}
};
Run Code Online (Sandbox Code Playgroud)
GCC:
<source>: In member function 'void AllDer::foo()':
<source>:10:21: error: 'Base' is an ambiguous base of 'AllDer'
10 | Der1::Base::x = 5;
| ^
Compiler returned: 1
Run Code Online (Sandbox Code Playgroud)
Clang gives a similar error, and MSVC gives no error.
Who …
c++ multiple-inheritance qualified-name diamond-problem language-lawyer
VB .Net中是否可以进行多重继承?如果是这样,语法是什么?
使用Python super()进行方法链接时,必须明确指定自己的类,例如:
class MyDecorator(Decorator):
def decorate(self):
super(MyDecorator, self).decorate()
Run Code Online (Sandbox Code Playgroud)
我必须指定我的类的名称MyDecorator作为参数super().这不是DRY.当我重命名我的课程时,我将不得不重命名它两次.为什么这样实现?有没有办法避免必须两次(或更多)写出类的名称?
我将IList绑定到GridView.IMyInterface看起来像
public interface IMyInterface: IHasTotalHours, IHasLines
{
DateTime GoalStartDate { get; set; }
DateTime GoalEndDate { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我将一个实例绑定到Grid,如下所示:
IList<IMyInterface> instance= GetMyData();
myGrid.DataSource = instance;
myGrid.DataBind();
Run Code Online (Sandbox Code Playgroud)
将此绑定到网格时,显示在网格中的唯一成员是IMyInterface的直接成员:GoalStartDate和GoalEndDate.
这是为什么?如何让网格显示其继承的其他接口的成员?
更新 继承的接口定义简单的数据属性,如
public interface IHasTotalHours
{
string Description { get; set; }
int Hours{ get; set; }
}
public interface IHasLines
{
double TotalLines { get; set; }
double LinesPerHour { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
有一个实现IMyInterface的类:
public class MyClass : IMyInterface
{
public string Description { get; set; }
public …Run Code Online (Sandbox Code Playgroud) data-binding reflection interface multiple-inheritance typedescriptor
我正在完全重写一个旧库,我不知道如何处理这种情况(为了理解,所有人都欢呼自行车比喻):
我有以下课程:
TBike - 自行车本身TBikeWheel - 自行车的一个轮子TBikeWheelFront并且TBikeWheelBack,它们都从它继承TBikeWheel并实现了它们所需的特定内容这非常简单,但现在我决定制造多种自行车,每辆自行车都有自己的轮子 - 它们和普通的前轮/后轮一样,加上特定的自行车.
TBikeXYZ - 继承自 TBikeTBikeWheelXYZ - 继承自 TBikeWheel这是我的问题:TBikeWheelFrontXYZ应该继承TBikeWheelXYZ(以获得XYZ轮的特定方法),但它也应该继承TBikeWheelFront(以获得前轮的特定方法).
我的问题是,如何以不这样的方式实现它:
我目前正致力于在非RTTI平台(Android)上集成使用大量RTTI内容的第三方软件包.基本上,我做了自己的RTTI实现,但我遇到了问题.
问题是很多类都有钻石继承问题,因为所有类派生自相同的基类(对象)..所以,如果我想从基类转发到派生类,我必须使用一个dynamic_cast - 但RTTI不可用!当没有dynamic_cast的虚拟继承时,如何将对象从父对象转换为子对象?
它看起来像这样:
class A
{
public:
virtual char* func() { return "A"; };
};
class B : public virtual A
{
public:
//virtual char* func() { return "B"; };
};
class C : public virtual A
{
public:
//virtual char* func() { return "C"; };
};
class D : public B, public C
{
public:
//virtual char* func() { return "D"; };
};
D d;
A* pa = static_cast<A*>(&d);
D* pd = static_cast<D*>(pa); // can't do that! …Run Code Online (Sandbox Code Playgroud) 可能重复:
Super可以处理多重继承吗?
Python继承?我有一个类结构(下面),并希望子类调用__init__父双方.这可能是以"超级"方式做的还是只是一个可怕的想法?
class Parent1(object):
def __init__(self):
self.var1 = 1
class Parent2(object):
def _init__(self):
self.var2 = 2
class Child(Parent1, Parent2):
def __init__(self):
## call __init__ of Parent1
## call __init__ of Parent2
## super(Child, self).__init__()
Run Code Online (Sandbox Code Playgroud) 我将以下内容称为"多重继承":
我想知道它是否存在以及如何明确地访问嵌入的子对象.
1.)[ 专业C++,2 次编] †中指明了编译程序不能有直接继承其两个直接父类和所述父的父类.这是真的吗?
鉴于a GrandParent和Parent,扩展GrandParent,VC12和g ++允许a GrandChild直接从两者Parent和GrandParent.继承.在VC12和g ++中,可以按如下方式定义这些类:
GrandParent声明一个int num数据成员.除继承之外,Parent声明自己的.除了继承's和s 之外,它还声明了它自己.numGrandParentnumGrandChildnumParentGrandParentnum
VC12似乎允许明确的成员访问,但g ++只允许它在某些情况下.
#include <iostream>
using std::cout;
using std::endl;
struct GrandParent { int num; };
struct Parent : GrandParent { int num; };
struct GrandChild : GrandParent, Parent { int num; };
int main()
{
GrandChild gc;
gc.num = …Run Code Online (Sandbox Code Playgroud) c++ ×4
inheritance ×2
python ×2
c++11 ×1
casting ×1
data-binding ×1
delphi ×1
init ×1
interface ×1
oop ×1
php ×1
reflection ×1
rtti ×1
super ×1
vb.net ×1