【问题标题】:Chicken Egg Class Interface in C++C++中的鸡蛋类接口
【发布时间】:2021-09-29 08:26:07
【问题描述】:

我需要一些帮助来理解这个问题中使用的语法,以便我不久前进行评估。

将缺少的代码添加到ChickenEgg,这样就完成了以下操作:

  • Chicken 实现了 Bird 类。
  • 鸡下蛋后会孵化成新鸡。
  • 其他类型鸟类的卵应该孵化成其父类型的新鸟类。
  • 第二次孵化鸡蛋会引发 std::logic_error。

这是代码模板:

#include <stdexcept>
#include <functional>

class Egg;

class Bird
{
public:
    virtual ~Bird(){};
    virtual Egg *lay() = 0;
};

class Egg
{
public:
    Egg(std::function<Bird *()> createBird)
    {
        throw std::logic_error("Waiting to be implemented");
    }

    Bird *hatch()
    {
        throw std::logic_error("Waiting to be implemented");
    }
};

class Chicken : public Bird
{
public:
    Chicken()
    {
    }

    Egg *lay()
    {
        throw std::logic_error("Waiting to be implemented");
    }
};

#ifndef RunTests
int main()
{
    Bird *chicken = new Chicken();

    Egg *egg1 = chicken->lay();
    Bird *childChicken1 = egg1->hatch();
}
#endif

这是我尝试完成的一些任务。

#include <stdexcept>
#include <functional>
#include <iostream>
using namespace std;

class Egg;

class Bird
{
public:
    virtual ~Bird(){};
    virtual Egg *lay() = 0;
};

class Egg
{
    int hatchCount = 0;

public:
    Egg(std::function<Bird *()> createBird)
    {
        throw std::logic_error("Waiting to be implemented");
    }

    Bird *hatch()
    {
        if (hatchCount > 0)
        {
            throw std::logic_error("This egg already hatched!");
        }
        hatchCount++;
    }
};

class Chicken : public Bird
{
public:
    Chicken()
    {
    }

    Egg *lay()
    {
        return Egg(Chicken());
    }
};

#ifndef RunTests
int main()
{
    Bird *chicken = new Chicken();

    Egg *egg1 = chicken->lay();
    Bird *childChicken1 = egg1->hatch();
}
#endif

显然不多。我确实从 Bird 类中理解了 Chicken 的类扩展。我迷失了一些使用的关键字/语法(虚拟?函数 createBird??)。

为此:

Egg *egg1 = chicken->lay();

我们是否试图通过访问某个地方的变量来创建一个新的 egg 对象?

如果有人能阐明这个问题,我将不胜感激!

【问题讨论】:

    标签: c++ class object


    【解决方案1】:

    std::function&lt;Bird* ()&gt; 是 lambda 表达式的包装器。你可以实现一个私有变量std::function&lt;Bird* ()&gt; hatch_egg;,比如:

    class Egg
    {
        int hatchCount = 0;
    
    public:
        Egg(std::function<Bird *()> createBird) : hatch_egg(createBird)
        {
            throw std::logic_error("Waiting to be implemented");
        }
    
        Bird *hatch()
        {
            if (hatchCount > 0)
            {
                throw std::logic_error("This egg already hatched!");
            }
            hatchCount++;
            return hatch_egg();
        }
    private:
       std::function<Bird* ()> hatch_egg;
    };
    

    可以使用here 概述的语法来完成lambda 捕获,但简而言之[params_to_be_caprutred_from_current_context](arguments to the function) -&gt; trailing_ret_type {function body}。包装器的名称如您所料:std::function&lt;ret_type (parameter_types)&gt;,所以std::function&lt;Bird* ()&gt; 是一个不带参数并返回Bird* 的函数。

    通常你会这样做:

    
    class Chicken{
    public:
        Egg* lay(){
            // We don't need any parameters from this context hence [], but if
            // we want to add a parent to the Bird object and the constructor
            // would take a Bird* then it would be like:
            // return new Egg([this]() -> Bird*{ return new Chicken(this) });
            return new Egg([]() -> Bird*{ return new Chicken; });
        }
    };
    
    

    【讨论】:

      猜你喜欢
      • 2017-03-08
      • 1970-01-01
      • 2017-02-28
      • 2010-11-25
      • 2011-02-13
      • 2016-03-03
      • 1970-01-01
      • 2010-10-21
      • 2020-05-18
      相关资源
      最近更新 更多