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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:09:38+00:00 2026-05-11T08:09:38+00:00

I have a class with a (non smart) pointer to an interface object (lets

  • 0

I have a class with a (non smart) pointer to an interface object (lets call it pInterface) and I am building a nested class which also needs access to that interface. I am going to get around this by passing the pointer to the interface into the constructor of the nested class like so:

CNestedClass someClass( pInterface, ... ); 

However I am unsure of the best way of storing this pointer in the nested class. I could use:

1) A scoped (or other smart) pointer (to the original object) 2) A pointer to a pointer  

What would you guys suggest and why?

EDIT: I should clarify – the nested class will need to call methods on the interface object, however it does not create it (or modify the object ‘pointed’ to ), the parent class is responsible for 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. 2026-05-11T08:09:38+00:00Added an answer on May 11, 2026 at 8:09 am

    The use a pointer to a pointer is if either class may alter the value of the pointer – e.g. by deleting the existing object and replacing it with a new one. This allows both classes to still use the same object by dereferencing the pointer-to-pointer.

    If not your concern is ensuring the object remains valid throughout the lifetime of both classes.

    • If the nested class lives shorter you don’t really have to worry.
    • If it’s the same, provided you clean-up in the correct order (e.g. nested class first, object later) then again, you don’t have to worry
    • If the nested class could persist after the owner is destroyed then you must implement a way to ensure the object also persists.

    If you need to ensure the lifetime of the object it could be done via reference counting-semantics, either manually or through a smart-pointer interface.

    For a smart pointer then boost::shared_ptr would be a good choice. shared_ptr allows the ownership of an object to be shared amount multiple pointers. When the last shared_ptr goes out of scope, the object is deleted.

    (note this is not the case with auto_ptr, where an object are exclusively owned).

    Things to be aware of;

    1. When using boost::shared_ptr be sure the nested class has a copy of the shared_ptr and not a reference/pointer.
    2. std::auto_ptr behaves quite differently, objects are exclusively owned and not shared
    3. boost::shared_ptr can only work with heap objects, e.g pointers returned from a call to ‘new’

    Example:

    typedef boost::shared_ptr<Interface> shared_interface;  class NestedClass {   shared_interface mInterface; // empty pointer }  void NestedClass::setInterface(shared_interface& foo) {   mInterface= foo; // take a copy of foo. }  void ParentClass::init( void ) {   // mInterface is also declared as shared_interface   mInterface = new Interface();   mNestedClass->setInterface(mInterface); } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a non-Activity class (let's call it NonActivity) that needs to post a
I have a problem to test my non activity-class which need the context of
I have a smart pointer class and I want to overload operator-> ; it's
I have a non-service class which is defined as such: class A{ B b
I am building a dynamic view in a non-activity class, which is called by
I tried the following in Clojure, expecting to have the class of a non-lazy
I have non-template class with a templatized constructor. This code compiles for me. But
I have a class (called Employee. non static) defined in a class library. I
I have a static field in a non static class. public class DBtools {
I have the following: MODEL.PY LIST = (('Manager', 'Manager'),('Non-Manager', 'Non-Manager'),) class Employee(models.Model): fname =

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.