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 6026179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:24:30+00:00 2026-05-23T04:24:30+00:00

I have a base class which looks something like this: class Base { public:

  • 0

I have a base class which looks something like this:

class Base
{
public:
  typedef std::shared_ptr<Base> ptr_t;
  typedef std::weak_ptr<Base> wptr_t;

  enum class Type { foo, bar, baz };

  Type x;
  // ...
};

I’d like those internal types to be public so that I can do stuff like Base::ptr_t my_ptr(new Base); and so on. But if I make a new class like this…

class Derived : public Base
{
  // ...
};

unfortunately, Derived::ptr_t is still a Base pointer. I’d like Derived to publicly inherit x from Base, but not inherit ptr_t,wptr_t, or Type. For example

Derived a;
a.x = Base::Type::foo; // this should work
a.x = Derived::Type::foo; // but I want this to fail

Is this possible, perhaps though some magic use of friend or virtual or something like that?

  • 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-23T04:24:30+00:00Added an answer on May 23, 2026 at 4:24 am

    Based on the answers of iammilind and Luc Danton, here’s what I’ve come up with:

    class Base
    {
    private:
      // only 'BaseClass' is allowed to derive from Base
      Base() { }
      friend class BaseClass;
    public:
      typedef std::shared_ptr<Base> ptr_t;
      typedef std::weak_ptr<Base> wptr_t;
    
      enum class Type { foo, bar, baz };
    
      Type x;
      // ...
    };
    
    class BaseClass : public Base
    {
    private:
      // make all the raw_Base types private
      using Base::ptr_t;
      using Base::wptr_t;
      using Base::Type;
    };
    
    class Derived : public BaseClass
    {
      // define as usual, and now all is well in the world.
    };
    
    // now, to test it
    class Derived2 : public Base { }; // fails
    
    Derived d;
    d.x = Derived::Type::foo; // fails
    d.x = Base::Type::foo; // works
    // Which is exactly what I wanted.
    

    As far as I can tell, the only problem with this solution is that it adds a new and potentially confusing class. The class is defined in such a way that it can’t really be misused – Base itself cannot be derived from except by BaseClass, but still, BaseClass is an unattractive piece of namespace-clutter.

    However, for the particular piece of code that I intend to use this in, I happen to be using the equivalent of BaseClass already to solve an unrelated problem. So this BaseClass technique suits my purposes just fine.

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

Sidebar

Related Questions

We have a model association that looks something like this: class Example < ActiveRecord::Base
I have an abstract class. Let's call it Lifeform. It looks something like: public
I have a template class which looks like the following: template <template <class TypeT>
I'm working with a product called SiteFinity. I have a class which looks like
I have a base class which is non-generic with a derived generic class. The
I have an abstract base class which acts as an interface. I have two
I have a base class in which I want to specify the methods a
I have a base class Node which contains a list of child nodes. Node
I have a base class object array into which I have typecasted many different
I have a base class with a property which (the get method) I want

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.