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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T19:15:20+00:00 2026-06-18T19:15:20+00:00

I’m learning C++ (coming from Java) and this is bugging the hell out of

  • 0

I’m learning C++ (coming from Java) and this is bugging the hell out of me, say I have…

class Foo {

public:

    Bar &x; // <- This is a ref, Bar is not instantiated (this is just a placeholder)
    Bar *y; // <- This is a pointer, Bar is not instantiated (this is just another placeholder)
    Bar z;  // <- What is this???

};

class Bar {

public:

    Bar(int bunchOfCrap, int thatNeedsToBeHere, double toMakeABar);

};

In this example Bar has one constructor that needs a bunch of fields specified in order to create a “Bar.” Neither x, nor y creates a Bar, I understand that x creates a reference that can be pointed to a Bar and that y creates a pointer that can also represent a Bar.

What I don’t understand is what exactly z is.

  • Is it a Bar? And if so, how can it be given Bar’s only constructor?

  • Is it a Bar sized chunk of memory belonging to instances of Foo that can be initialized into a Bar?

  • Or is it something else?

Thanks!

  • 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-18T19:15:22+00:00Added an answer on June 18, 2026 at 7:15 pm

    For pedagogical reasons, let’s try to compile this code:

    class Bar {
    public:
        Bar(int bunchOfCrap, int thatNeedsToBeHere, double toMakeABar);
    };
    
    class Foo {
    public:
        Bar &x; // <- This is a ref, Bar is not instantiated (this is just a placeholder)
        Bar *y; // <- This is a pointer, Bar is not instantiated (this is just a *safer* placeholder)
        Bar z;  // <- What is this???
    };
    
    int main() {
        Foo foo;
    }
    

    Output:

    c++     uuu.cpp   -o uuu
    uuu.cpp:10:7: error: implicit default constructor for 'Foo' must explicitly initialize the reference member 'x'
    class Foo {
          ^
    uuu.cpp:14:10: note: declared here
        Bar &x; // <- This is a ref, Bar is not instantiated (this is just a placeholder)
             ^
    uuu.cpp:10:7: error: implicit default constructor for 'Foo' must explicitly initialize the member 'z' which does not
          have a default constructor
    class Foo {
          ^
    uuu.cpp:16:9: note: member is declared here
        Bar z;  // <- What is this???
            ^
    uuu.cpp:2:7: note: 'Bar' declared here
    class Bar {
          ^
    uuu.cpp:21:9: note: implicit default constructor for 'Foo' first required here
        Foo foo;
            ^
    2 errors generated.
    make: *** [uuu] Error 1
    

    As it says, you must initialize both the member reference Bar &x and the member variable Bar z;, because Bar doesn’t have a default constructor. y doesn’t have to be initialized, and it will default to NULL.

    Both x and y indirectly refer to the object. You can’t change what x refers to (so it must be initialized when Foo is instantiated). You can change what y refers to. z is a Bar-sized chunk of memory which lives inside Foo; for you to legally declare a member variable like this, you have to put the complete definition of Bar before Foo so that the compiler knows how big Bar is.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms
I have a text area in my form which accepts all possible characters from
I know there's a lot of other questions out there that deal with this
Does anyone know how can I replace this 2 symbol below from the string

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.