How to disallow temporaries

后端 未结 10 1715
一个人的身影
一个人的身影 2020-12-02 05:19

For a class Foo, is there a way to disallow constructing it without giving it a name?

For example:

Foo(\"hi\");

And only allow it i

10条回答
  •  清歌不尽
    2020-12-02 05:55

    How about a little hack

    class Foo
    {
        public:
            Foo (const char*) {}
    };
    
    void Foo (float);
    
    
    int main ()
    {
        Foo ("hello"); // error
        class Foo a("hi"); // OK
        return 1;
    }
    

提交回复
热议问题