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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:25:32+00:00 2026-05-12T05:25:32+00:00

this snippet of code is given me headache. Personally, I would like to use

  • 0

this snippet of code is given me headache. Personally, I would like to use reference as they are neater compared to pointer, so I tried this:

include "SomeClass.h"

class FooBar 
{
   private:
     SomeClass& member_;

   public:
     FooBar() : member_(SomeClass()) { };
}

I have read that you need to assign a temp variable to a class member reference, so in this case I create a dummy SomeClass() (I’m not sure if I am doing it right here. I tried it with and without specifying a default constructor). However, it does not compile in VS 2005, saying that member_ cannot be initialised.

How should I be doing this?
Thanks in advance!

  • 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-12T05:25:32+00:00Added an answer on May 12, 2026 at 5:25 am

    1) References can not be changed to point to another object after it is initialized.

    Do you really need this behavior?

    2) When you initialize reference with temporary object, after this temp. object is out of scope your reference is invalid.

    That’s why your code is incorrect. And you have useless member now. I’d recommend to think about two alternatives

    a) Consider using pointer instead of reference.

    b) Change you constructor to something like this:

    MyClass(type & a):membmer_(a){...}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given this code snippet that can be readily pasted into Linqpad (or slightly modified
Given this code snippet, I know that if the user selects 25c the value
Given this snippet of code: funcs = [] for x in range(3): funcs.append(lambda: x)
Given this snippet of OCaml code: let rec range a b = if a
Given this simple code snippet I am astounded to provoke a stack overflow this
Given the following HTML code snippet; after finding the link by ID, how would
I am trying to convert the code snippet given in this http://www.movable-type.co.uk/scripts/latlong.html into java.
Given this snippet of code public abstract class Foo { private static SqlConnection _sqlConnection;
given this code snippet //Creates a list of List numbers List<List<Number>> num = new
This snippet of code always parses the date into the current timezone, and not

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.