Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. [Solved] How to create a class that only can be instantiated from another?

[Solved] How to create a class that only can be instantiated from another?

Scheduled Pinned Locked Moved C++ Gurus
7 Posts 5 Posters 6.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    inejose
    wrote on last edited by
    #1

    Hi,

    I would like to create a class ( ClassA ) with the following property:

    ClassA only can be instantiated from ClassB.

    For example, if another class called ClassC tries to instantiate ClassA, compiler must launch an error:

    @ClassC::someMethod {

    ClassA *objectA = new ClassA();

    }@

    ERROR: ClassC can't instantiate ClassA.

    It is possible?

    I was thinking about private constructor and friend class (ClassA and ClassB in my example), but constructor can't be private by definition.

    Thank you in advance.

    [Edit: Moved to C++ Gurus forum, as this is not Qt-related; mlong]

    call it version 1.0 <a href="http://www.clicpsicologos.com">psicologo online</a> & <a href="http://www.isomus.com">musicoterapia</a>

    1 Reply Last reply
    0
    • ZlatomirZ Offline
      ZlatomirZ Offline
      Zlatomir
      wrote on last edited by
      #2

      Have you tried the code for what you where thinking?
      I don't know about anything that stops you to code a class with private or protected constructor.

      And if you give more details about what are you trying to accomplish, maybe some of us come with more appropriate solutions.

      https://forum.qt.io/category/41/romanian

      1 Reply Last reply
      0
      • T Offline
        T Offline
        task_struct
        wrote on last edited by
        #3

        Hi,

        If you put definition of class A inside private section of class B, it will be visible only inside calss B.

        "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program."

        • Linu...
        1 Reply Last reply
        0
        • T Offline
          T Offline
          Tannin
          wrote on last edited by
          #4

          I agree with Zlatomir: the constructor CAN be private and there are many reasonable use-cases (i.e. singletons)
          Just remember to also make the copy constructor private because otherwise it will be auto-generated as public, so class C could then make a copy of an A that was created in B.

          1 Reply Last reply
          0
          • I Offline
            I Offline
            inejose
            wrote on last edited by
            #5

            Thanks to Zlatomir, task_struct and Tannin, it works!

            I read in a C++ manual that constructors cannot be private, so I didn't try.

            Finally, I used private constructors and friends class to achieve my first objective as follow:

            Class A definition:

            @class ClassA {

            public:
            friend class ClassB;

            private:
            ClassA();

            //(edited) Copy constructor added
            ClassA(const ClassA &classAsource);
            

            };@

            Class B definition:

            @class ClassB
            {

            public:
            ClassB();

            };@

            Now, only ClassB can create objects from ClassA class, because only ClassB can access to constructor.

            In this way I ensure that nobody will use ClassA on my code except ClassB.

            Thank you!

            call it version 1.0 <a href="http://www.clicpsicologos.com">psicologo online</a> & <a href="http://www.isomus.com">musicoterapia</a>

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mlong
              wrote on last edited by
              #6

              Don't forget what Tannin wrote:

              bq. Just remember to also make the copy constructor private because otherwise it will be auto-generated as public, so class C could then make a copy of an A that was created in B.

              Software Engineer
              My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

              1 Reply Last reply
              0
              • I Offline
                I Offline
                inejose
                wrote on last edited by
                #7

                [quote author="mlong" date="1338822657"]Don't forget what Tannin wrote:

                bq. Just remember to also make the copy constructor private because otherwise it will be auto-generated as public, so class C could then make a copy of an A that was created in B.[/quote]

                Thanks mlong, I will keep it in mind.

                (I have edited my previous code example.)

                call it version 1.0 <a href="http://www.clicpsicologos.com">psicologo online</a> & <a href="http://www.isomus.com">musicoterapia</a>

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved