小编ant*_*npp的帖子

无法使用来自私有基的公共成员函数的指针

考虑这段代码:

class Base {
 public:
  int foo(int x) const { return 2*x; }
};

class Derived : Base {
 public:
  using Base::foo;
};
Run Code Online (Sandbox Code Playgroud)

现在Derived有一个公共方法 foo 并且可以调用它

Derived d;
d.foo(2);   // compiles (as it should)
Run Code Online (Sandbox Code Playgroud)

但是,如果我通过指针使用该方法,我将无法执行任何操作:

Derived d;
(d.*&Derived::foo)(2);  // does not compile because `Derived::foo` expects a pointer to `Base` and `Derived` cannot be casted to its private base class (without a C-style cast).
Run Code Online (Sandbox Code Playgroud)

对于这种行为是否有任何合乎逻辑的解释,或者可能是标准中的疏忽?

c++ language-lawyer

19
推荐指数
1
解决办法
609
查看次数

这是Function接口的正确用法吗?

我想熟悉lambda函数.首先,我决定写一个名为的方便的类TernaryOperator.那么,问题是我是否正确地认识了这一意识形态,或者我错过了一些应该以不同方式完成的事情?

public class TernaryOperator<T, U> implements Function<T, U> {

    private final Function<T, U> f;

    public TernaryOperator(Predicate<? super T> condition,
                           Function<? super T, ? extends U> ifTrue,
                           Function<? super T, ? extends U> ifFalse) {
        this.f = t -> condition.test(t) ? ifTrue.apply(t) : ifFalse.apply(t);
    }

    @Override
    public U apply(T t) {
        return f.apply(t);
    }
}
Run Code Online (Sandbox Code Playgroud)

我看到这个类的用法是这样的:

Predicate<Object> condition = Objects::isNull;
Function<Object, Integer> ifTrue = obj -> 0;
Function<CharSequence, Integer> ifFalse = CharSequence::length;
Function<String, Integer> safeStringLength = new TernaryOperator<>(condition, ifTrue, …
Run Code Online (Sandbox Code Playgroud)

java lambda java-8

7
推荐指数
1
解决办法
580
查看次数

在 bash 中测试程序

我用 C++ 编写了一个程序,现在我有了一个二进制文件。我还生成了一堆用于测试的测试。现在我想用 bash 自动化测试过程。我想在我的二进制文件的一次执行中保存三件事:

  1. 执行时间处理时间
  2. 退出代码
  3. 程序的输出

现在我正在使用一个脚本,该脚本只测试二进制文件是否完成其工作并返回 0,并且不保存我上面提到的任何信息。我的脚本看起来像这样

#!/bin/bash

if [ "$#" -ne 2 ]; then
    echo "Usage: testScript <binary> <dir_with_tests>"
    exit 1
fi

binary="$1"
testsDir="$2"

for test in $(find $testsDir -name '*.txt'); do
    testname=$(basename $test)

    encodedTmp=$(mktemp /tmp/encoded_$testname)
    decodedTmp=$(mktemp /tmp/decoded_$testname)

    printf 'testing on %s...\n' "$testname"

    if ! "$binary" -c -f $test -o $encodedTmp > /dev/null; then
        echo 'encoder failed'
        rm "$encodedTmp"
        rm "$decodedTmp"
        continue
    fi

    if ! "$binary" -u -f $encodedTmp -o $decodedTmp > /dev/null; then
        echo 'decoder …
Run Code Online (Sandbox Code Playgroud)

bash

5
推荐指数
1
解决办法
209
查看次数

爪哇尼奥。空路径

谁能告诉我Paths.get("")指向哪里?

这是代码和输出。

public static void main(String[] args) {
    Path path = Paths.get("");
    System.out.printf("`%s`%n", path);
    System.out.printf("`%s`%n", path.normalize());
    System.out.println(Files.exists(path));
    System.out.println(Files.isExecutable(path));
}

``
``
true
true
Run Code Online (Sandbox Code Playgroud)

java nio

4
推荐指数
1
解决办法
5229
查看次数

具有虚拟多重继承的构造函数定义

我认为一个例子最能描述我的问题。

struct Base {
    Base() = delete;
    Base(int x) : x(x) {}
    int x;
};

struct Derived1 :  virtual Base {
    Derived1(int y) : Base(5), y(y) {}
    int y;
};

struct Derived2 :  virtual Base {
    Derived2(int z) : Base(10), z(z) {}
    int z;
};

struct Derived3: Derived1, Derived2 {
public:
    Derived3(int y, int z) : Derived1(y), Derived2(z) {}

};
Run Code Online (Sandbox Code Playgroud)

我得到一个错误: In constructor ‘Derived3::Derived3(int, int)’: error: use of deleted function ‘Base::Base()’ Derived3(int y, int z) : Derived1(y), Derived2(z) {}

我不明白为什么会出现这个错误。在我看来,所有的基类实际上得到通过它们的构造函数初始化在这个例子中(明确 …

c++ inheritance multiple-inheritance

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

猜测用户用java(数组)思考的数字

我对编程很陌生,我们的老师要求我们制作一个能够猜出用户使用数组思考的数字的程序.我这样做了:

import java.util.Scanner;

public class Exercise11 {

    public static void main(String[] args) {
        Scanner entrada = new Scanner(System.in);
        System.out.println("Think a number between 1 and 100");
        int array[] = new int[100];

        for (int x = 0; x < 100; x++) {
            array[x] = (int) ((Math.random() * 100) + 1);
        }
        //This allow us to fill the array with random numbers, without caring if they are repeated or not.
        for (int i = 0; i < 100; i++) {
            for (int j …
Run Code Online (Sandbox Code Playgroud)

java arrays for-loop

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

try-catch in for循环

我遇到了一些我今天无法向自己解释的事情.在线资源上编写一个将在某些测试中运行的程序是一项小任务.你能否解释一下两种方法之间的区别以及为什么其中一种方法在某些测试中失败(我没有它们).

任务是编写一个静态方法,打开一个连接,然后通过调用它的方法尝试3次与这个连接做一些抽象的东西.问题是您使用的任何方法都可能抛出异常(打开连接和连接方法).您必须尝试完成3次(如果所有尝试都失败 - 抛出异常),另一个条件是必须关闭每个打开的连接.

连接类被调用RobotConnection并实现AutoCloseable.这个类有方法void moveRobotTo(int x, int y)(它是任务中的"做东西方法").你RobotConnection只能通过调用获得实例RobotConnectionManager.getConnection().这些类的方法可以抛出RobotConnectionException extends RuntimeException.

所以真正的问题就在这里:

这段代码失败了(不知道为什么,可能是无限循环):

public static void moveRobot(RobotConnectionManager robotConnectionManager, int toX, int toY) {
    boolean success = false;
    for (int i = 0; !success && (i < 3); ++i) {
        try (RobotConnection connection = robotConnectionManager.getConnection()) {
            connection.moveRobotTo(toX, toY);
            success = true;
        }
    }
    if (!success) {
        throw new RobotConnectionException("3 attempts failed");
    }
}   
Run Code Online (Sandbox Code Playgroud)

这个被接受为工作(我看不到真正的差异=()

public static void moveRobot(RobotConnectionManager robotConnectionManager, …
Run Code Online (Sandbox Code Playgroud)

java exception-handling

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