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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:40:13+00:00 2026-05-30T03:40:13+00:00

Right Ill try to explain my thinking here. I have two classes at the

  • 0

Right Ill try to explain my thinking here.
I have two classes at the moment, known as class1 and class2.
I want to pass an object of class2 into an object of class1, by reference.

but i want to only have to pass it in once, rather than having to pass it in every time a method is called.

I’ve tried creating a pointer and I have tried creating a reference, but to no avail.
Any help appreciated.

#include <iostream>

using namespace std;

class myclass
{
    private:
int i;

    public:
myclass()
{
}
void method()
{
    cout << "Enter num: ";
    cin >> i;
}
void display()
{
    cout << i;
}
};

class relatedclass
{

    public:

relatedclass(myclass ob)
{
    pmc = &ob;
}
myclass *pmc;

};

void main()
{
myclass mc;

relatedclass rc(mc);

//display value of mc.i
mc.display();
cout << endl;

//ok lets change the i variable
rc.pmc->method();
cout << endl;

//display new value of mc.i
mc.display();
cout << endl;
}

for the test date I entered 50, and i expected the mc object to be updated and i would now equal 50.

  • 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-30T03:40:15+00:00Added an answer on May 30, 2026 at 3:40 am
    struct class2 {
      int i, j;
    };
    struct class1 {
      class2& c2;
      class1(class2& c2) : c2(c2) {}
      void Froz() {
        c2.i = c2.j;
      }
      int Baz() {
        return c2.i * c2.j;
      }
    };
    

    EDIT: In your example, in the relatedclass constructor, you take the address of a local variable. That variable will be destroyed when the constructor returns. After that, your pmc pointer points to a destroyed object, which is a no-no.

    Change this line:

    relatedclass(myclass ob)
    

    to this

    relatedclass(myclass& ob)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Probably this isn't the right place... but ill give it a try. I want
Ill try and make this quick and easy...I hope, Right now I have a
I'll try to explain this as best as I can. I have a form
I have this simple problem. I'll try to explain with a sample code An
Right, I'll try and explain this the best I can. I'm using PHP COM
So I'll try to clearly explain my goal first: First, I want to query
I want to add a panel in android activity.I'll try explain its behaviour. It
I'll try and explain this the best I can. First of all, here is
Right now, I keep all of my projects on my laptop. I'm thinking that
Right, initially ran: c:\regsvr32 Amazing.dll then, (accidentally - I might add) I must have

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.