相关疑难解决方法(0)

c ++中的Static_cast和虚方法

在下面的代码中,由于name()是虚拟的,我希望将调用派生结构的方法.相反,什么是写出来的是"A".为什么?

#include <iostream>
using namespace std;
struct A {
    virtual string name() { return "A"; }
};
struct B : A {
    string name() { return "B"; }
};
int main (int argc, char *argv[]) {
    B b;
    cout << static_cast<A>(b).name() << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ virtual static-cast

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

添加继承后,为什么我的副本列表初始化停止工作?

我一直在用这种方式初始化我的一个结构而没有任何问题很长一段时间:

struct MyStruct
{
    float firstArr[4];
    float secondArr[4];
    float thirdArr[4];
    float fourthArr[4];
};

void Function()
{
    std::vector<MyStruct> myArr;

    myArr.push_back // This works fine
    ({
        { 0, 0, 0, 0 },
        { 0, 0, 0, 0 },
        { 0, 0, 0, 0 },
        { 0, 0, 0, 0 }
    });
}
Run Code Online (Sandbox Code Playgroud)

当我开始从Parent派生我的结构时,我不再能够这样做了.

struct Parent
{
    // Nothing
};

struct MyStruct: Parent
{
    float firstArr[4];
    float secondArr[4];
    float thirdArr[4];
    float fourthArr[4];
};

void Function()
{
    std::vector<MyStruct> myArr;

    myArr.push_back // This gets compiler …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance struct initializer-list

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

C++:切片到rvalue基础?

我遇到了以下代码,我在谷歌上找不到为什么以下语句是有效的C++:

Base&& b = Derived();
Run Code Online (Sandbox Code Playgroud)

请解释或参考

这是一个示例代码:

#include <iostream>
using namespace std;

class Base{
public:
    virtual ~Base(){}
    virtual void say_hi() { cout << "hi base"; }
};
class Derived : public Base{
public:
    virtual ~Derived(){}
    virtual void say_hi() { cout << "hi derived"; }
};

int main(int argc, const char * argv[]) {
    Base&& b = Derived();
    b.say_hi();

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

印刷品:

hi derived
Run Code Online (Sandbox Code Playgroud)

c++ rvalue-reference c++11

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

C++类设计并按值返回

我有一个类层次结构,如下所示:

class Address {
public:
    virtual ~Address() {}

    enum Type {
        Internal, External
    };

    Type type() const { return _type; }

protected:
    Address(Type type) : _type(type) {}

private:
    Type _type;
};

class InternalAddress : public Address {
public:
    InternalAddress(const string& portName) : Address(Internal), _portName(portName) {}
    ~InternalAddress() {}

    const string& portName() const { return _portName; }
private:
    string _portName;
};

class ExternalAddress : public Address {
public:
    ExternalAddress(const string& handlerName) : Address(External), _handlerName(handlerName) {}
    ~ExternalAddress() {}

    const string& handlerName() const { …
Run Code Online (Sandbox Code Playgroud)

c++

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

在函数调用中创建的对象与传入的对象之间有什么区别

这显然是我自学计算机科学教育的一个漏洞......

wxWidgets应用程序中文本控件(wxTextCtrl)的构造函数具有验证器对象的可选参数.所有代码示例都在文本控件的构造函数中动态创建验证器.

这有效..

wxString value = L"0.0";
wxTextCtrl* _Text = new wxTextCtrl(this, wxID_ANY, value, 
    wxDefaultPosition, wxDefaultSize, 0, 
    wxTextValidator(wxFILTER_NUMERIC, &value));
Run Code Online (Sandbox Code Playgroud)

但是在我的特殊情况下,我想在另一个函数中创建验证器并将其传回,这是行不通的.作为一个中间步骤,我试图在创建wxTextCtrl之前创建它并将其传递但是这也不起作用......

wxString value = L"0.0";
wxValidator valid = wxTextValidator(wxFILTER_NUMERIC, &value);
wxTextCtrl* _Text = new wxTextCtrl(this, wxID_ANY, value, 
    wxDefaultPosition, wxDefaultSize, 0, valid);
Run Code Online (Sandbox Code Playgroud)

虽然这编译并运行但它不执行验证.有谁能解释为什么?

wxTextValidator的原型调用常量引用.

wxTextCtrl::wxTextCtrl  (   wxWindow *  parent,
    wxWindowID  id,
    const wxString &    value = wxEmptyString,
    const wxPoint &     pos = wxDefaultPosition,
    const wxSize &  size = wxDefaultSize,
    long    style = 0,
    const wxValidator &     validator = wxDefaultValidator,
    const wxString & …
Run Code Online (Sandbox Code Playgroud)

c++ wxwidgets c++11

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

C++ 使用纯虚函数实例化抽象类的子类

我有一个抽象类,例如Animal。Animal 有一个纯虚函数eat,如果每个动物不想挨饿,就必须实现它。我确保只能通过这种方式实例化 Animal 的孩子:

动物.hpp

class Animal
{

public:

    enum eAnimal{
        CAT=0,
        DOG=1,
        BIRD=2
    };

    // Instantiates the desired animal.
    static Animal GetAnimal(const int animal);

    virtual void Eat() const = 0;

protected:

    Animal();
};
Run Code Online (Sandbox Code Playgroud)

动物.cpp

Animal Animal::GetAnimal(const int animal)
{
    switch(animal)
    {
    case CAT:
        return Cat();
    case DOG:
        return Dog();
    case BIRD:
        return Bird();
    default:
        cerr << "Animal not recognized." << endl;
        exit(-1);
    }
}

Animal::Animal()
{}
Run Code Online (Sandbox Code Playgroud)

有了这个,将是:

猫.hpp

class Cat : public Animal …
Run Code Online (Sandbox Code Playgroud)

c++ polymorphism abstract-class return-by-value

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

无法实例化抽象类c ++

我有这个代码的问题 - 这是taskData:

static std::map<int8_t, std::vector<Task>> taskData;
Run Code Online (Sandbox Code Playgroud)

并且有一个问题:

taskData.emplace(pi::enumerations::taskManager::taskCategory_t::SECURITY, std::vector<Task>{FirefightingTask()});
Run Code Online (Sandbox Code Playgroud)

FirefightingTask:

#pragma once

#include "Task.hpp"

namespace mc
{
    class FirefightingTask :public Task
    {
    public:

        FirefightingTask( uint8_t category = 0, uint8_t kind = 0, NPC* npc = nullptr );

        virtual bool act() override;
    };
}
Run Code Online (Sandbox Code Playgroud)

任务:

#pragma once

#include "engine/Config.hpp" 

#include <queue>

class NPC;

namespace mc
{
    //Task
    //Represents a task for AI object
    class Task
    {

    public:
        Task(uint8_t category = 0, uint8_t kind = 0, NPC* npc = nullptr ); …
Run Code Online (Sandbox Code Playgroud)

c++ class abstract

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

我重载了一个虚函数,但它不起作用

到目前为止,我做了以下工作:

class Stamp{
 public:
    virtual std::string GetStampName() const { return "undefined"; }
};
Run Code Online (Sandbox Code Playgroud)
class FooStamp : public Stamp {
 public:
    std::string GetStampName() const override { return "foo-stamp"; }
};
Run Code Online (Sandbox Code Playgroud)

我像这样使用它:

FooStamp fooStamp;
std::cout << "foo-stamp:" << fooStamp.GetStampName() << std::endl;

const Stamp stamp = fooStamp;
std::cout << "stamp:" << stamp.GetStampName() << std::endl;
Run Code Online (Sandbox Code Playgroud)

实际输出如下?

foo-stamp:foo-stamp
stamp:undefined     // expected: foo-stamp
Run Code Online (Sandbox Code Playgroud)

转换为基类的类型不起作用。我做错了什么。

有没有办法使覆盖有效并确保按值复制对象。

c++

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

为什么基类指针指向基类中的纯虚方法而不是派生类中的覆盖方法?

#include <iostream>

class A
{
    public:
        virtual ~A() = default;
        virtual void foo(void) = 0;
};

class B : public A
{
    private:
        int x;

    public:
        B(int a) : x(a) {}
        void foo(void) { std::cout << "B: " << x << "\n"; }
};

class Foo
{
    private:
        A* a_ptr;

    public:
        Foo (B& x) { a_ptr = &x; }
        A* get_ptr(void) { return a_ptr; }
        void dummy(void) { std::cout << "Foo: "; std::cout << a_ptr << "\t "<< typeid(*a_ptr).name() << …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance gcc run-time-polymorphism

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

关于多态性应该知道什么是C++的东西

在C++中使用多态时,了解它的重要事实是什么.

比如,例如,从何时derived开始base,应该注意什么?

什么时候可以上线,什么时候不可以?你什么时候需要一个'虚拟'析构函数?什么时候不需要?

使用从base到派生对象的指针时需要注意什么?

有一个在C++休息室讨论刚才关于sizeof(*this)同态的类型,这启发了我问这个问题.

像这样一个"模糊"的事实:

"如果你有一个隐藏在Base&后面的Derived,那么静态类型是Base,而动态类型是Derived."

在这个问题上,也是我正在寻找的东西.

c++ polymorphism c++11

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