:
int numSet = 0;
A = MyFunc(); if (A != nullptr) numSet++;
B = MyFunc(); if (B != nullptr) numSet++;
C = MyFunc(); if (C != nullptr) numSet++;
if (numSet == 1) // only one is set
あなたはまた、ヘルパー関数と動作をカプセル化できます。
LPCWSTR MyFuncWithCount(int &countSetProperly) {
LPCWSTR retVal = MyFunc();
if (retVal != nullptr) countSetProperly++;
return retVal;
}
int numSet = 0;
A = MyFuncWithCount(numSet);
B = MyFuncWithCount(numSet);
C = MyFuncWithCount(numSet);
if (numSet == 1) // only one is set
そこから次のステップアップが使用されるだろうループの範囲ベースとがブレースされていますそのリストは、以下の完全なプログラムごとのように:
#include <iostream>
#include <vector>
typedef void * LPCWSTR; // Couldn't be bothered including Windows stuff :-)
int main() {
// Only set two for test purposes.
LPCWSTR A = nullptr, B = nullptr, C = nullptr;
LPCWSTR D = &A, E = nullptr, F = &A;
int numSet = 0;
for (const auto &pointer: {A, B, C, D, E, F})
if (pointer != nullptr)
numSet++;
std::cout << "Count is " << numSet << std::endl;
}
それとも、以下のとおり、ラムダ関数を使用することによって、そのすべての栄光に現代のC++を受け入れることができます:
#include <iostream>
#include <vector>
typedef void * LPCWSTR; // Couldn't be bothered including Windows stuff :-)
int main() {
// Only set two for test purposes.
LPCWSTR A = nullptr, B = nullptr, C = nullptr;
LPCWSTR D = &A, E = nullptr, F = &A;
int numSet = 0;
[&numSet](const std::vector<LPCWSTR> &pointers) {
for (const auto &pointer: pointers)
if (pointer != nullptr)
numSet++;
} (std::vector<LPCWSTR>{A,B,C,D,E,F});
std::cout << "Count is " << numSet << std::endl;
}
おそらくですしかし、あなたの特定のケースの過剰殺到:-)
'xor'をチェーンできませんでしたか? '!(A^B^C)'のようなものです。 '^'はビット単位ですが、うまくいくでしょうか?ちょうどボールを吐き出す。 – Carcigenicate
@Carcigenicateありがとう!しかし、私は決して 'xor'をC++で使ったことはありません:-(それはとにかく試してみます。 – Blueeyes789
実際に1つだけがヌルである限り、どちらがヌルであるかは気にしませんか? – user4581301