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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:51:08+00:00 2026-06-14T08:51:08+00:00

I have a class called foo1 and in foo1.h I have class foo1 {

  • 0

I have a class called foo1 and in foo1.h I have

class foo1
{
public:
int var;
foo1();
};

There is also another class called foo2, in which I have an object based on foo1:

class foo2
{
public:
foo1 *afoo1;
foo2();
void func1(int,int);
};

My question is that how I can have access to afoo1->var. If I use afoo1->var in foo2.cpp, everything looks fine and it compiles with no error. But when I run it in the command prompt window, a window pups up and asks to close the program. I guess this is because I am violating memory access, the cause of which could be afoo1->var.

Can anyone help me with this?

Thanks

Extra Note

I make a dll file from foo2, and use it in another program, the source code of which I do not have any access to. Just to make things clarified a bit more.

Answer

I had made two mistakes. The first one was that I was referencing to Null. afoo1 = new foo1(); in the constructor of foo2 took care of that.

The second one was that I had not included foo1.cpp in my makefile. I know, stupid mistake.

  • 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-14T08:51:10+00:00Added an answer on June 14, 2026 at 8:51 am

    You need to do foo2Instance->afoo1->var, inside the foo2 methods this will just be afoo1->var. If that is crashing it’s more likely because you’re not initializing afoo1 in foo2’s constructor.

    #ifndef FOO1_H
    #define FOO1_H 
    
    class foo1
    {
    public:
    int var;
    foo1() { var = 0; } //make sure the constructor is in fact defined. 
    // if it isn't implemented it would cause that linker error.
    };
    
    #endif
    

    Make sure you have #include "foo1.h" in foo2.h

       // constructor and deconstructor, these are in the cpp file.
       foo2::foo2()
       {
            afoo1 = new foo1();
       }
       foo2::~foo2()
       {
            delete afoo1;
       }
    

    If you’re getting a NullReferenceException it’s likely because you’re missing that line in the constructor. afoo2->afoo1 is fine when afoo1 == NULL however, if you try to access a property of afoo1 you will crash every time. NULL->someProperty is never okay.

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

Sidebar

Related Questions

I have a class called Path for which there are defined about 10 methods,
I have a class called Product in my Business object and in another class
I have a class called Object which stores some data. I would like to
I have the following situation: class A { public: A(int whichFoo); int foo1(); int
I have a base class called Entity: public class Entity { public int Id
I have a class called Trial which has_many results. Now What I want to
I have a class called Account public class Account { public double balance =
I have a class called Metadata, which is declared within the namespace A::B::C ,
I have a class called Flamethrower which naturally has its own ammunition that is
I have a class called Property which has nothing but get -methods. All the

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.