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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:07:28+00:00 2026-06-14T19:07:28+00:00

I am somehow aware of returning by reference by this got me confused. I

  • 0

I am somehow aware of returning by reference by this got me confused.

I have a function which returns a reference to a public field of class BoardNode, std::vector<BoardNode> _neighboursVector.

I also have a class Board which holds a std::vector<BoardNode>.

my member function goes like this:

const std::vector<BoardNode>& 
Board::getNeighboursVector(unsigned int x, unsigned int y) const
{
    BoardNode node = this->getBoardNode(x, y);
    //...
    node._neighboursVector.push_back(...);
    //...
    return node._neighboursVector;
}

While debugging on return line I get the correct values in the vector but outside of this function I get empty vector. Why ?

std::vector<BoardNode> v = b.getNeighboursVector(5,5);

EDIT

getBoardNode definitions

const BoardNode & Board::getBoardNode(unsigned int rowIdx, unsigned int colIdx) const
{
//...
}

BoardNode & Board::getBoardNode(unsigned int rowIdx, unsigned int colIdx)
{
//...
}
  • 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-14T19:07:29+00:00Added an answer on June 14, 2026 at 7:07 pm

    node is a local object. And by extension, node._neighborsVector is also a local object. As a local object, it gets destroyed at the end of the function. So you are returning a reference to a destroyed object. That’s undefined behavior.

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

Sidebar

Related Questions

I have a function in R that looks somewhat like this: setMethod('[', signature(x=stack),definition=function(x,i,j,drop){ new('class',
I am aware that System.Threading.Timer exists, but I already have a Thread. This thread
I must have missed this somehow... In my app user x owns posts 1,
I'm aware of this question which mentions Boost's STATIC WARNING, but I'd like to
Somehow this doesn't work... var paper = Raphael(test, 500, 500); var testpath = paper.path('M100
Somehow my master and my origin/master branch have diverged. I actually don't want them
Edit: I'm well aware of that this works very well with value types, my
Having just written the title for this question I am aware of how dodgy
Is it somehow possible to accesse a sesssion-scoped bean in a class extending the
Can I somehow get a reference to the instance I am creating using object

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.