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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:26:14+00:00 2026-05-28T18:26:14+00:00

I have two classes, class A and class B . A.h -> A.cpp B.h

  • 0

I have two classes, class A and class B.

A.h -> A.cpp
B.h -> B.cpp

And then, I set B as a member in class A. Then, class A can access class B by

#include <B.h>   

But, how can I get the pointer of class A in class B and access the public member of class A?

I found some information about on the internet: a Cross-class. They said you can make it by setting the class B as a nested class in class A.

Do you have any other advice?

sorry.
myCode: as follow..

class A:

#ifndef A
#define A

#include "B.h"

class A
{
public:
    A() {
        b = new B(this);
    }

private:
    B* b;
};

#endif


#ifndef B
#define B

#include"A.h"

class B
{
public:
    B(A* parent = 0) {
        this->parent = parent;
    }

private:
    A* parent;
};

#endif
  • 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-28T18:26:14+00:00Added an answer on May 28, 2026 at 6:26 pm

    Just use forward declaration. Like:

    A.h:

    #ifndef A_h
    #define A_h
    
    class B; // B forward-declaration
    
    class A // A definition
    {
    public:
        B * pb; // legal, we don't need B's definition to declare a pointer to B 
        B b;    // illegal! B is an incomplete type here
        void method();
    };
    
    #endif
    

    B.h:

    #ifndef B_h
    #define B_h
    
    #include "A.h" // including definition of A
    
    class B // definition of B
    {
    public:
        A * pa; // legal, pointer is always a pointer
        A a;    // legal too, since we've included A's *definition* already
        void method();
    };
    
    #endif
    

    A.cpp

    #inlude "A.h"
    #incude "B.h"
    
    A::method()
    {
        pb->method(); // we've included the definition of B already,
                      // and now we can access its members via the pointer.
    }
    

    B.cpp

    #inlude "A.h"
    #incude "B.h"
    
    B::method()
    {
        pa->method(); // we've included the definition of A already
        a.method();   // ...or like this, if we want B to own an instance of A,
                      // rather than just refer to it by a pointer.
    }
    

    Knowing that B is a class is enough for compiler to define pointer to B, whatever B is. Of course, both .cpp files should include A.h and B.h to be able to access class members.

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

Sidebar

Related Questions

I have two classes class A { public string something { get; set; }
I have two classes: class Player { public string Id { set; get; }
I have two classes: class Foo{ public int FooId { get; set; } ...
I have two classes: class Customer { public string Name { get; set; }
I have two classes: class Foo { public Bar SomeBar { get; set; }
I have a two classes: public class Question { public IList<Answer> Answers { get;
I have these two classes public class Person { } public class Company {
Let's say we have these two classes: public class Base { public static int
greetngs, i have two classes: [Serializable] public class FireGridUnit { public GridUnit FireGridLocation {
I have two classes Address and Employee as follows: public class Address { public

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.