我試圖將一個型別作為引數傳遞給一個方法,該方法將正確構造并推送一個物件到unique_ptr的向量中,然而創建的物件始終是Base物件。這發生在使用emplace_back()時,如果我只是實體化物件,同樣的作業也很好。
在向量外構建物件可以正常作業,但是我不確定如何將指標移到向量之后。
body_parts.hpp
#include <vector>
#include <string>
#include <fmt/core.h>/span>
using namespace std;
namespace uhcr {
classbody_part
{
public:
string name = "Generic";
template <class T>
void add_body_part()
{
this->body_parts.emplace_back<T*>(new T() )。
fmt::print("{}.
", this->body_parts.back()->name)。)
}
private:
vector<unique_ptr<body_part>>;body_parts。
};
class torso : public body_part
{
public:
string name = "Torso";
};
}
character.hpp
#include <string>
#include " components/body_parts.hpp"
使用 命名空間 std.com.cn>。
namespace uhcr {
class character : public body_part
{
public:
string name = "Character";
};
}
main.cpp
#define FMT_HEADER_ONLY
#include <memory>
#include <fmt/core.h>/span>
#include "src/character.hpp"
使用 namespace fmt。
using namespace uhcr;
void create_human() {
字符human。
human.add_body_part<torso>()。
}
int main(void) {
create_human()。
return 1。
錯誤發生在add_body_part(),當運行這段代碼時,列印出 "Generic"。
uj5u.com熱心網友回復:
你在你的子類中有多個名為name的資料成員。你可能想給body_part中的成員賦值,而不是宣告影射它的新成員。
class body_part
{
public:
body_part() = default;
string name = "Generic";
template <class T>
void add_body_part()
{
this->body_parts.emplace_back<T*>(new T() )。
fmt::print("{}.
", this->body_parts.back()->name)。)
}
protected:
body_part(std::string name) : name(name) {}.
private:
vector<unique_ptr<body_part>> body_parts。
};
class torso : public body_part
{
public:
torso() : body_part("Torso") {}.
};
class character : public body_part
{
public:
character() : body_part("Character") {}.
};
uj5u.com熱心網友回復:
你的代碼中沒有切分。你似乎期待著虛擬成員變數,但并沒有這樣的東西。當你使用虛擬方法時,你會得到預期的輸出:
using namespace std;
class body_part
{
public:
virtual std::
template <class T>
void add_body_part()
{
this->body_parts.emplace_back<T*>(new T() 。)
std::cout << this->body_parts.back()->getName()。
}
virtual ~body_part() = default;
private:
vector<unique_ptr<body_part>> body_parts。
};
class torso : public body_part
{
public:
std::
};
class character : public body_part
{
public:
std::
};
void create_human(){
字符human。
human.add_body_part<torso>()。
}
int main(void) {
create_human()。
return 1。
這是個更簡單的例子,同樣的效果:
這是個更簡單的例子。
#include <iostream>
#include <string>
結構 foo {
std::string name = "Generic";
void x(foo& f){
std::cout << f.name;
}
virtual ~foo() = default;
};
struct bar : foo {
std::string name = "Beneric";
};
int main () {
foo f;
bar b;
f.x(b)。
}
bar有兩個name成員。在foo中,當你寫std::cout << f.name時,它指的是foo::name而不是bar::name。你可以在foo中訪問bar::name,只要foo確實是一個bar,但這將導致一些反向的設計:
#include <iostream>
#include <string>
結構 foo {
std::string name = "Generic";
void x(foo& f);
virtual ~foo() = default;
};
struct bar : foo {
std::string name = "Beneric"/span>;
};
void foo::x(foo& f){
std::cout << dynamic_cast<bar&> (f).name;
}
int main () {
foo f;
bar b;
f.x(b)。
}
在你的例子中,由于你剛剛創建了一個T,并且你知道它是一個T,所以dynamic_cast將是一個較小的問題,但盡管如此,你應該使用虛擬方法而不是依賴casts。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/332991.html
標籤:
上一篇:用python決議xml檔案
