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

  • Home
  • SEARCH
  • 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 807893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:28:30+00:00 2026-05-15T00:28:30+00:00

I’m having some difficulties with this problem. The main idea is, I initialized a

  • 0

I’m having some difficulties with this problem.

The main idea is, I initialized a variable of class type B in class A, class A.h has the variable Z declared as public, like B *Z;

In class A.cpp, I initialized it as Z = new B();

Now, I want to access that variable from class C and I’m unable to do so. C.h includes A.h and B.h

Here goes a bit of code:

Car.h

#include "Model.h"

class Car {
public:

    static Model *Z;

}

Car.cpp

#include "Car.h"

void Car::init() {
    Z = new Model();
}

Model.h

Class Model {}

Camera.h

#include "Model.h"
#include "Car.h"

class Camera {}

Camera.cpp

Camera::init() {
    Car::Z->getPos();
}
  • 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-15T00:28:31+00:00Added an answer on May 15, 2026 at 12:28 am

    I initialized a variable of class type
    B in class A

    #pragma once
    #include "B.h"
    
    class A
    {
       public:
    
       B* Z;
       A()
       { 
          Z = new B();
       }
    }
    

    B.h

    #pragma once
    class B
    { 
    }
    

    C.h

    #pragma once
    #include "A.h"
    
    class C
    { 
       A a; //here you construct A
       C() 
       { 
          a.Z = new B(); //you can read/write Z
       }
    }
    

    This should work! Be careful to include the #pragma once or a header guard/include guard (http://en.wikipedia.org/wiki/Header_file) so the headers won’t be included twice (if should have done it).

    There are 3 classes, car, model and
    camera. in car I declare a new model Z
    and I want the camera to follow that
    model so I’ll have to access the model
    positions in camera class

    • A = Car
    • B = Model
    • C = Camera
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.