2

次のクラスの例では、getter メソッドの例外安全性の保証は何ですか?

そのようなゲッターメソッドは、最低限の強力な保証を提供しますか?

基本型を値で返すと、常にノースロー保証が提供されますか?

class Foo
{
public:

    // TODO: Constructor

    // Getter methods
    int getA() const { return a; }
    std::string getB() const { return b; }
    std::vector<int> getC() const { return c; }
    Bar getD() const { return d; }
    std::vector<Bar> getE() const { return e; }

protected:
    int a;
    std::string b;
    std::vector<int> c;
    Bar d;
    std::vector<Bar> e;
}
4

2 に答える 2

3

例外の安全性はまったく保証されません。

たとえば、getA()が初期化されていない場合は例外をスローする可能性aがあります (または、動作が定義されていないため、他のことを行います)。一部のチップ (Itanium など) は、初期化された変数が読み取られた場合に通知します。

getC()メモリが不足している場合はスローされる可能性がstd::bad_allocあります。同上getB()getD()およびgetE()

于 2015-09-24T10:32:32.160 に答える