Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3785720
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T11:34:18+00:00 2026-05-19T11:34:18+00:00

Suppose i have a non-copyable class Foo , and one of its constructors just

  • 0

Suppose i have a non-copyable class Foo,
and one of its constructors just happens to receive a reference to Foo.

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

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

The compiler thinks that this is a copy-constructor,
while it does something entirely unrelated to copying (so the assignment operator is disabled).

Is there any danger in defining the constructor this way,
or should i change its signature artificially, e.g. use pointer instead of reference,
or add a mandatory dummy parameter?

Here is some context (probably not required to understand/answer my question).

I have a class library that i have written myself,
which serves as a connection between user code and another library.
The other library provides a service that i call frobnicate for brevity.
User code can look like this:

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

I want to support an hierarchy of user objects: each object is contained in another (its parent),
while there are a few (in my case, 5) top-level objects that contain every other objects.

Each object has a log-file; i want each call to be logged in several log files,
up the containment hierarchy until a top-level object.

I have it implemented this way:

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
        };
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T11:34:19+00:00Added an answer on May 19, 2026 at 11:34 am

    This appears to be an unfortunate ambiguity in the meaning of such a constructor. I think the most straight forward way to disambiguate this is to use a pointer

    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;
    };
    

    I know a lot of people who would expect the parameter to be a pointer anyway. Incidentally, you yourself mistyped parent.frobnicate as parent->frobnicate.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have this class hierarchy: class A { public: virtual void foo(Base *b)
Suppose I have a resource like below: namespace OpenRastaApp.Resources { public class Foo {
Suppose you have a class NonCopyable class NonCopyable { public: NonCopyable(int n){} ~NonCopyable(){} [...]
Suppose you have some method that could be made static, inside a non-static class.
Suppose I have this class: template</*some other parameters here */class toggle> class Foo {
Suppose I have this: class Validator { private $db; public checkIfUsernameAlreadyExists($username) { if (!$this->db)
Suppose I have a function template: template <typename T> std::string foo(const T& x) {
Suppose I have a class template <typename T> class A { public: template <typename
Suppose I have this code... class GraphFactory : public QObject { private: QMap<QString, IGraphCreator*>
Suppose I have a non-recurring event that needs to be raised X seconds from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.