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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:02:27+00:00 2026-06-11T12:02:27+00:00

I have the following classes: Class called House (which have data regarding the house

  • 0

I have the following classes:

  • Class called House (which have data regarding the house structure)
  • Class called Room that is a composition of House.

So the problem comes when i want for example display the name of the house IN the room, so in order to do that i need to access House name from Room class which composition “has a” wont let me.

The workaround is to pass the house name by constructor to the room class, but that’s redundancy since i already have the data inside House class.

The other solution would be inherit House in Room which isn’t exactly “House IS A Room” and also i will loose the sugar syntax to make:

House.Room

So the question is: Is there are an standard workaround or re design pattern for this kind of problems?

Also here is an example:

class Room {
public:
  MyHouseName();       //Needs access to const char* name from House
};

class House {
   const char* name
   vector<Room> rooms;
};
  • 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-11T12:02:29+00:00Added an answer on June 11, 2026 at 12:02 pm

    Luchian’s answer is right, but I see what you are thinking, so let me reinforce his answer. You want your House’s name accessible in a Room object, but a Room shouldn’t have a House reference, because a Room compose a House, and should not have any control on a House, right? Lets supose:

    class House;
    class Room
    {
    public:
         string getMyHouseName(); //return m_myHouse->getName();
         void demolish(); //demolish the room
         //other methods...
    
    private:
         House* m_myHouse;
    };
    
    class House
    {
    public:
         string getName();
         void demolish(); //demolish the house, calling demolish() of all rooms in "m_rooms"
         //other methods...
    
    private:
         vector<Room*> m_rooms;
         const string m_name;
    };
    

    As you can see, you have access to the name of the House, and that’s ok, but you have also the power of demolish the house from a room! Ok, you know you shouldn’t use “m_myHouse->demolish()” inside Room class, but you can, and that’s a common mistake, providing more access to an object than the necessary. So the solution for this problem is use const methods and const objects. Let me show you:

    class House;
    class Room
    {
    public:
         string getMyHouseName(); //return m_myHouse->getName();
         void demolish(); //demolish the room
         //other methods...
    
    private:
         const House* m_myHouse;
    };
    
    class House
    {
    public:
         string getName() const;
         void demolish(); //demolish the house, calling demolish() of all rooms in "m_rooms"
         //other methods...
    
    private:
         vector<Room*> m_rooms;
         const string m_name;
    };
    

    Now, the “getName()” of House is const method, and that means that this method can be called from a const instance of House, because it doesn’t change the state of the object, you just read an information from House. And “demolish()” is not const, because it can change the state of a House (demolishing its rooms).

    So, now we put a const reference of House in a Room, a “read-only” object. Now, Room doesn’t have the power to change the House anymore. You can’t call m_myHouse->demolish() in Room, you haven’t that control, but House can demolish any room. That’s how House “has a” Room.

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

Sidebar

Related Questions

I have a base class called Component which has a number of classes derived
The goal is to have a singleton data controller class called FetchData.h/.m that pulls
Let's say you have a class called Customer, which contains the following fields: UserName
I have a number of classes and interface that are in the following structure
I have 2 classes with HasMany relationship, say a class called Parent that has
Consider the following problem: You have a class 'A' that serves as a base
I have the following classes: class Vigil < ActiveRecord::Base after_update :do_something_cool private def do_something_cool
Assume I have the following classes class Genre { static hasMany=[author:Author] } class Author{
Suppose, I have following classes: public class DisposableObj : IDisposable { public ChildObj CreateObj();
with C#, java or c++ ... we have the following classes class A {

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.