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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:23:26+00:00 2026-06-13T05:23:26+00:00

I’m trying to better understand the differences between pointers and references in C++. Coming

  • 0

I’m trying to better understand the differences between pointers and references in C++. Coming from a Java background, I was expecting references in C++ to be similar; I expected a pointer minus the pointer arithmetic. However, I have been very disappointed and, at times, confused. After some reading I thought I understood references to be pointers without pointer arithmetic and that can never be set to NULL. To test what I’ve learned I decided to start coding. However, I came across this problem and I do not understand why my code does not compile.

Here is what I was trying:

  3 void test(biNode*& bn)
  4 {
  5    string& s("another test");
  6    bn = new biNode(s);
  7    printf("Just Checking: %s\n", bn->getObject().c_str());
  8 }
  9
 10 int main()
 11 {
 12    biNode* bn;
 13    test(bn);
 14    printf("Just Checking: %s\n", bn->getObject().c_str());
 15 }

And here is the my ‘biNode’ header:

  1 #include <string>
  2 #include <iostream>
  3
  4 using namespace std;
  5
  6 class biNode
  7 {
  8    public:
  9       biNode(string& s);
 10       string& getObject();
 11    private:
 12       string& obj;
 13 };

With corresponding definitions:

  1 biNode::biNode(string& s) : obj(s)
  2 {}
  3 string& biNode::getObject()
  4 {
  5    return this->obj;
  6 }

Attempting to compile this produces the following error:

./test.cpp: In function `void test(biNode*&)':
./test.cpp:5: error: invalid initialization of non-const reference of type 'std::string&' from a temporary of type 'const char*'

I do not understand how ‘string& s(“another test”);’ is not valid. Can anyone explain this please?

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-06-13T05:23:28+00:00Added an answer on June 13, 2026 at 5:23 am

    You can’t initialize a non-const reference with anything but an existing object of the same type, which the reference will then alias. But your class biNode contains a reference member, too, and so you must only initialize a biNode instance with an object that exists for as least as long as the node instance itself!

    Here’s an example that demonstrates how you might use the biNode in a sane way:

    int main()
    {
        std::string s("Hello");
    
        for (int i = 0; i != 10; ++i)
        {
            biNode bn(s);
            // more stuff
        }
    }
    

    A sensible version of your test function might look like this:

    biNode test(std::string & s)
    {
        return biNode(s);
    }
    
    int main()
    {
        std::string s("World");
        auto bn = test(s);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group

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.