在下面的代码中,由于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) 我一直在用这种方式初始化我的一个结构而没有任何问题很长一段时间:
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++:
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) 我有一个类层次结构,如下所示:
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) 这显然是我自学计算机科学教育的一个漏洞......
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) 我有一个抽象类,例如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) 我有这个代码的问题 - 这是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) 到目前为止,我做了以下工作:
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)
转换为基类的类型不起作用。我做错了什么。
有没有办法使覆盖有效并确保按值复制对象。
#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++中使用多态时,了解它的重要事实是什么.
比如,例如,从何时derived开始base,应该注意什么?
什么时候可以上线,什么时候不可以?你什么时候需要一个'虚拟'析构函数?什么时候不需要?
使用从base到派生对象的指针时需要注意什么?
有一个在C++休息室讨论刚才关于sizeof(*this)同态的类型,这启发了我问这个问题.
像这样一个"模糊"的事实:
"如果你有一个隐藏在Base&后面的Derived,那么静态类型是Base,而动态类型是Derived."
在这个问题上,也是我正在寻找的东西.
c++ ×10
c++11 ×3
inheritance ×2
polymorphism ×2
abstract ×1
class ×1
gcc ×1
static-cast ×1
struct ×1
virtual ×1
wxwidgets ×1