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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:05:01+00:00 2026-06-16T17:05:01+00:00

I want to initialize a member (of reference type) in one object to point

  • 0

I want to initialize a member (of reference type) in one object to point to a private member of another object (of a different type). I use friend to provide access to the private member. (Please bear with me for a bit, I explain further down why I need to do this.)

Below is a bare minimum version of the code I tried to get started with, and it obviously does not work. Now clearly, I am only trying to initialize the reference with this code Aries a(t.Leo);, but because this is done inside main(), it does not compile.

I went through a number of posts here and on the internet (on the topics of friend and reference), but could not figure out a way to solve it. How could I make this work, or what other approach could I try to bind the reference correctly?

class Aries;

class Taurus {
friend class Aries;
public:
  void setLeo(int inLeo) {
    Leo = inLeo;
  }

private:
  int Leo;
};

class Aries {
public:
  Aries(const int& inCancer) : Cancer(inCancer) {}

private:
  const int& Cancer;
};

int main() {
  Taurus t;
  Aries a(t.Leo);  // Intention: initialize (bind) the Cancer member with t's Leo.
}

Note I know enough C++ to understand why the above practice is considered questionable or bad.

I am working on a research project in a hardware simulation environment, where the testbench is written in C++. The aim of the project is to display the class (private) members as a waveform. The only reliable way of doing this is to make the “waveform maker” class as a friend of the testbench objects, and then have it “stalk” the private members (by creating a reference as above). The other alternative of providing getter methods in the testbench is impractical, since this project would be used with 20 years worth of legacy code.

  • 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-16T17:05:02+00:00Added an answer on June 16, 2026 at 5:05 pm

    I know you know that friend is questionable but sometimes necessary, but in this case, these classes don’t have to be friends.

    struct AriesBinder {
      AriesBinder(const int& c) : Cancer(c) {}
      const int& Cancer;
    };
    
    class Taurus {
    public:
      void setLeo(int inLeo) {
        Leo = inLeo;
      }
    
      AriesBinder bind() const { return AriesBinder(Leo); }
    
    private:
      int Leo;
    };
    
    class Aries {
    public:
      Aries(const int& inCancer) : Cancer(inCancer) {}
      Aries(AriesBinder b) : Cancer(b.Cancer) {}
    
    private:
      const int& Cancer;
    };
    
    int main() {
      Taurus t;
      Aries b(t.bind());  // Intention was made clear
    }
    

    This is especially useful if many objects like Taurus can be bound to Aries. Also, it decouples the both classes since now the don’t have to know each other so you may change the internal representation of Taurus.

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

Sidebar

Related Questions

I want to initialize a protected member in a derived class using the constructor,
Window c# Desktop Application I want to initialize a DataRow object with the selected
Say I have an object of type A having Initialize() method. The method receives
I am trying to pass a member reference to a parameter of another method.
I want to have a class with a private static data member: class C
I want to initialize a property of a class that holds a reference to
Is it possible to initialize a reference member to NULL in c++? I'm trying
I want to initialize a static Class variable in Java: public class NumberExpression {
I want to initialize a couple of variables on Excel Dna .dll gets loaded
I want to initialize an array and then initialize a pointer to that array.

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.