相关疑难解决方法(0)

如果我要覆盖它,我可以调用基类的虚函数吗?

假设我有课程FooBar设置如下:

class Foo
{
public:
    int x;

    virtual void printStuff()
    {
        std::cout << x << std::endl;
    }
};

class Bar : public Foo
{
public:
    int y;

    void printStuff()
    {
        // I would like to call Foo.printStuff() here...
        std::cout << y << std::endl;
    }
};
Run Code Online (Sandbox Code Playgroud)

正如在代码中注释的那样,我希望能够调用我所覆盖的基类函数.在Java中有super.funcname()语法.这在C++中是否可行?

c++ overriding virtual-functions

315
推荐指数
5
解决办法
22万
查看次数

使用C++基类构造函数?

在使用模板时,我遇到了一个需要,可以从继承的类访问基类构造函数来创建对象,以减少复制/粘贴操作.我想通过using关键字以与功能案例相同的方式做到这一点,但这不起作用.

class A
{
public: 
    A(int val) {}
};

class B : public A
{
};

class C : public A
{
public:
    C(const string &val) {}
};

class D : public A
{
public:
    D(const string &val) {}
    using A::A;              // g++ error: A::A names constructor
};

void main()
{
    B b(10);                // Ok.   (A::A constructor is not overlapped)
    C c(10);                // error: no matching function to call to 'C::C(int)'
}
Run Code Online (Sandbox Code Playgroud)

所以我的问题是:在声明了继承类中的新构造函数后,有没有办法导入基类构造函数?

或者只有一个替代方法来声明新构造函数并从初始化列表中调用基础构造函数?

c++ inheritance constructor c++11

50
推荐指数
3
解决办法
9万
查看次数

你可以在VB中继承带参数的子新(构造函数)吗?

在下面的代码中,我收到编译错误

Error Too many arguments to 'Public Sub New()'
Run Code Online (Sandbox Code Playgroud)

在...上Dim TestChild As ChildClass = New ChildClass("c").TestChild.Method1()即使它们都是我继承的基类,我也没有收到它.

Public Class BaseClass
    Public ReadOnly Text As String
    Public Sub New(ByVal SetText As String)
        Text = SetText
    End Sub
    Public Sub New()
        Text = ""
    End Sub
End Class

Public Class ChildClass
    Inherits BaseClass
End Class

Public Class TestClass
    Sub Test()
        Dim TestChild As ChildClass = New ChildClass("c")
        TestChild.Method1()
    End Sub
End Class
Run Code Online (Sandbox Code Playgroud)

我可以将子类更改为:

Public Class ChildClass
    Inherits BaseClass
      Public …
Run Code Online (Sandbox Code Playgroud)

vb.net inheritance constructor new-operator

41
推荐指数
2
解决办法
4万
查看次数

从派生类构造函数中调用基类构造函数

我有个问题:

说我原来这些我无法改变的课程(让我们说它们来自我正在使用的图书馆):

class Animal_
{
public:
    Animal_();
    int getIdA()
    {
        return idA;
    };
    string getNameA()
    {
        return nameA;
    }
private:
    string nameA;
    int idA;
}

class Farm
{
public :
    Farm()
    {
        sizeF=0;
    }
    Animal_* getAnimal_(int i)
    {
        return animals_[i];
    }
    void addAnimal_(Animal_* newAnimal)
    {
        animals_[sizeF]=newAnimal;
        sizeF++;
    }

private:
    int sizeF;
    Animal_* animals_[max];
}
Run Code Online (Sandbox Code Playgroud)

但后来我需要一个我只添加几个字段的类,所以我这样做了:

class PetStore : public Farm
{
public :
    PetStore()
    {
     idF=0;
    };
private:
    int idF;
    string nameF;
}
Run Code Online (Sandbox Code Playgroud)

但我无法初始化我的派生类,我的意思是我做了这个继承,所以我可以添加动物到我的PetStore但现在因为sizeF是私有的我怎么能这样做?我想也许在PetStore默认构造函数中我可以调用Farm()...所以任何想法?

c++ inheritance constructor visibility derived-class

27
推荐指数
3
解决办法
13万
查看次数

在C++中的其他一些指令之后调用基类的构造函数

据我所知,不可能调用基类的构造函数.我知道的唯一方法是:

MyClass::MyClass(/* args */) : Base(/* args */)
{
   // ...
}
Run Code Online (Sandbox Code Playgroud)

但这会在开始时调用构造函数.有没有办法在构造函数中的其他地方调用它?像这样的东西:

MyClass::MyClass(/* args */)
{
   // ... instructions
   Base::Base(/* args */);
   // ... other_instructions
}
Run Code Online (Sandbox Code Playgroud)

根据这个调用超类构造函数的规则是什么?问题我明白没有办法,但我在这里阅读,我猜这是可能的,但如果我尝试我得到:

error: invalid use of 'class Base'.
Run Code Online (Sandbox Code Playgroud)

难道我做错了什么?有可能以某种方式做到这一点还是有其他可能的解决方案来满足这种需求?

谢谢!

编辑:我理解我忘记了一个关键点:基类是框架的一部分,因此如果可能的话,最好不要修改它.

c++ oop class

19
推荐指数
3
解决办法
9080
查看次数

Python和C++构造函数之间的差异

我最近一直在学习更多关于Python的知识,并且当我通过优秀的Dive进入Python时,作者在这里指出该__init__方法在技术上不是构造函数,即使它通常像一个函数.

我有两个问题:

  1. C++如何构造对象和Python如何"构造"对象之间有什么区别?

  2. 什么使构造函数成为构造函数,以及该__init__方法如何不符合此条件?

c++ python constructor

14
推荐指数
1
解决办法
1764
查看次数

有没有办法将QWidget添加到QtCreator中的QMenu

我创建一个文本编辑器,我想放QComboBoxQMenu.我没有在QMenu处理这样的事情中找到任何方法.最接近的是QMenu::addAction().我想知道绕过这个障碍.

谢谢!

c++ qt qwidget qmenu

10
推荐指数
1
解决办法
7925
查看次数

从子类的构造函数体调用基类的构造函数

我觉得这是不可能的,例如: 在C++中的其他一些指令之后调用基类的构造函数
但是下面的程序运行并生成两行"Constructor Person":

#include <iostream>

class Person
{
public:
    Person() 
    { 
        std::cout << "Constructor Person" << std::endl; }
    };

class Child : public Person
{
public:
    Child() 
    { 
        c = 1; 
        Person(); 
    }
    int c;
};

int main() 
{
    Child child;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

第一个是默认构造函数的隐式调用,这是明确的.第二个怎么样 - 这是否意味着标题中描述的行为是合法的?我使用Visual C++ 2010.

c++ constructor base-class

9
推荐指数
3
解决办法
2万
查看次数

C++继承:在标头中调用基类构造函数

假设class Child是类的派生类Parent.在五个文件程序中,我将如何指定Child.h我想要调用构造函数Parent?我不认为以下内容在标题内是合法的:

Child(int Param, int ParamTwo) : Parent(Param);
Run Code Online (Sandbox Code Playgroud)

在这种情况下,Child.cpp构造函数的语法应该是什么样的?

c++ syntax inheritance inline initializer-list

7
推荐指数
2
解决办法
8488
查看次数

构造函数和析构函数继承

我相信,ConstructorsDestructorsbase class不能被继承derived classes的基类的.我的理解是否正确.

c++ inheritance base-class derived-class

6
推荐指数
1
解决办法
9613
查看次数