Constructor Foo :: Foo gets a reference to Foo, but not copy-constructor

Suppose I have a non-copyable class Foo, and one of its constructors just gets a link to Foo.

class Foo
{
public:
    Foo(Foo& parent) {...}

private:
    void operator=(Foo); // disabled
    ...
};

The compiler considers this to be an instance constructor, while it does something completely unrelated to copying (therefore, the assignment operator is disabled).

Is there any danger when defining a constructor this way, or should I change my signature artificially, for example. use a pointer instead of a link, or add a required dummy parameter?

Here is some context (it may not be necessary to understand / answer my question).

, , . , frobnicate . :

class UsefulObject: public mylib::Frobnicator
{
    ...
    void DoStuff()
    {
        int x = ...
        ...
        frobnicate(x); // it important to allow simple syntax here
        frobnicate(x + 1);
        ...
    }
    ...
};

: ( ) ( , 5) , .

; , , .

:

namespace mylib
{
    class Frobnicator // provides the frobnication service
    {
    public:
        Frobnicator(Frobnicator& parent): parent(parent) {}
    protected:
        virtual void frobnicate(int x) {
            ... // some logging code
            parent->frobnicate(x);
        }
    private:
        Frobnicator& parent;
    };

    namespace internal // users of mylib, please don't use this!
    {
        class TheUltimateFrobnicator: public Frobnicator
        {
        protected:
            virtual void frobnicate(int x) {
                the_other_library::frobnicate(x);
            }
        private:
            TheUltimateFrobnicator(int id); // called by a factory or some such
        };
    }
}
+3
1

. , -

class Frobnicator // provides the frobnication service
{
public:
    explicit Frobnicator(Frobnicator *parent): parent(parent) {}
protected:
    virtual void frobnicate(int x) {
        ... // some logging code
        parent->frobnicate(x);
    }
private:
    void operator=(Foo); // disabled
    Frobnicator(Frobnicator const&); // disabled

    Frobnicator *parent;
};

, , . , parent.frobnicate parent->frobnicate.

+7

Source: https://habr.com/ru/post/1787287/


All Articles