我有一个类的功能,我想依赖于一组插件策略.但是,我不确定如何从一个任意数量的类派生一个类.
下面的代码是我想要实现的一个例子.
// insert clever boost or template trickery here
template< class ListOfPolicies >
class CMyClass : public ListOfPolicies
{
public:
CMyClass()
{
// identifiers should be the result of OR-ing all
// of the MY_IDENTIFIERS in the TypeList.
DWORD identifiers;
DoSomeInitialization( ..., identifiers, ... );
}
int MyFunction()
{
return 100;
}
// ...
};
template< class T >
class PolicyA
{
public:
enum { MY_IDENTIFIER = 0x00000001 };
int DoSomethingA()
{
T* pT = static_cast< T* >( this …Run Code Online (Sandbox Code Playgroud) 我怀疑我们是否可以使用'模块'概念实现多重继承?还是有任何其他关键字或概念在ruby中实现多重继承?
可能的重复:
为什么C#不支持多重继承
C#是否包含多重继承?
我喜欢接口支持多继承和类不支持的原因是什么
将实现多个接口的对象传递给只需要其中一个接口的函数时,我遇到了构建错误.
我正在开发一个通讯包.该软件包有一个Receiver类和一个Sender类.Receiver类使用通知接口Receive_Notifier(函数对象)来处理通知.同样,Sender类使用通知接口Send_Notifier来处理通知.
interface Send_Notifier
{
void sending_text(string text);
}
interface Receive_Notifier
{
void raw_text_received(string raw_text);
}
class Sender
{
Send_Notifier m_notifier = null;
public Sender(ref Send_Notifier notifier)
{
m_notifier = notifier;
}
public void send(string text)
{
m_notifier.sending_text(text);
return;
}
}
class Receiver
{
Receive_Notifier m_notifier = null;
public Sender(ref Receive_Notifier notifier)
{
m_notifier = notifier;
}
public void receive(string text)
{
m_notifier.raw_text_received(text);
return;
}
}
Run Code Online (Sandbox Code Playgroud)
我已将接口组合到Communications_Notifier中:
interface Communications_Notifier
: Send_Notifier, Receive_Notifier
{
}
Run Code Online (Sandbox Code Playgroud)
我创建了一个Notifier实现Communications_Notifier接口的类:
class …Run Code Online (Sandbox Code Playgroud) 我在Lua有两节课.一个继承了另一个.
test1 = {test1Data = 123, id= {0,3}}
function test1:hello()
print 'HELLO!'
end
function test1:new (inp)
inp = inp or {}
setmetatable(inp, self)
self.__index = self
return inp
end
test2 = {}
function test2:bye ()
print ('BYE!', self.id)
end
function test2:new_inst_test (baseClass, inp)
inp = inp or {}
setmetatable(inp, self)
self.__index = self
if baseClass then
setmetatable( inp, { __index = baseClass } )
end
return inp
end
a = test1:new({passData='abc1'})
b = test1:new({passData='ghyrty'})
c = test2:new_inst_test(a,{temp = '123343321135'})
d …Run Code Online (Sandbox Code Playgroud) 你好我在python中搜索类继承,我发现它也支持多重继承,但不知何故似乎有问题:o我找到了一个例子:
class ParentOne:
def __init__(self):
print "Parent One says: Hello my child!"
self.i = 1
def methodOne(self):
print self.i
class ParentTwo:
def __init__(self):
print "Parent Two says: Hello my child"
class Child(ParentOne, ParentTwo):
def __init__(self):
print "Child Says: hello"
A=Child()
Run Code Online (Sandbox Code Playgroud)
产量
Child Says: hello
Run Code Online (Sandbox Code Playgroud)
因此,当子继承ParentOne和ParentTwo时,为什么不初始化这些类?我们应该在继承类Child中手动初始化它们吗?什么是正确的例子,所以我们可以看到只使用继承打印的所有消息?
事实上,它稍微复杂一些; 方法解析顺序动态变化以支持对super()的协作调用.这种方法在一些其他多继承语言中称为call-next-method,并且比单继承语言中的超级调用更强大.
当需要手动初始化时,它如何更强大?对不起,所有这些问题.提前致谢.
C++ 11标准提供了一种从基类继承构造函数的方法.我的问题是关于早期的标准.假设我以下列方式继承构造函数:
class Base {
public:
Base() {};
Base(int b) { a = ++b;}
virtual int foo() {return 0;}
int a;
};
class A : public virtual Base {
public:
A() {}
A(int b): Base(b) {}
int foo() {return a*a;}
};
class C : public A {
public:
C() {}
C(int b ): A(b) {}
int foo() { return (a*a + a);}
};
Run Code Online (Sandbox Code Playgroud)
请注意,我正在使用Base类的虚拟继承.现在,当我尝试初始化指向C类型对象的指针时,代码不再调用Base(b)构造函数,代码最终调用Base()构造函数.这是我使用的主要功能:
int main(){
C *c = new C(5);
std::cout << c->Base::a << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
"a"的输出值为0.但是,当我继承Base类时删除virtual关键字,然后调用Base(b)构造函数并且"a"的值为6.有人可以帮助我理解什么是继续?为什么使用虚拟继承默认构造函数被调用?
我有一个基类,由多个派生类继承.我正在构造函数初始化基类的一些属性.有什么办法可以让我的派生对象共享基类属性,而不是为每个派生类对象创建相同的属性值.这非常重要,因为一些基类属性值是由服务生成的,共享这可以提高性能.以下是我想说的简单蓝图:
public class ClassA
{
//i dont want to use static here as it will be shared for multiple codes
protected string country { get; set; }
public ClassA(string code)
{
country = CallsomeService(code);
}
}
public class ClassB : ClassA
{
public ClassB(string code) : base(code)
{
//blah blah
}
public void DomeSomethingWithCountry()
{
Console.WriteLine($"doing this with {country} in classB");
}
}
public class ClassC : ClassA
{
public ClassC(string code) : base(code)
{
//blah blah
}
public void …Run Code Online (Sandbox Code Playgroud) 我正在查看http://en.cppreference.com/w/cpp/utility/variant/visit上的文章std::variant
该示例基本上包含以下几行(由我轻轻修改):
template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
auto a = overloaded {
[](auto arg) { std::cout << arg << ' '; },
[](double arg) { std::cout << std::fixed << arg << ' '; },
[](const std::string& arg) { std::cout << std::quoted(arg) << ' '; },
};
Run Code Online (Sandbox Code Playgroud)
代码基本上使用列表中的每个lambda函数作为struct的基类overloaded.第一行将lambda operator()引入结构的范围.第二行使用类模板参数推导指南(C++ 17).
题
我不明白第3行{ }后使用括号overloaded.
这里有什么样的C++机制?我们是否使用初始化列表并将其转换为可变参数模板参数,或者它是一种统一/聚合初始化?是否在这一行中调用了任何实际的构造函数?
有趣的是,如果我使用,施工将失败( ).
c++ lambda multiple-inheritance template-argument-deduction c++17
我正在尝试学习和理解如何在Python中使用super,我一直在遵循《从新手到专家的Python历程》一书,尽管我觉得我理解概念“在我自己的代码中执行super存在问题”。
例如,此方法对我有用:
class Employee:
def __init__(self, firstname, lastname, age, sex, dob):
self.firstname = firstname
self.lastname = lastname
self.age = age
self.sex = sex
self.dob = dob
self.all_staff.append(self)
class Hourly(Employee):
def __init__(self, firstname, lastname, age, sex, dob, rate, hours):
self.rate = rate
self.hours = hours
super().__init__(firstname, lastname, age, sex, dob)
def __str__(self):
return "{} {}\nAge: {}\nSex: {}\nDOB: {}\n".format(self.firstname, self.lastname, self.age,
self.sex, self.dob)
def get_rate(self):
print('The hourly rate of {} is {} '.format(self.firstname, self.rate))
hourlystaff1 = Hourly('Bob', 'Foo', '23', 'M', '12/1/1980', '$15', …Run Code Online (Sandbox Code Playgroud)