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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:55:22+00:00 2026-05-30T15:55:22+00:00

hi i have faced an issue while accessing an object, in my program there

  • 0

hi i have faced an issue while accessing an object,

in my program there are 2 classes class A and B

class b has a member variable name, which kepts as private.and gettes/setter functions to access this variable(bcoz the variable is private).

in Class A, has a member variable , object of class B b(private).And i have used a getter to get this object outside the class.

now i want to set the name of object b using the object of class a.
so created the following code, but i didnot working.

please help me to solve this.

     // GetObject.cpp : Defines the entry point for the console application.
   //

   #include "stdafx.h"
  #include <iostream>

  class B
 {
   int name;
   public:
   int getname()
   {
     return name;
   }
   void SetName(int i)
   {
     name = i;
   }
  };
  class A
  {
   private:
   B b;
   public:

   B GetB()
   {
      return b;
   }

   };


   int _tmain(int argc, _TCHAR* argv[])
   {
   int ii = 10;
  A a;
  a.GetB().SetName(ii);
  std::cout<<" Value :"<<a.GetB().getname();
  getchar();
return 0;
  }
  • 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-30T15:55:23+00:00Added an answer on May 30, 2026 at 3:55 pm

    You need to return the member by reference (or pointer):

    B& GetB()
    {
       return b;
    }
    //or
    B* GetB() //i'd prefer return by reference
    {
       return &b; 
    }
    

    The way you have it now, you’re returning a copy of the object.

    So B A::GetB() doesn’t return the original object. Any changes you make on it will not affect the member of a. If your return by reference, a copy is not created. You would be returning the exact B object that is a member of a.

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

Sidebar

Related Questions

I'm wondering if anyone has faced this issue. Let's assume I have two tables:
I need to convert several Java classes to C#, but I have faced few
While implementing a flash-based uploader , we were faced with an issue: Flash doesn't
While developing for multiple sets of browsers, what issues have you faced while development
Good day everyone, i have faced with such an issue as linkage error like
I have an issue while parsing my Solr Queries that I pass on the
I have faced this problem quite often during the last couple of months, during
I have a problem that I have not faced before: It seems that the
We have recently been faced with the problem of porting our C++ framework to
I have designed a custom section handler before but I'm faced with a problem

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.