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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:09:17+00:00 2026-06-06T06:09:17+00:00

I have a class A that looks like this: class A { // A’s

  • 0

I have a class A that looks like this:

class A {
// A's internal state
public:
  void someMethod();
  void anotherMethod();
};

I want to use A inside another class B. I don’t want B to be a subtype of A, but I want A‘s public methods to be accessible in some form to B‘s users.

One way to accomplish this is by simply including an instance of A as a public member of B:

class B {
public:
  A a;
  // other members
};

Another is to have A be a private member of B, and provide wrappers around A‘s public methods:

class B {
  A a;
public:
  void someMethod(){ a.someMethod(); }
  void anotherMethod(){ a.anotherMethod(); }
}

I’d like to know if there’s a “preferred” way of doing this (or even one that might not involve the two alternatives above), or if it’s simply a matter of preference. Thanks.

  • 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-06T06:09:20+00:00Added an answer on June 6, 2026 at 6:09 am

    I’d like to know if there’s a “preferred” way of doing this (or even one that might not involve the two alternatives above), or if it’s simply a matter of preference. Thanks.

    It really comes down to specifics here I’m afraid.

    You’re right in preferring composition over inheritance when inheritance doesn’t make sense. How you implement that depends on whether or not it makes sense to make an instance of A public.

    Is there any state in A that you do not want changed, or functions that you do not wish to be public? Do you wish to perform any pre- or post-pocessing when one of A‘s functions are called? Do you care is users set your instance of A to another? You probably do (though you could just make a getter function), so in this case prefer wrapping the functions.

    Does it make more sense for A to be completely public? If so you save time by not writing a bunch of boilerplate code.

    It really comes down to the semantics of your wrapper class here.

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

Sidebar

Related Questions

So I have a class that looks like this: internal class MyClass { public
I have a class that looks like this. class A { public: void doSomething();
I have a class that looks like this: public class BasePadWI { public WorkItem
Currently I have a class that looks like this: public class MyClass : IMyClass
Assume I have a class that looks like this: class Sample { public string
I have a class that roughly looks like this: public class ViewModel { public
I have a Handler class that looks like this: class Handler{ public $group; public
Let's say we have a class that looks like this: class A { public:
I have a District class that looks like this: public class District { public
I have a simple class that looks like this: public class Item { //

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.