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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:49:47+00:00 2026-06-16T22:49:47+00:00

This is probably a simple question, but I’m stuck on it. I’m trying to

  • 0

This is probably a simple question, but I’m stuck on it. I’m trying to pass an object down from ObjectA to ObjectB (which is a member of ObjectA) via it’s constructor. However, instead of passing by value, I want to pass only a const reference and store that reference indefinitely. The problem is that I’m not sure how to do it.

I can get this working with pointers like this:

class ClassB
{
private:
    int *ptrInternalX;
public:
    ClassB( int *tempX );
}

ClassB::ClassB( int *tempX )
{
    ptrInternalX = tempX
}

This way, an object is created and passed a pointer to an int, and that pointer is stored inside the class for later use.

However, pointers make me worry about memory leaks and other issues when using larger objects, so I’d like to try to do something like this using ‘constant references’ (const &). However, this doesn’t seem to work…

class ClassB
{
private:
    int &internalX;
public:
    ClassB( const int &tempX );
}

ClassB::ClassB( const int &tempX )
{
    internalX = tempX
}

I know that references are essentially an ‘alias’ for an existing variable (a different name that refers to the same memory address), and they need to be initialized immediately using an existing variable. So this creates an error in my program!

Is this even possible? Or is there a better/more clear way of doing something like this? The reasons I want to use constant references are the speed of passing just a reference instead of a large object while keeping the data safe from accidental changes and memory leaks… I’m sure that there is a simple and straight-forward way to do this but I’m not very familiar with const reference passing.

  • 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-16T22:49:48+00:00Added an answer on June 16, 2026 at 10:49 pm
    class    ClassB
    {
        private:
            const int&    internalX;
        public:
            ClassB(const int& tempX);
    }
    
    ClassB::ClassB(const int& tempX):
         internalX(tempX)
    {
    }
    

    As you said, a reference has to be initialized immediately. Thus, if you want your reference to be a class member, you have to use your constructor’s initialization list to set it.

    (This short explanation might also make things clearer for you, as it is specifically centered on the situation you’ve just met)

    Good luck

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

Sidebar

Related Questions

I know this is probably a simple question, but I'm still trying to figure
I know this is probably a pretty simple question, but i am trying to
This is probably a simple question but I'm having a hard time trying to
This is probably a simple question, but I'm trying to lookup the constant name
This is probably a simple question but I can't seem to figure out how
This is probably a simple question but I am developing a web app in
This is probably a very simple question but I'm a little confused how this
This is probably a really simple question but one I've never quite worked out
I know this is probably a very simple question but how would I do
This is probably a simple question, but i have been unable to find 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.