如何在一个好的但多重继承的截肢语言如C#中解决"Must be MarshalByRefObject"?
问题非常简单,在某些情况下,您只需继承此类(基础结构要求).这在哪种情况下并不重要.那么,如果你已经从其他一些类(你的域模型要求)继承了,你会怎么做?
顺便说一句好的应用程序框架,就像spring.net一样,无论你需要在什么类型的基础架构中应用你的类,都要确保你不必继承这个类.
我想知道我在这里得到什么-3票?:)
我有一个像这样的钻石多重继承场景:
A
/ \
B C
\ /
D
Run Code Online (Sandbox Code Playgroud)
公共父A定义了虚函数fn().
B和C都可以定义fn()吗?
如果是,那么下一个问题是 - D可以访问B和C的fn()而不消除歧义吗?我假设有一些语法...
而D是否有可能在不知道谁是B和C的情况下做到这一点?B和C可以替换为其他类,我希望D中的代码是通用的.
我想要做的是让D以某种方式枚举它在其祖先中具有的fn()的所有实例.这是否可能在某些其他方面表示虚函数?
我创建了一个子类ListView和两个已实现get_context_data函数的自定义mixin的类.我想在子类上覆盖此函数:
from django.views.generic import ListView
class ListSortedMixin(object):
def get_context_data(self, **kwargs):
print 'ListSortedMixin'
return kwargs
class ListPaginatedMixin(object):
def get_context_data(self, **kwargs):
print 'ListPaginatedMixin'
return kwargs
class MyListView(ListSortedMixin, ListPaginatedMixin, ListView):
def get_context_data(self, **context):
super(ListSortedMixin,self).get_context_data(**context)
super(ListPaginatedMixin,self).get_context_data(**context)
return context
Run Code Online (Sandbox Code Playgroud)
当我执行MyListView它时只打印"ListSortedMixin".出于某种原因,python正在执行ListSortedMixin.get_context_data而不是代替MyListView.get_context_data.为什么?
如果我将继承顺序更改为ListPaginatedMixin, ListSortedMixin, ListView,ListPaginatedMixin.get_context_data则执行.
我该如何覆盖该get_context_data功能?
python django listview multiple-inheritance django-generic-views
我需要编写一个将由新手和经验丰富的C++开发人员使用的编码约定.动态多态的继承规则如下:
- 对于动态多态,请考虑使用单继承(树状层次结构),可能具有抽象接口的多重继承
- 对于继承层次结构(基类等),默认情况下使用公共继承
- 对于继承抽象接口,默认情况下使用公共虚拟继承
此规则之后将提供有关实施,可能的例外等的详细信息.
所以,问题是这个规则是否适合新手和经验丰富的C++开发人员?(欢迎/利弊,以及来源和链接是受欢迎的)
我看到的是:
注意:我已阅读以下在线资源:
注2:在"C++编码标准"第36项中使用Sutter&Alexandrescu后,使用"抽象接口"名称
这是一个应该工作的情况(它的Java/C#等效使用接口工作),但如果接口继承不是虚拟的,那么在C++中就不会这样:
class A
{
public :
virtual ~A() = 0 {}
} ;
class B : public A {} ; // should have been virtual to avoid the error
class C : public A {} ; // should have been virtual to avoid the error
class D : public B, public C
{
public …Run Code Online (Sandbox Code Playgroud) 鉴于此代码:
#include <iostream>
struct A {
};
struct B {
};
struct C {
};
struct E : A {
int field;
};
struct F : A, B {
int field;
};
struct G : A, B, C {
int field;
};
int main() {
std::cout << _MSC_VER << std::endl;
std::cout << sizeof(E) << std::endl;
std::cout << sizeof(F) << std::endl;
std::cout << sizeof(G) << std::endl;
int o;
std::cin >> o;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我得到以下输出:
1900
4
8
8
Run Code Online (Sandbox Code Playgroud)
为什么会F …
我一直在尝试为我正在研究的C++库创建一些自定义异常类.这些自定义异常捕获调试所需的额外信息,例如文件,行号等,如果出于某种原因,在测试异常时没有捕获到正确的位置.然而,大多数人似乎建议继承STL中的std :: exception类,我同意这一点,但我想知道使用多重继承来继承每个派生的std :: exception类会更好(例如.std :: runtime_error)和自定义异常类,如下面的代码?
另一件事,如何在异常类中进行复制构造函数和赋值运算符?他们应该被禁用吗?
class Exception{
public:
explicit Exception(const char *origin, const char *file,
const int line, const char *reason="",
const int errno=0) throw();
virtual ~Exception() throw();
virtual const char* PrintException(void) throw();
virtual int GetErrno(void);
protected:
std::string m_origin;
std::string m_file;
int m_line;
std::string m_reason;
int m_errno;
};
class RuntimeError: public virtual std::runtime_error, public Exception{
public:
explicit RuntimeError(const char *origin, const char *file,
const int line, const char *reason="",
const int errno=0) throw();
virtual ~RuntimeError() …Run Code Online (Sandbox Code Playgroud) 我知道我们可以使用接口从多个类继承,但是也可以继承状态吗?
如何从2个类继承定义方法并将它们放在Java的第三个类中?
以下代码:
struct interface_base
{
virtual void foo() = 0;
};
struct interface : public interface_base
{
virtual void bar() = 0;
};
struct implementation_base : public interface_base
{
void foo();
};
struct implementation : public implementation_base, public interface
{
void bar();
};
int main()
{
implementation x;
}
Run Code Online (Sandbox Code Playgroud)
无法编译时出现以下错误:
test.cpp: In function 'int main()':
test.cpp:23:20: error: cannot declare variable 'x' to be of abstract type 'implementation'
test.cpp:16:8: note: because the following virtual functions are pure within 'implementation':
test.cpp:3:18: note: virtual …Run Code Online (Sandbox Code Playgroud) 所以我有一堆使用SQLAlchemy的表,它们被建模为从结果继承到调用的对象declarative_base().即:
Base = declarative_base()
class Table1(Base):
# __tablename__ & such here
class Table2(Base):
# __tablename__ & such here
Run Code Online (Sandbox Code Playgroud)
等等.然后,我想为每个数据库表类提供一些常用功能,根据文档执行此操作的最简单方法是执行多重继承:
Base = declarative_base()
class CommonRoutines(object):
@classmethod
def somecommonaction(cls):
# body here
class Table1(CommonRoutines, Base):
# __tablename__ & such here
class Table2(CommonRoutines, Base):
# __tablename__ & such here
Run Code Online (Sandbox Code Playgroud)
我不喜欢这件事是A)多重继承一般有点狡猾(变得棘手解决诸如super()调用之类的东西等),B)如果我添加一个新表我必须记住从两者继承Base而且CommonRoutines,和C)实际上,"CommonRoutines"类在某种意义上是"一种"类型的表.真正CommonBase的是一个抽象基类,它定义了一组对所有表都通用的字段和例程.换句话说:"它是一个"抽象表.
那么,我想要的是:
Base = declarative_base()
class AbstractTable(Base):
__metaclass__ = ABCMeta # make into abstract base class
# define common attributes for all …Run Code Online (Sandbox Code Playgroud) 我知道虚拟继承在此之前和之前都有涉及这个问题,我详细介绍了虚拟继承,并详细介绍了类似的问题,如下所示:
多重钻石继承的编译,不虚,但-犯规,与 和 为什么GCC给我一个错误-最终超控器
我的问题略有不同,因为我没有使用纯虚函数并明确使用虚拟继承来拥有一个唯一的'Base'类.层次结构如下:
Base
/\
/ \
Der1 Der2
\ /
Der3
Run Code Online (Sandbox Code Playgroud)
我知道衍生问题上的可怕钻石,这就是我使用虚拟继承的原因.
#include <iostream>
class base
{
public :
base()
{
std::cout<<"base()" << std::endl;
}
virtual void fun()
{
std::cout<<"base" << std::endl;
}
};
class der1: virtual public base
{
public :
void fun()
{
std::cout<<"der1" << std::endl;
}
};
class der2 : virtual public base
{
public :
void fun()
{
std::cout<<"der2" << std::endl;
}
};
class der3 : public der1,public der2
{
public :
/*void …Run Code Online (Sandbox Code Playgroud)