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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:40:59+00:00 2026-05-23T09:40:59+00:00

I have the following situation: class B { public: void methodInB(); }; class C

  • 0

I have the following situation:

class B
{
    public:
        void methodInB();
};

class C
{
    public:
        void methodInC();
};

class A
{
    public: 
        void methodInA();
    private:
        B objB;
        C objC;
};

void A::methodInA()
{
    objB.methodInB();
}

int main()
{
    A objA;
    objA.methodInA();
    return 0;
}

I want to be able to call C::methodInC() from within B::methodInB(), but I’m not sure what the way to go about it would be (not without messing with globals).

My first idea was to add a C* pC pointer as a member of B, and then from methodInB() call it as pC->methodInC. This would require I set the pointer from within A before using the method (possibly in A‘s constructor). My problem is I may need to call other objects from within B if I add a D and E objects, and I don’t want to fill the class definition with pointers.

Is there some other way of doing this? An implicit reference to the object the object belongs to? Kind of like this but for the parent? So I could at least do parent->objC->methodInC()?

  • 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-23T09:40:59+00:00Added an answer on May 23, 2026 at 9:40 am

    I think the cleanest way would be to “inject the dependency”, that is, to pass objC to methodInB, which would then invoke methodInC on that object:

    void A::methodInA()
    {
        objB.methodInB(objC);
    }
    
    // ...
    
    void B::methodInB(C &objC)
    {
        objC.methodInC();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose you have the following situation #include <iostream> class Animal { public: virtual void
I have the following situation: class A { public: A(int whichFoo); int foo1(); int
I have the following situation: typedef void (*F_POINTER)(ClassName*); class ClassName { public: F_POINTER f;
I have following class: public class PairOfDice { private Dice d1,d2; public int Value
I have following situation class base { public: virtual void Accepted(SOCKET s) //// Event
I have the following classes class A{ private String name; private int value; public
Consider the following snippet: import java.util.*; public class EqualsOverload { public static void main(String[]
I have a problem with JPA 1.0 (OpenJPA) Following situation @Entity public class A{
I have the following helper class (simplified): public static class Cache { private static
Suppose I have the following situation: 9 class Program 10 { 11 public static

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.