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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:09:43+00:00 2026-05-29T05:09:43+00:00

In my current code I have an TNCPConnection class which is derived from enable_shared_from_this

  • 0

In my current code I have an TNCPConnection class which is derived from enable_shared_from_this:

class TNCPConnection : public boost::enable_shared_from_this<TNCPConnection> { ... };

Now I need to create a second class ETCPConnection which will share a lot of code with the existing TNCPConnection class. So the obvious solution is to have an abstract base class CPConnectionBase which implements the common functionality.

I know that only one class in a inheritance hierarchy can derive from enable_shared_from_this. While looking for a solution I stumbled across the CRTP pattern, but I’m not sure if I understood this correctly. Here is my approach:

template <class Derived>
class CPConnectionBase : public boost::enable_shared_from_this<Derived>
{
public:

    CPConnectionBase(void) { }
    virtual ~CPConnectionBase(void) = 0 { }
};

class TNCPConnection : public CPConnectionBase<TNCPConnection> { ... };

class ETCPConnection : public CPConnectionBase<ETCPConnection> { ... };

Is this the correct way to solve my problem? Will reference counting work correctly with this approach? In particular I’d like to know if the template parameter of enable_shared_from_this must be Derived (as in the example above) or something like CPConnectionBase<Derived>?

  • 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-29T05:09:44+00:00Added an answer on May 29, 2026 at 5:09 am

    If you use that approach each derived class will have its own unique base class. Even though they have the same name they will be different instantiations. Are you sure that having the base class inherit from enable_shared_from_this won’t provide the capability to all children?

    Also, which will share a lot of code with the existing ... class. is not an immediate indicator to use inheritance. In general, inherit to extend, not to reuse. If all the base class does is implement common functionality but doesn’t provide a common interface to the two classes you should probably rethink the approach. Either use composition and delegation to the common methods, or, if they don’t have continuous state, put the common code into (possibly anonymous) namespaces and call them from the actual functional classes you need. You haven’t provided quite enough information to determine exactly what you’re trying to do.

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

Sidebar

Related Questions

I have a code sample that gets a SEL from the current object, SEL
We have some code in which we need to maintain our own identity (PK)
I have a jar file that runs this code: public class InputOutput { /**
I am having a bit of difficulty with this current code I have set
As a beginner in Ocaml, I have this current working code: ... let ch_in
I have some code that gets the current logged in user. userID = request.session.get(_auth_user_id)
I have this code: Dim pathString As String = HttpContext.Current.Request.MapPath(Banking.mdb) Dim odbconBanking As New
This is my current scenario: I have checked in my grails app code +
We have a scenario where some .NET code is attempting to access the current
The following code summarizes the problem I have at the moment. My current execution

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.