如何从基类中打印出派生类名,而不必将构造函数一直关闭.换句话说,是否可以从基类严格执行此操作而不在每个派生类中添加代码?
这是我得到的一个例子,如果有一种方法我想摆脱构造函数链接.
编辑: 理想情况下,我正在寻找添加到基类中的东西,而无需编辑所有派生类.目前我的真实代码已经有17个类(需要更多),所以可以直接从基类完成工作的东西是理想的.即使它是特定于编译器的(g ++或clang).
#include <iostream>
class Base {
public:
Base(std::string id) {
std::cout<<"Creating "<<id<<std::endl;
}
};
class Child : Base {
public:
Child(std::string id) : Base(id) {}
Child() : Base(typeid(this).name()) {}
};
class GrandChild : Child {
public:
GrandChild(std::string id) : Child(id) {}
GrandChild() : Child(typeid(this).name()) {}
};
class GrandGrandChild : GrandChild {
public:
GrandGrandChild(std::string id) : GrandChild(id) {}
GrandGrandChild() : GrandChild(typeid(this).name()) {}
};
int main() {
GrandGrandChild *A = new GrandGrandChild();
GrandChild *B = new GrandChild();
Child …
Run Code Online (Sandbox Code Playgroud) 我有一个大多是化妆品问题.我正在使用ggplot2库创建四个图,然后我将其排列在一列中(使用此列).图表显示相同的数据,但对于四组,x轴是时间,这就是我想将图形保持在一个列中的原因.
所以我将图例添加到顶部图形,将x轴的标签添加到底部图形.这两个动作改变了图形的大小; 添加图例会导致图形增长,添加x轴标签会使其缩小以适应这些事物.
有没有办法指定固定的图形大小,这将使我的布局一致?
我的情节:
可重现结果的代码:
library(ggplot2)
library(reshape)
raw_data <- structure(list(Sample = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L,
10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L,
23L, 24L, 25L, 26L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L,
24L, 25L, 26L), Month = structure(c(12L, 12L, 11L, 11L, 10L,
10L, 3L, 3L, 5L, 5L, 4L, 4L, …
Run Code Online (Sandbox Code Playgroud)