相关疑难解决方法(0)

继承与析构 - 理论问题 - C++

class A
{
    public:
         virtual void f(){ printf("A.f "); }
         ~A(){ f(); }
};

class B : public A
{
    A a;

    public:
         void f(){ printf("B.f "); }
         B(){ throw -1; }
        ~B(){ f(); }
};

int main()
{
    try{ B b; }
    catch(...){ printf("Exc");}
}
Run Code Online (Sandbox Code Playgroud)

所以这就是我看到它的方式.在try块内部,构建时没有任何东西被打印出来B b;.块结束.我认为编译器首先破坏了A a;成员.所以A.f()会被打印出来.这是否意味着class B实例的破坏已经完成?之后,编译器会简单地调用~A()(破坏基类)吗?

我认为我应该得到A.f(),然后B.f()(破坏B类实例)然后A.f()再次(基类的析构函数).编译这个让我想一点.当然,正在打印Exc.我经历了几个主题,但没有找到任何东西.

编辑:Dev-C++(GCC 3.4.2)的输出是

Af Af Exc

c++ inheritance destructor exception

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

是否可以从抽象类构造函数中调用虚方法?

类图

目前,我有每个孩子的构造函数中的图中所示的"大量代码".我的目标是将其移动到父的构造函数.

c++ oop

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

虚函数调用,parrent的函数使用

关于StackOverflow的第一个问题.我要为我糟糕的英语道歉.

我实际上正在为视频游戏开发一个菜单.我想清除代码,所以我想用vector来调用不同的函数(比如为图片充电或修改文本)

这是代码:

Menu.hh:

class Menu
{
int _position
std::vector<Menu> _graph;
std::list<sf::Text> _text;
public:
//constructors and destructors
virtual std::list<sf::Text> &modifyText();
}
Run Code Online (Sandbox Code Playgroud)

Menu.cpp

std::list<sf::Text> &modifyText()
{
std::cout << this->_position << std::endl;
this->_text = this->_graph[this->_position].modifyText();
return (this->_text); // don't care of the return
}

void Menu::initGame()
{
this->_graph.push_back(MenuBase()); 
// if i Put this code on the constructor, the constructor call himself and not the MenuBase constructor
this->modifyText();
}
Run Code Online (Sandbox Code Playgroud)

MenuBase.hpp

class MenuBase : public Menu
{
//constructor and destructor
std::list<sf::Text &modifyText(){std::cout << "work" …
Run Code Online (Sandbox Code Playgroud)

c++ virtual function sfml

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

从子构造函数/析构函数调用纯虚函数

class A{
virtual void setEnable(bool enable) = 0;
};

class B : A{
    B() {
        setEnable(true);
    }
    ~B() {
        setEnable(false);
    }
    bool enable_ = false;
    
void setEnable(bool enable) override {
    enable_ = enable;
}
};
Run Code Online (Sandbox Code Playgroud)

我是否正确理解 B :: setEnable 函数仅在构造函数退出后才会添加到 vtable 并且这是未定义的行为?

c++

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

C++纯虚函数调用不会抛出运行时异常?

据说在C++的构造函数中,只要对象还没有构造完成,就不应该调用虚函数,否则会抛出“纯虚函数调用错误”。所以我尝试了这个:

#include<stdio.h>
class A{
    virtual void f() = 0;
};

class A1 : public A{
public:
    void f(){printf("virtual function");}
    A1(){f();}
};
int main(int argc, char const *argv[]){
    A1 a;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在 Windows 上用 g++ 编译它,它可以工作并打印

virtual function
Run Code Online (Sandbox Code Playgroud)

那么如何让我的程序抛出“纯虚函数调用”异常呢?

谢谢!

c++ virtual function call pure-virtual

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

大多数派生类的析构函数中的纯虚拟调用

我知道你不应该在基类的ctor或dtor中调用任何虚函数,但是从派生类最多的那个呢?应该没事吧?例如

class base {
    ...
    virtual void free() = 0;
};
class child : public base {
    ...
    free() {/* free memory */}
    ~child() {free();}
};
Run Code Online (Sandbox Code Playgroud)

c++ virtual-functions

0
推荐指数
1
解决办法
271
查看次数

vtable 何时创建/填充?

我很难弄清楚为什么我的代码不起作用。我将问题追溯到 vtable 生成/填充。

这是我的代码的简化版本:

#include <iostream>
#include <functional>
#include <thread>

using namespace std;

typedef std::function<void (void)> MyCallback;

MyCallback clb = NULL;

void callCallbackFromThread(void){
    while(clb == NULL);
    clb();
}

int someLongTask(void){
    volatile unsigned int i = 0;
    cout << "Start someLongTask" << endl;
    while(i < 100000000)
        i++;
    cout << "End someLongTask" << endl;
    return i;
}

class Base{
public:
    Base(){
        clb = std::bind(&Base::_methodToCall, this);
        cout << "Base-Constructor" << endl;
    }
protected:
    virtual void methodToCall(void){
        cout << "Base methodToCall got called!" << …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance multithreading vtable

0
推荐指数
1
解决办法
131
查看次数

C++ 从父级调用子函数

我试图从父级调用一个覆盖函数,发现它只是崩溃了。

这很难描述,所以这里是最小的可重现代码:

#include <iostream>

class A
{
public:
    A()
    {
        init1();
    }
    
    void init1()
    {
        printf("1");
        init2();
        printf("2");
    }

    virtual void init2() = 0;
};

class B : public A
{
public:
    B()
    : A()
    {}

    void init2() override
    {
        printf("hello");
    }
};

int main()
{
    B b;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在 MSVC 2019 上它崩溃了,在http://cpp.sh/ 上它输出“1”然后main()返回 0,但我们从未看到"hello""2"

  1. 为什么会崩溃?从低层次的角度来看会发生什么?是A试图调用它自己的init2()吗?

  2. 有没有办法做到这一点,所以我不必在每个派生类中添加init2()其构造函数?

c++ class

-3
推荐指数
1
解决办法
62
查看次数