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

  • Home
  • SEARCH
  • 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 9005031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:02:42+00:00 2026-06-16T01:02:42+00:00

I want to have a class hierarchy and be able to create objects from

  • 0

I want to have a class hierarchy and be able to create objects from it only inside a Factory.

Example:

class Base
{
    protected:
        Base(){};
        virtual void Init(){};

    friend class Factory;
};

class SomeClass : public Base
{
    public://I want protected here! Now it's possible to call new SomeClass from anywhere!
        SomeClass(){};
        void Init(){};
};

class Factory
{
    public:
        template<class T>
        T* Get()
        {
            T* obj = new T();
            obj->Init();

            return obj;
        }
};

int main()
{
    Factory factory;
    SomeClass *obj = factory.Get<SomeClass>();
}

My problem is that I want to be able to make objects only from Factory, but I don’t want to declare friend class Factory in every class derived from Base.

Is there any way to propagate friend in derived classes? Is there any other way to achieve this behavior?

  • 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-06-16T01:02:43+00:00Added an answer on June 16, 2026 at 1:02 am

    No, it’s deliberately impossibile.

    Is an issue by encapsulation.

    Suppose to have a class “PswClass” that manage any password, that is cascade friend with other class: if I inherit from PswClass:

     class Myclass : public PswClass {
         .......
     }
    

    In this way I can, maybe, have access to field that it would be private.

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

Sidebar

Related Questions

I want to have a base class which dictates the alignment of the objects
I have a following class hierarchy: class Base{ .... virtual bool equal(Base *); }
Preface: I want to be able to store a (abstract) base class reference from
I have a class that I want to contain multiple objects of something I
Imagine I have such class hierarchy: Base A : Base B : Base C
I have a problem with Class.getSuperclass(). I want to generate a hierarchy of classes
I have a class hierarchy where IObject is the base class for product and
I have the following hierarchy: Party is a base class, extended by Person and
I have a simple class hierarchy where I have a virtual method that is
I have a class hierarchy, this one: type TMatrix = class protected //... public

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.