需要地图帮助(c++,STL)

发布于 2024-09-01 20:11:36 字数 1526 浏览 1 评论 0原文

实际上我是 C++ 新手。我尝试了一些东西(实际上是地图容器),但它并不像我想象的那样工作......在发布我的代码之前,我将简短地解释它。

我创建了3个类:

ClassA 派生类A ClassAnotherDerivedA

最后两个是从“ClassA”派生的。

此外,我创建了一个地图:

  map<string,ClassA> test_map;

我将一些对象(来自 Type ClassDerivedA 和 ClassAnotherDerivedA)放入地图中。请记住:映射值来自类型“ClassA”。这只有在多态性的情况下才有效。最后,我创建了一个迭代器,它在我的地图上运行,并将用户输入与地图中的键进行比较。如果它们匹配,它将调用一个名为“printOutput”的特定方法。

还有一个问题: 虽然我将“printOutput”声明为“虚拟”,但调用的唯一方法是我的基类中的方法,但为什么呢? 这是代码:

#include <iostream>
#include <map>

using namespace std;

class ClassA
{
    public:
        virtual void printOutput() { cout << "ClassA" << endl;      }
};

class ClassDerivedA : public ClassA
{
    public:
        void printOutput() { cout << "ClassDerivedA" << endl;       }
};

class ClassAnotherDerivedA: public ClassA
{
    public:
        void printOutput() { cout << "ClassAnotherDerivedA" << endl;        }
};

int main()
{
    ClassDerivedA class_derived_a;
    ClassAnotherDerivedA class_another_a;


  map<string,ClassA> test_map;
    test_map.insert(pair<string,ClassA>("deriveda", class_derived_a));
    test_map.insert(pair<string,ClassA>("anothera", class_another_a));

    string s;

    while( cin >> s )
    {
    if( s != "quit" )
    {
        map<string,ClassA>::iterator it = test_map.find(s);
      if(it != test_map.end())
        it->second.printOutput();
    }
    else
      break;
    }

}

Actually I'm new to C++. I tried something out (actually the map container) but it doesn't work the way I assumed it will... Before posting my code, I will explain it shortly.

I created 3 classes:

ClassA
ClassDerivedA
ClassAnotherDerivedA

The two last ones are derived from "ClassA".

Further I created a map:

  map<string,ClassA> test_map;

I put some objects (from Type ClassDerivedA and ClassAnotherDerivedA) into the map. Keep in mind: the mapped value is from type "ClassA". This will only work because of Polymorphism. Finally I created an iterator which runs over my map and compares the user input with my keys in the map. If they match, it will call a specific method called "printOutput".

And there is the Problem:
Although i declared "printOutput" as "virtual" the only method called is the one from my base class, but why?
and here is the code:

#include <iostream>
#include <map>

using namespace std;

class ClassA
{
    public:
        virtual void printOutput() { cout << "ClassA" << endl;      }
};

class ClassDerivedA : public ClassA
{
    public:
        void printOutput() { cout << "ClassDerivedA" << endl;       }
};

class ClassAnotherDerivedA: public ClassA
{
    public:
        void printOutput() { cout << "ClassAnotherDerivedA" << endl;        }
};

int main()
{
    ClassDerivedA class_derived_a;
    ClassAnotherDerivedA class_another_a;


  map<string,ClassA> test_map;
    test_map.insert(pair<string,ClassA>("deriveda", class_derived_a));
    test_map.insert(pair<string,ClassA>("anothera", class_another_a));

    string s;

    while( cin >> s )
    {
    if( s != "quit" )
    {
        map<string,ClassA>::iterator it = test_map.find(s);
      if(it != test_map.end())
        it->second.printOutput();
    }
    else
      break;
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

挽袖吟 2024-09-08 20:11:36

问题是切片。您正在地图中存储 ClassA 值。当您将派生类实例存储到映射中时,该实例会被切片为 ClassA 对象。您需要在映射中存储指针而不是值。

有关切片的更多信息,请参阅此内容:什么是对象切片?

The problem is slicing. You are storing ClassA values in your map. When you store derived class instances into the map, the get sliced into ClassA objects. You'll need to store pointers in your map instead of values.

See this for more info on slicing: What is object slicing?

后eg是否自 2024-09-08 20:11:36

C++ 不是 Java。不能将派生类型存储在基类型的变量中。例如:

Base b = Derived();

只会将 Derived 的 Base 部分存储在变量 b 中。为了获得多态行为,您需要使用指针,并动态创建派生类:

Base * b = new Derived();

C++ 容器也是如此 - 您需要:

map <string, Base *> m;

所有这些内容都应该包含在每一本介绍性 C++ 教科书中 - 您使用的是哪一本?

C++ is not Java. You cannot store a derived type in a variable of a base type. For example:

Base b = Derived();

will only store the Base part of Derived in the variable b. In order to get polymorphic behaviour, you would need to use pointers, and create the derived class dynamically:

Base * b = new Derived();

The same goes for C++ containers - you need:

map <string, Base *> m;

All of this should be covered in every introductory C++ text book - which one are you using?

影子是时光的心 2024-09-08 20:11:36

您正在经历“切片”。为了使虚拟函数正常工作,您需要使用指针或引用来调用它们。换句话说,您的 map 应包含指向 ClassA 的指针:

maptest_map

请记住在完成后删除它们,或者使用智能指针。

以下是有关切片的更多信息:此处此处,以及此处

You are experiencing "slicing". To get the virtual functions to work properly, you need to call them using a pointer or a reference. In other words, your map should contain pointers to ClassA:

map<string, ClassA *> test_map

Please remember to delete them when you are done, or use smart pointers.

Here's more on slicing: here, here, and here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文