- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个名为 base 的类。这个类是不可访问的(我的意思是源代码),因为它是第 3 方类。这个类有一个方法,我想从(父)姐妹(比如说叔叔)类访问。
class base {
public:
void do_something() {
std::cout << "base1::do_something" << std::endl;
}
};
我有另一个父类,它是使用 crtp 创建的访问子类的成员:
template<typename crtpT>
class crtp {
public:
void foo() {
std::cout << "crtp::foo" << std::endl;
static_cast<crtpT*>(this)->bar();
//static_cast<crtpT::base_type*>(this)->do_something();
// how do i access base::bar
}
};
最后,继承自两者的子类:
template<typename baseT>
class child : public baseT, public crtp<child<baseT> > {
public:
typedef baseT base_type;
void bar() {
std::cout << "child::bar" << std::endl;
}
};
主要.cpp:
int main(int argc, char* argv[]) {
child<base> ch;
ch.do_something();
ch.foo();
ch.bar();
return 0;
}
现在我的问题是,我需要从我的 crtp 类访问 do_something
。这甚至可能吗?
static_cast 将不起作用。我认为在生成 crtp 类时,编译器对基类一无所知。 crtp 类必须是类子类的父类,因为很多其他类都派生自它。否则我可能会使用 delegation to sister-class .
我可以在我的 child 体内做第二个 bar 方法,但这不是很好,因为我必须为 base 类中的所有方法编写包装器。
template<typename baseT>
class child : public baseT, public crtp<child<baseT> > {
public:
typedef baseT base_type;
void bar() {
std::cout << "child::bar" << std::endl;
}
void bar2() {
base_type::do_something();
}
};
并调用:
static_cast<crtpT*>(this)->bar2();
最佳答案
static_cast
: [expr.static.cast]/11
A prvalue of type “pointer to cv1 B,” where B is a class type, can be converted to a prvalue of type “pointer to cv2 D,” where D is a class derived from B [...]
因此,您需要从crtp<...>*
进行转换给 child 类child*
然后你甚至可以使用隐式转换为 base
,例如
crtpT::base_type* p = static_cast<crtpT*>(this);
关于c++ - crtp父访问姐妹类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17772552/
我有一个模型 FollowUp,其中包含与用户和项目关联的日期和描述: class FollowUp(Base): __tablename__ = 'followup' id =
这个问题在这里已经有了答案: Is there a "previous sibling" selector? (30 个答案) 关闭 6 年前。 我在一个 div 中有两个图像。当用户将鼠标悬停在第
我有一个控件,它是父控件的第一个子控件,但在父控件和控件中没有什么是唯一的。我可以找到第二个 child 的独特属性,所以我需要找到第二个 child 的前一个 sibling 最佳答案 这可以通过使
我有一个表单,我希望能够添加和删除文本框和标签。每个文本框前面都有一个标签,因此标签是同级标签。我正在尝试只选择文本框的兄弟,所以当文本框被删除时我也将其删除。 我的问题是如何选择上一个标签。删除文本
这个问题在这里已经有了答案: Is there a "previous sibling" selector? (32 个答案) 关闭 7 年前。 我有两个元素: ... ... 当 prayers_
这个问题在这里已经有了答案: Is there a "previous sibling" selector? (29 个答案) 关闭 8 年前。 我有一个列表,想选择我悬停的列表中的上一个和下一个
HTML: First Name JS: var FNAME = document.getElementById('FNAME'); FNAME.addEventListener('input',
如果我有一长串元素,当单击一个元素时,如何获取所有同级元素,直到到达类名包含给定字符串的元素? 示例: test test test test test test test test 在上面的例子中,
我试图在悬停时在 DIV 上获得 Accordion 效果。 Accordion 的右侧已经开始工作,但左侧还没有。 我把我的代码放在jsFiddle 有人可以帮我解决一下左边吗?我已经尝试了几个小时
我的 React 应用正在创建一个步骤列表: let steps = this.props.formSteps.map((step, i) => { let stepNum =
这个问题在这里已经有了答案: Is there a "previous sibling" selector? (33 个答案) 关闭 3 年前。
我有这样的结构: 和这个 scss: .as-twl-list-item { border-bottom: 1px solid
我是一名优秀的程序员,十分优秀!