简体   繁体   English

C++ function 解析基 class 指向派生 class 指针的指针

[英]C++ function resolving base class pointer to derived class pointer

I tried implement something similar to JSON with pointers as a part of an exam question, and I ended up with an error that I didn't know how to solve.我尝试实现类似于JSON的东西,并将指针作为考试问题的一部分,但我最终遇到了一个我不知道如何解决的错误。

I had these classes (leaving out the implementation, because I don't think it is important for my error):我有这些类(省略了实现,因为我认为这对我的错误并不重要):

struct JSON_BaseObject;

template<typename J_Type>
struct JSON_Value : public JSON_BaseObject;

struct JSON_String : public JSON_Value<std::string>;
struct JSON_Number : public JSON_Value<double>;
struct JSON_Null : public JSON_BaseObject;

using JSON_Children = std::map<std::string, JSON_BaseObject*>;
using JSON_Object = JSON_Value<JSON_Children>;

And I wanted to use the children of type JSON_BaseObject* in JSON_Object as a parameter to functions that had different overloads for these derived types, so I made these functions and hoped that they would resolve JSON_BaseObject* to the respective class (pointer):我想在JSON_Object中使用JSON_BaseObject*类型的子代作为对这些派生类型具有不同重载的函数的参数,所以我制作了这些函数并希望它们将JSON_BaseObject*解析为相应的 class(指针):

void print(JSON_Null*, std::ostream& os);
void print(JSON_String* str, std::ostream& os);
void print(JSON_Number* number, std::ostream& os);
void print(JSON_Object* object, std::ostream& os);

When I ran it, I got a run-time error that the program wasn't able to find the function print(JSON_BaseObject*, std::ostream&) and I realized that I wasn't as good at c++ as I thought I was.当我运行它时,我得到一个运行时错误,程序无法找到 function print(JSON_BaseObject*, std::ostream&)我意识到我在 c++ 方面不如我想的那么好.

Question 1问题 1

Is it possible to resolve pointer type parameters from a baseclass like this somehow?是否有可能以某种方式从这样的基类解析指针类型参数?

Question 2问题2

How can I make it nice eg begin able to use print(obj, os);我怎样才能让它变得更好,例如开始能够使用print(obj, os); without knowing the derived class of obj ?不知道obj的派生 class 吗?

After trying out the visitor pattern I realized that it solves all my problems.在尝试了访问者模式后,我意识到它解决了我所有的问题。 It also resolves the base pointers in an easy way.它还以一种简单的方式解析基指针。

It enabled a nice code like this:它启用了一个很好的代码,如下所示:

int main()
{
    JSON_Object obj = JSON_Object();
    JSON_String str = JSON_String("I am a string!");

    std::vector<JSON_Base*> basePtrs
    {
        &obj,
        &str
    };

    Printer printer;

    for (auto json : basePtrs)
    {
        json->PrintMe(printer);
    }
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 指向C ++中派生类的基类指针 - the base class pointer to the derived class in c++ C ++将派生类的成员指针转换为基类指针 - c++ cast member pointer of derived Class to base class pointer c ++ - 将基类指针转换为派生类指针 - c++ - converting a base class pointer to a derived class pointer 在C ++中,派生类指针应指向基类指针吗? - In C++ should derived class pointer point to base class pointer? 在C++中将派生类指针分配给基类指针 - assigning derived class pointer to base class pointer in C++ 指向派生类对象的C ++基类指针不调用派生相等运算符函数 - C++ Base class pointer pointing to derived class object doesn't call derived equal operator function 在C ++中,是否允许将函数指针转换为一个函数指针,该函数指针作为参数获取指向基类或派生类的指针? - In C++, is it allowed to cast a function pointer to one that gets as a parameter a pointer to a base or derived class? C ++访问派生类与基类的指针 - c++ access derived class with a pointer from base class 如何在c ++中将指向基类的指针赋给派生类的对象? - How to assign a pointer to base class to an object of a derived class in c++? C ++-在运行时从基类指针识别派生类 - C++ - Identify derived class from base class pointer at runtime
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM