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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:25:49+00:00 2026-05-17T06:25:49+00:00

This is a noobie question, but I’m not sure how to pass by reference

  • 0

This is a noobie question, but I’m not sure how to pass by reference in C++. I have the following class which sets up a Node and a few functions.

class Node
{
  public:
    Node *next;
    int data;
    Node(int dat)
    {
      next = NULL;
      data = dat;
    }
    Node* getNext()
    { return next; }
    void setNext(Node *n)
    { next = n;}

    void reverse(Node *root)
    {
      Node *previous = NULL;
      while(root != NULL)
      {
        Node *next = root->getNext();
        root->setNext(previous);
        previous = root;
        root = next;
      }
      root = previous;
    }
};

Now, the purpose of my little class is to create a singularly linked list and have the ability to reverse it. And it seems to work fine, if I return the node named ‘previous’ at the end of reverse.

But look at my main function:

int main()
{
  Node *root = new Node(1);
  Node *num2 = new Node(2);
  Node *num3 = new Node(3);
  Node *num4 = new Node(4);

  root->setNext(num2);
  num2->setNext(num3);
  num3->setNext(num4);
  root->printList();
  root->reverse(root);
  root->printList();

  return 0;
}

printList() was omitted for sake of space, but it just prints a list given a node. The problem is, when root->reverse(root) is called, root doesn’t actually end up pointing to ‘previous’.

The output would be this:

1
2
3
4
  // the value of previous from the reverse function is 4
1

I really don’t understand the output. Anyone care to explain what’s happening? (Why isn’t the list reversed even though if I did something like this root = root->reverse(root) where reverse returns previous, it would) why is it that root now only points to itself? I’m new to c++ and appreciate your help!

  • 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-17T06:25:50+00:00Added an answer on May 17, 2026 at 6:25 am

    C++ has support for reference semantics. Therefore, for a given function:

    void foo(Bar& bar);
    

    To pass by reference you do:

    int main() {
      Bar whatsit;
    
      foo(whatsit);
    
      return 0;
    }
    

    That’s it!

    This is commonly confused with passing a pointer, where for a function such as:

    void foo(Bar* bar);
    

    You would do:

    int main() {
      Bar whatisit;
    
      foo(&whatsit);
    
      return 0;
    }
    

    The difference is mostly a matter of semantics:
    – A reference is always valid. There is no reason to check for a NULL pointer.
    – A pointer could be NULL, and as such, should be checked.

    It is, however, possible for a reference to refer to a NULL pointer, however, if the programmer decides to be evil and abuse reference semantics, but the principle remains.

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

Sidebar

Related Questions

This may be a very noobie question, but in today's world of web app
This might seem like a stupid question I admit. But I'm in a small
This is a difficult and open-ended question I know, but I thought I'd throw
excuse what seems like a real noobie question but how can I implement the
This is a bit of a long shot, but if anyone can figure it
This is kinda oddball, but I was poking around with the GNU assembler today
Total noobie question here. I'm just learning Java, and studying passing arguments to functions.
I've looked at the routing on StackOverflow and I've got a very noobie question,
Thanks for your help on my last noobie jquery question and since you done
This is starting to vex me. I recently decided to clear out my FTP,

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.