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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:36:51+00:00 2026-05-26T03:36:51+00:00

Background: I’m writing a C++ wrapper interface for a C library not written by

  • 0

Background: I’m writing a C++ wrapper interface for a C library not written by me. My wrapper classes mimic the library structs, and in this library, some members of struct b point to members of struct a. The documentation for this library says, “Don’t destroy a variable of struct a before one of struct b.” There are actually situations where this should be allowed, so I want to handle the situation better in code. Therefore, in my wrapper, should an instance of class A with one or more instances of class B pointing to it be destroyed before all of those instances of B, I want to copy the data from A to every instance of B. Currently, I handle this with public member functions, as such:

// some code shortened or not shown

struct a {
  int d;                            // in reality, data is much more complicated
};
struct b {
  int* d;
};

class B;
class A {
  struct a a_;
  vector<B*> registered_bs_;                // should probably use unordered_set
public:
  ~A(void) { for (iterator it: registered_bs_) (*it)->copyA(); }    // C++0x for
  void registerB(B* b)   { registered_bs_.push_back(b); }
  void unregisterB(B* b) { registered_bs_.erase(b); }   // find() code not shown
};

class B {
  struct b b_;
  A* pa_;
public:
  B(A& a): b_(), pa_(0) { a.registerB(this); pa_ = &a; }
  ~B(void) { pa_->unregisterB(this); if (b_.d) delete b_.d; } // if B goes first
  void copyA(void) { b_.d = new int(*b_.d); }
};

As can be seen from the above, the register and copy member functions are only and should only be called from ctor/dtors. In other words, users of my classes should never call these functions. Therefore, in keeping with the principles of encapsulation and Scott Meyer’s philosophy of “make interfaces easy to use correctly and difficult to use incorrectly,” I want to put those functions in the private sections of A and B. However, this obviously means that I could no longer invoke them from their peer class. I’ve considered using friend functions, as follows:

// this doesn't work

class B;
class A {
  struct a a_;
  vector<B*> registered_bs_;

  void copyA(B& b) { b.b_.d = new int(*(b.b_.d)); }                 // circular
  friend void B::registerB(A& a);                                   // circular
  friend void B::unregisterB(A& a);                                 // circular
public:
  ~A(void) { for (iterator it: registered_bs_) copyA(*it); }       // C++0x for
};

class B {
  struct b b_;
  A* pa_;

  void registerB(A& a)   { a.registered_bs_.push_back(this); }
  void unregisterB(A& a) { a.registered_bs_.erase(this); }  // find() not shown
  friend void A::CopyA(B& b);
public:
  B(A& a): b_(), pa_(0) { registerB(a); pa_ = &a; }
  ~B(void) { unregisterB(*pa_); if (b_.d) delete b_.d; }
};

However, there are at least three things wrong with this code: 1) there’s a circular relationship that can’t be resolved, 2) each class is still trying to access private members of the other class in the friend declarations, and 3) it isn’t well encapsulated or intuitive.

Therefore, I ask again: is there a better way to design two classes that privately manipulate each other’s data?

  • 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-26T03:36:52+00:00Added an answer on May 26, 2026 at 3:36 am

    Yes, look into C++ friends.

    class B;
    
    class A
    {
        friend class B;
        // ...
    };
    
    class B
    {
        friend class A;
        // ...
    };
    

    The C++ FAQ has a nice explanation of friendship.

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

Sidebar

Related Questions

Background: This is really a general best-practices question, but some background about the specific
Background: Some time ago, I built a system for recording and categorizing application crashes
Background I am writing and using a very simple CGI-based (Perl) content management tool
BACKGROUND: I'm writing a single level cache simulator in C for a homework assignment,
Background: I have a short list of strings. The number of strings is not
Background: I've wrote a small library that is able to create asp.net controls from
Background / Disclaimer First of all, please feel free to skip this entirely if
Background: I have viewed this question as well as this one - sadly, to
BACKGROUND I am automating an PowerPoint 2007 via C# I am writing unittests using
Background This is only my second PyQt4 project. Developing a Windows app that has

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.