将抽象类对象从映射存储到抽象类引用

发布于 2024-12-18 01:48:57 字数 301 浏览 2 评论 0原文

我正在尝试实现以下方法来获取与其名称相对应的总线,该名称作为字符串传递给该方法。这里 AbstractBus 是一个包含总线功能的抽象类。 localBusses 是一个 std::map

void getLocalBusByName(string& name, AbstractBus& bus) 
{
    bus =  localBusses.find(name)->second;
}

但是,我收到错误“没有运算符”= 与这些操作数匹配” 谁能解释一下这里出了什么问题以及我该如何纠正它?

I am trying to implement the following method to get the bus corresponding to its name which is passed as a string to the method. Here AbstractBus is an abstract class which contains functionality of a bus. localBusses is a std::map

void getLocalBusByName(string& name, AbstractBus& bus) 
{
    bus =  localBusses.find(name)->second;
}

However, I am getting an error "no operator "=" matches these operands"
Can anyone explain what is wrong here and how can I correct it?

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

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

发布评论

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

评论(1

苹果你个爱泡泡 2024-12-25 01:48:57

该代码提出了几个问题

  1. 它不处理 localBusses.find(...) 找不到总线时的情况
  2. 您似乎想返回对找到的总线的引用。您应该记住,引用是别名,它们仅在声明时才能绑定,并且之后保持不可变。也就是说,您无法将引用重新绑定到另一个对象。
  3. 在 2 之后,bus = ... 尝试调用赋值运算符将找到的总线分配给引用所引用的总线。赋值运算符不存在或不可访问。这很好,因为在这种情况下有一个赋值运算符是一个坏主意,如果存在的话,如果 AbstractBus 类是可实例化的(非抽象的),它可能会导致切片。

编辑:我相信你要么有:

typedef std::map<string, AbstractBus*> tBussesMap;
tBussesMap localBusses;

要么 AbstractBus 并不是真正抽象的(不包含任何纯方法)(尽管它的名字)

在任何一种情况下我都假设你有从 AbstractBus 派生的类。 getLocalBusByName 必须应对找不到总线的情况并避免无意的切片。

因此,您应该在以下行中编写 getLocalBusByName 方法:

AbstractBus* 
getLocalBusByName( const string& name )
{
    tBussesMap::iterator it = localBusses.find(name);

    if ( it != localBusses.end() )
    {
        return it->second;
    }

    return nullptr;
}

其中 localBusses 被假定为类型:std::map (如果 AbstractBus 是抽象的,这是唯一可行的方法)

The code presents several problems

  1. It does not handle the case when localBusses.find(...) doesn#t find a bus
  2. You seem to want to return a reference to the found bus. You should remember that references are aliases and they can bind only when declared and remain immutable afterwards. That is you cannot re-bind a reference to another object
  3. Following 2, the bus = ... attempts to call the assignment operator to assign the found bus to the one refered to by the reference. The assignment operator does not exist or is not accessible. This is good so since having an assignment operator in this context is a bad idea and if present it would probably lead to slicing if the AbstractBus class would be instantiable (non-abstract).

EDIT: I believe you either have:

typedef std::map<string, AbstractBus*> tBussesMap;
tBussesMap localBusses;

or AbstractBus is not really abstract (does not contain any pure methods) (despite its name)

In either case I assume however you have classes derived from AbstractBus. And getLocalBusByName has to provide for situations where no bus is found and avoid inadvertent slicing.

So you should write getLocalBusByName method in the lines of:

AbstractBus* 
getLocalBusByName( const string& name )
{
    tBussesMap::iterator it = localBusses.find(name);

    if ( it != localBusses.end() )
    {
        return it->second;
    }

    return nullptr;
}

where localBusses is assumed to be of type:std::map<string, AbstractBus*> (the only viable way if AbstractBus is abstract)

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