我有两个班级A班和B班.我想B类拥有的功能A级加上它自己的一些更多的功能.执行此操作的一种非常简单的方法是从A类继承B 类.但出于某些具体实施原因,我不能这样做.
我想到了另一个解决方案,我将在其中创建另一个包含所有虚函数的类C.的类A和B将继承C类.的类A将包含虚拟功能和实现B类也可以定义一组相同的功能,因此B类将具有功能A类.这是最佳解决方案吗?除了从A 类直接继承B 类之外,还有比这更好的解决方案吗?
class Base{};
class D1:virtual public Base{};
class D2:virtual public Base{};
class DD:public D1,public D2{};
int main(){
Base *pBase=new DD;
delete pBase;
}
Run Code Online (Sandbox Code Playgroud)
这会导致崩溃,但我修改如下:
class Base{
public:
virtual ~Base(){};
};
class D1:virtual public Base{
public:
virtual ~D1(){}
};
class D2:virtual public Base{
public:
virtual ~D2(){}
};
class DD:public D1,public D2{
};
Run Code Online (Sandbox Code Playgroud)
然后,它就通过了,但是默认的析构函数应该是虚拟虚拟函数,不是吗?
c++ destructor virtual-inheritance delete-operator virtual-destructor
#include <iostream>
using namespace std;
class A {
int a;
};
class B1 : virtual public A {
int b1;
};
class B2 : virtual public A {
int b2;
};
class C : public B1, public B2 {
int c;
};
int main() {
A obj1; B1 obj2; B2 obj3; C obj4;
cout << sizeof(obj1) << endl;
cout << sizeof(obj2) << endl;
cout << sizeof(obj3) << endl;
cout << sizeof(obj4) << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
4
16
16 …Run Code Online (Sandbox Code Playgroud) 我目前正在开发一个C++项目,我有一个稍后实现的抽象接口.该接口还有一个实现的方法,我的实现不会覆盖.我的问题是,在使用我的实现时,编译器(MSVC)没有看到接口方法.是什么导致这种情况,我该如何解决?
这是代码.
#include <string>
#include <vector>
using std::string;
class A
{
public:
string name;
};
class interface
{
public:
virtual int num_foo() = 0;
virtual A* foo(int) = 0;
virtual A* foo(string &name){
for ( int i(0); i < num_foo(); i++)
if ( foo(i)->name == name )
return foo(i);
return 0;
}
};
class implementation : public interface
{
public:
virtual int num_foo() { return m_foos.size(); }
virtual A* foo(int i) {
//check range
return &m_foos[i];
}
std::vector<A> m_foos;
}; …Run Code Online (Sandbox Code Playgroud) 是否可以在c ++中从父函数调用子函数.
我们举个例子:父类在函数中定义(解析)一般工作流.然后,工作流调用表示流的一部分的不同方法(parseElementA).这些函数可以被子类覆盖,如果不是标准函数,则应使用父类的一部分.
我的问题是:我创建一个子对象并执行工作流功能(解析).当在工作流函数中调用覆盖函数(parseElementA)时,它从父级而不是从子级调用函数.我怎么办呢,它调用了孩子的覆盖功能.
class Parent {
public:
void parse() { parseElementA(); }
virtual void parseElementA() { printf("parent\n"); }
};
class Child : public Parent {
public:
void parseElementA() { printf("child\n"); }
};
Child child;
child.parse();
Run Code Online (Sandbox Code Playgroud)
输出是父.我能做什么让它回归孩子.
非常感谢您的任何建议.
我读到在虚拟继承中,构造函数被称为"从最派生的".请考虑以下代码.在我看来,这里派生最多的类是D.然后B和C和"最不得派"是A.那么为什么最先调用"Base"构造函数而不是"派生最多"?谢谢.
#include <iostream>
using namespace std;
struct A
{
A()
{
cout << "A default constructor" << endl;
}
};
struct B : virtual public A
{
B() : A()
{
cout << "B default constructor" << endl;
}
};
struct C : virtual public A
{
C() : A()
{
cout << "C default constructor" << endl;
}
};
struct D : public B, public C
{
D() : B(), C()
{
cout << "D default constructor" << endl; …Run Code Online (Sandbox Code Playgroud) 我知道钻石问题,但问题是 - 当我谷歌"虚拟继承"时,结果只提到钻石问题.我想知道它一般是如何工作的,它与正常继承有什么不同.
我知道当一个类(通常)继承自另一个类时,它只包含其所有成员(字段和方法,不考虑访问级别).其中一些可能会被新成员覆盖或隐藏,但他们仍然存在.继承还定义了层次结构中类之间的某些关系,这些关系会影响转换和多态.
现在虚拟继承有何不同?例如:
class A
{
public:
int a;
int b;
void fun(int x)
{}
void gun(int x)
{}
};
class B : public A
{
public:
int a;
int c;
void fun(int x)
{}
void hun(int x)
{}
};
class C : virtual public A
{
public:
int a;
int c;
void fun(int x)
{}
void hun(int x)
{}
};
Run Code Online (Sandbox Code Playgroud)
B和之间有什么区别C?我的例子没有利用其他任何差异吗?标准说什么?另外,如果C++ 03和C++ 11之间存在差异,请提及它.
class base
{
public:
virtual void display() = 0;
};
class derived : virtual public base
{
public:
void display()
{
cout << "Display of derived : " << std::endl;
}
};
class derived1 : virtual public base
{
public:
void display()
{
cout << "Display of derived : " << std::endl;
}
};
class derived2 : public derived, derived1
{
};
Run Code Online (Sandbox Code Playgroud)
我将一个纯虚函数放入基类中。我在创建从基类继承的派生类和派生类 1 时使用 virtual 关键字,最后创建了从派生类和派生类 1 继承的派生类 2,然后我会收到错误“派生类:base::void(display) 的继承不明确” “如何解决这个错误?
c++ overriding virtual-functions multiple-inheritance virtual-inheritance
我有以下课程:
class ServoPart {
protected:
virtual void doJob(byte* job) = 0;
private:
bool moving;
Servo servo;
};
// the following classes only have a constructor so I can use two ServoParts to inherit from
class Major: public ServoPart {};
class Minor: public ServoPart {};
class Arm: public Major, public Minor {
private:
void move() {
// do stuff
if (target == current) {
moving = false;
}
};
public:
void doJob(byte* job) {/* do stuff */};
};
Run Code Online (Sandbox Code Playgroud)
我不能使用虚拟继承(我认为),因为 Major 和 …
在下面的示例中,我有我的父类和两个子类。任何一个孩子的对象都存储在父母的向量中。循环遍历向量我只能看到来自父类的方法调用。
如何正确获得方法定义和 vtables 以及如何避免切片效应。我一直在做 Python 太长时间了,这样的事情会起作用。
#include <iostream>
#include <vector>
using namespace std;
class A{
public:
virtual string print(){return string("A");};
};
class B: public A{
virtual string print() final {return string("B");};
};
class C: public A{
virtual string print() final {return string("C");};
};
int main()
{
vector<A> v;
v.push_back(B());
v.push_back(C());
for(auto x : v){
cout << x.print() << endl;
}
}
Run Code Online (Sandbox Code Playgroud)
=>
$g++ -std=c++11 -o main *.cpp
$main
A
A
Run Code Online (Sandbox Code Playgroud) 我在在线测试中遇到了以下c ++代码.
#include <iostream>
class A
{
public:
A(int n = 2) : m_n(n) {}
public:
int get_n() const { return m_n; }
void set_n(int n) { m_n = n; }
private:
int m_n;
};
class B
{
public:
B(char c = 'a') : m_c(c) {}
public:
char get_c() const { return m_c; }
void set_c(char c) { m_c = c; }
private:
char m_c;
};
class C
: virtual public A
, public B
{ };
class D
: …Run Code Online (Sandbox Code Playgroud)