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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:26:41+00:00 2026-05-16T00:26:41+00:00

UPDATE: Let me clarify my files and reiterate my question: main.h #include other.h class

  • 0

UPDATE: Let me clarify my files and reiterate my question:

main.h

#include "other.h"

class MyClass
{
    public:
    void MyMethod();
    void AnotherMethod();

    OtherClass test;
}

main.cpp

#include "main.h"

void MyClass::MyMethod()
{
    OtherClass otherTemp;     // <--- instantitate OtherClass object
    otherTemp.OtherMethod();  // <--- in this method here, is where I want to also call MyClass::AnotherMethod()
}

void MyClass::AnotherMethod()
{
    // Some code
}

other.h

class OtherClass
{
    public:
    void OtherMethod();
}

other.cpp

#include "other.h"
void OtherClass::OtherMethod()
{
    // !!!! This is where I want to access MyClass::AnotherMethod(). How do I do it?
    // I can't just instantiate another MyClass object can I? Because if you look above in 
    // main.cpp, this method (OtherClass::OtherMethod()) was called from within
    // MyClass::MyMethod() already.
}

So basically what I want is this: you instantiate object A, which in turn instantiates object B, which in turn calls a method that is from object A. I’m sure something like this is possible, but it might just be poor design on my part. Any direction would be greatly appreciated.

  • 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-16T00:26:42+00:00Added an answer on May 16, 2026 at 12:26 am

    Some do one .h/.cpp per class:

    my.h

    #ifndef MY_H
    #define MY_H
    #include "other.h"
    class MyClass
    {
        public:
        void MyMethod();
    
        OtherClass test;
    }
    #endif // MY_H
    

    other.h

    #ifndef OTHER_H
    #define OTHER_H
    class OtherClass
    {
        public:
        void Othermethod();
    }
    #endif // OTHER_H
    

    my.cpp

    #include "my.h"
    void MyClass::MyMethod() { }
    

    other.cpp

    #include "other.h"
    #include "my.h"
    void OtherClass::OtherMethod)()
    {
       // (ab)using MyClass...
    }
    

    If you’re using only Visual Studio, you could use #pragma once instead of #ifndef xx_h #define xx_h #endif // xx_h.
    EDIT: as the comment says, and also the related Wikipedia page, #pragma once is also supported (at least) by GCC.

    UPDATE:
    About the updated question, unrelated to #include, but more about passing objects around…

    MyClass already has an embedded instance of OtherClass, test. So, in MyMethod, it’s probably more like:

    void MyClass::MyMethod()
    {
        test.OtherMethod();
    }
    

    And if OtherMethod needs to access the MyClass instance, pass this instance to OtherMethod either as a reference, or a pointer:

    By reference

    class OtherClass { public: void OtherMethod(MyClass &parent); }
    
    void MyClass::MyMethod() { test.OtherMethod(*this); }
    
    void OtherClass::OtherMethod(MyClass &parent)
    {
       parent.AnotherMethod();
    }
    

    By Pointer

    class OtherClass { public: void OtherMethod(MyClass *parent); }
    
    void MyClass::MyMethod() { test.OtherMethod(this); }
    
    void OtherClass::OtherMethod(MyClass *parent)
    {
       if (parent == NULL) return;  // or any other kind of assert
       parent->AnotherMethod();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Realize this may sound like a broad question - so let me clarify. I
How do you guys update let's say for example a FormView with an ObjectDataSource
I want to update a VSTO addin for word I create without let VSTO
Let's assume I browse a specific web page that uses JavaScript to update its
Let say I have a screen name ITEM thats performs different actions: Save, Update,
I have a table, let's call is [MYTABLE] , with an FOR INSERT, UPDATE
I'm rather new with AS3, so forgive me if my question appears stupid. Let
I'm trying to update my app for iOS 6, and the main challenge is
In C++, assume following class hierarchy: class BaseClass { }; class ChildClass : public
Here's an attempt to clarify the question... I previously used Linq2Sql with all data

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.