小编Car*_*bon的帖子

将构造对象但不是构造函数的方法

我正在使用C++中的不可变结构.说我想mathematics进入zippy课堂 - 可能吗?它构造了一个zippy,但该函数不能是构造函数.它是否必须住在课外?

struct zippy
{
    const int a;
    const int b;
    zippy(int z, int q) : a(z), b(q) {};
};

zippy mathematics(int b)
{
    int r = b + 5;
    //imagine a bunch of complicated math here
    return zippy(b, r);
}

int main()
{
    zippy r = mathematics(3);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++

0
推荐指数
1
解决办法
77
查看次数

为什么我必须明确定义由固有类提供的方法?

考虑以下:

#include <string>

struct animal
{
public:
    virtual std::string speak() = 0;
};

struct bird : public animal
{
public:
    std::string speak()
    {
        return "SQUAK!";
    }
};

struct landAnimal : public animal
{
    virtual int feet() = 0;
};


struct sparrow : public bird, public landAnimal
{
    int feet() { return 2; }
    // This solves it, but why is it necessary, doesn't bird provide this?
    // std::string speak(){ return this->speak(); } 
};

int main()
{
    sparrow tweety = sparrow(); …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance diamond-problem

0
推荐指数
1
解决办法
51
查看次数

是否可以禁用bool运算符<(float,int)

在C++中,我可以在operator<比较整数和浮点类型时禁用或强制显式转换吗?operator<在integer和float类型之间使用很容易导致定量代码中的错误.我试过bool operator<(double, int) = delete;但它期望其中一种类型是类或枚举.如何使下面的代码错误无法编译?

int main()
{
    if (3.0 < 4)
    {
        std::cout << "X" << std::endl;
    }
    else
    {
        std::cout << "Y" << std::endl;
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++

0
推荐指数
1
解决办法
53
查看次数

if($?){}是否有Powershell模式

我发现自己将这些链接很多,例如:

do-cmd-one
if($?)
{
do-cmd-two
}
...
Run Code Online (Sandbox Code Playgroud)

然后最后:

if(!$?)
{
   exit 1
}
Run Code Online (Sandbox Code Playgroud)

我认为在Powershell中有这种模式,但我不知道。

syntax error-handling powershell

0
推荐指数
1
解决办法
70
查看次数

从函数的结果中分配std :: shared_ptr的一个衬里是什么?

有一个更惯用的方法来做到这一点:

Potato SpecialPotato();
std::shared_ptr<Potato> givePotato()
{
    std::shared_ptr<Potato> ret;
    *ret = SpecialPotato();
    return ret;
}
Run Code Online (Sandbox Code Playgroud)

c++

-2
推荐指数
1
解决办法
70
查看次数

为什么sizeof不关心功能?

鉴于以下内容,您将看到x和y的大小相同,但y具有附加功能.sizeof中包含哪些内容,哪些内容不包含在内?

struct x
{
    double a;
    double b;
    double c;
    double d;
};
struct y
{
    double a;
    double b;
    double c;
    double d;
    y(double q, double r, double s, double t) : a(q), b(r), c(s), d(t) {};
};
std::cout << sizeof(x)-sizeof(y) <<std::endl;
Run Code Online (Sandbox Code Playgroud)

c++

-3
推荐指数
1
解决办法
144
查看次数