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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:02:42+00:00 2026-06-10T10:02:42+00:00

Suppose I have the following function: void myFunc(P& first, P& last) { std::cout <<

  • 0

Suppose I have the following function:

void myFunc(P& first, P& last) {
    std::cout << first.child.grandchild[2] << endl;
    // ...
}

Now, let’s assume that first.child.grandchild[2] is too long for my purposes. For example, suppose it will appear frequently in equations inside myFunc(P&,P&). So, I’d like to create some sort of symbolic reference inside the function so that my equations would be less messy. How could I do this?

In particular, consider the code below. I need to know what statement I could insert so that not only would the output from line_1a always be the same as the output from line_1b, but also so that the output from line_2a would always be the same as the output from line_2b. In other words, I don’t want a copy of the value of first.child.grandchild, but a reference or symbolic link to the object first.child.grandchild.

void myFunc(P& first, P& last) {
    // INSERT STATEMENT HERE TO DEFINE "g"

    std::cout << first.child.grandchild[2] << endl; // line_1a
    std::cout << g[2] << endl;                      // line_1b

    g[4] = X; // where X is an in-scope object of matching type

    std::cout << first.child.grandchild[4] << endl; // line_2a
    std::cout << g[4] << endl;                      // line_2b
    //...
}    
  • 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-10T10:02:44+00:00Added an answer on June 10, 2026 at 10:02 am

    Say that the type of grandchild is T and size is N; then below is the way to create a reference for an array.

    void myFunc(P& first, P& last) {
      T (&g)[N] = first.child.grandchild;
      ...
    }
    

    I would not prefer pointer here, though it’s also a possible way. Because, the static size of array is helpful to a static analyzer for range checking.

    If you are using C++11 compiler then auto is the best way (mentioned by @SethCarnegie already):

    auto &g = first.child.grandchild;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

suppose we have following function: void someFunction(int * araye){ for (int i=0;i<5;i++) cout <<araye[i]<<'
Suppose I have the following function: void foo(std::vector<int> vec, int n); If I call
Suppose I have a function void myFun(int*) In C++ what exactly does the following
Suppose you have the following function foo . When I'm running a for loop,
Suppose I have the following code function myFunction(param, callback) { ... if (err) {
For example suppose I have the following two functions function a(param1) { console.log(param1); }
Suppose I have the following (shortened for simplicity): jQuery(#grid).jqGrid({ ... ondblClickRow: function() { //
For example suppose I have the following app.get('/', function(req, res) { var ip; if(req.headers['x-forwarded-for']){
Suppose I have the following struct and function returning a pointer: typedef struct {
Suppose I have the following: typedef struct { int itemSize; int count; void *list;

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.