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

The Archive Base Latest Questions

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

I have an (for C++ programmers better than me) simple problem with classes and

  • 0

I have an (for C++ programmers better than me) simple problem with classes and pointers.
I thought about posting example code describing my problem but I found it easier to just explain it in words.

Assuming I have three classes:

  • Class A: The main class – it contains an instance of both B and C.
  • Class B: This class contains a method that outputs some string, call it Greet().
  • Class C: This one has a method too, but that method has to call Greet() in the instance of B that is located in class A. Let’s name it DoSomethingWithB()

So the program starts, in the main function I create an instance of A. A, again, creates instances of B and C. Then, A calls C.DoSomethingWithB();.

And there my problem begins: I can’t access B from inside C.

Obviously, I will need to pass a pointer to B to the DoSomethingWithB() function so that I can call B.Greet() from inside C

Long explanation, short question: How do I do this?

Example code incoming:

#include <iostream>
using namespace std;

class B
{
public:
    void Greet( )
    {
        cout<<"Hello Pointer!"<<endl;
    }
};

class C
{
public:
    void DoSomethingWithB( )
    {
        // ... b.Greet( ); Won't work obviously
    }
};

class A
{
public:
 B b; // Not caring about visibility or bad class/variable names here
 C c;
 void StartTest( )
 {
      c.DoSomethingWithB( );
 }
};

int main( )
{
    A mainInstance;
    mainInstance.StartTest();
}
  • 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-13T17:26:36+00:00Added an answer on May 13, 2026 at 5:26 pm

    Wouldn’t you simply pass a pointer or reference to he B object?

    class C 
    { 
    public: 
        void DoSomethingWithB( B& b) 
        { 
            b.Greet( ); // will work fine 
        } 
    }; 
    
    class A 
    { 
    public: 
     B b; // Not caring about visibility or bad class/variable names here 
     C c; 
     void StartTest( ) 
     { 
          c.DoSomethingWithB( b); 
     } 
    };
    

    If the DoSomethingWithB() function won’t modify the passed in B instance, you should mark the reference const so it can be called with a const B object (for example if the owning A object happens to be const):

    void DoSomethingWithB( B const& b);
    

    You have a few options for how to pass the B object to the function:

    • as a reference (void DoSomethingWithB( B& b)) which will let the function modify the passed in object. Changes will be refelected in the object that’s passed in.

    • as a const reference (void DoSomethingWithB( B const& b)) which won’t let the function modify the passed in object (unless the constness is cast away – something which can lead to undefined behavior if done on an object the is truely const)

    • as a pointer or const pointer to a B object (void DoSomethingWithB( B* b) or void DoSomethingWithB( B const* pb) ). These have similar performance to passing by reference, but the function could be passed a NULL pointer which needs to be dealt with properly (by not dereferencing it in that case). Also, the call of the function would need to change slightly to pass the address of the B object:

      c.DoSomethingWithB( &b);
      
    • as a pass-by-value parameter (void DoSomethingWithB( B b)). This has difference that the function can do whatever it likes with theo bject passed in and it won’t affect the originally passed object since the function is dealing with a copy. The disadvantage is that passing the parameter causes a copy to be made which might be expensive. You could also pass in a const value, but there’s little to recommend that over passing a const reference.

    Note that when chosing the parameter passing method, you should first chose based on the sematics of what you need the function to do (or not do). Worry about efficiency later. Always first design and code for correctness – worry about efficiency only after you have the design and code correct.

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

Sidebar

Related Questions

This not a programming question but Most of the programmers using Eclipse should have
Let's say I have programmers and artists working on a project. The artists have
I have multiple programmers contributing examples for javadocs and some examples contain comments formatted
Q1: Is there something like too much ajax?? Explanation: I have been seeing programmers
We have a large number of programmers on different platforms all using CVS. We
Hi Programmers, Actually I have a DataGridViewComboBoxCell in DataGridvIew and I need to change
Question fellow programmers. Let's say I have a stand alone application in it's own
Suppose I have the following senario: Trunk is my main development line where programmers
This question is for experiences programmers. Do you have a set process that you
I am going through Programming Ruby - a pragmatic programmers guide and have stumbled

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.