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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:55:12+00:00 2026-05-17T00:55:12+00:00

Why this can’t compile: // RefToPointers.cpp : Defines the entry point for the console

  • 0

Why this can’t compile:

// RefToPointers.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>

using std::cout;

class T
{
public:
    T(int* value):data_(value)
    {
    }
    int* data_;
    int* getData_()
    {
        return data_;
    }
    int getValue()//<----------Here I do not return by ref
    {
        return *data_;
    }
};



  void fnc(const int*& left, const int*& right )//<------Doesn't work even though  
//it is identical to the example below just type is different. Why?
    {
        const int* tmp = left;
        left = right;
        right = tmp;
}

void fnc(const int& left,const int& right)//<---Here I pass by ref
{

}

int _tmain(int argc, _TCHAR* argv[])
{
    //int* one = new int(1);
    //int* two = new int(2);
    //cout << "Pointers before change:" << *one << '\t' << *two << '\n';
    //fnc(one,two);
    //cout << "Pointers before change:" << *one << '\t' << *two << '\n';

    T one(new int(1));


        T two(new int(2));
            fnc(one.getData_(),two.getData_());//<---This do not work
            fnc(one.getValue(),two.getValue());//<<------This still works even thoug I'm   
    //returning by value and fnc is taking args by ref. Why does it work with int
 //by not with int*?
            return 0;
    }

I’m getting following error:

_error C2664: ‘fnc’ : cannot convert parameter 1 from ‘int *’ to ‘int *&_

And why on earth underscores do not make font italic in line where error is listed?

  • 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-17T00:55:13+00:00Added an answer on May 17, 2026 at 12:55 am

    getData() returns an rvalue. You can’t take a reference to an rvalue pointer.

    You have two choices:

    1) Pass fnc() lvalues:

    int* lhs = one.getData_();
    int* rhs = two.getData_();
    fnc(lhs, rhs);
    

    2) Or, since you are passing pointer references, which really are the same size as pointers themselves, why not just pass pointers?

    void fnc(int* left, int* right )
    {
        int* tmp = left;
        left = right;
        right = tmp;
    }
    

    EDIT:

    A little bit more on lvalues and rvalues. “lvalue” used to mean “an expression that can be on the left of an = operation.” rvalue was effectively the converse: an rvalue was any expression that could not be on the left of an = operation.

    Things are a little bit more complex than that now, but that’s still a pretty good way to understand lvalues and rvalues.

    Now, consider the following code:

    int val()
    {
        return 42;
    }
    
    int main()
    {
        int* p = &val();
    }
    

    val() returns an int by value — in other words, it returns an unnamed temporary value. Should you be able to take the address of that temporary?

    You might think, “well, yeah, why not?” But the answer is actually no, you can’t take the address of the temporary. The reason has to do with the lifetime of that temporary. It’s scope is limited to the expression in which it was created. In other words: val(). As soon as val() has been completely evaluated, the temporary ceases to exist. In effect, it falls off the stack. By the time int* p = & is evaluated, the temporary is long gone. There’s nothing left to take the address of.

    This is basically why you can’t take the address of an rvalue, which by extension is why you can’t get a reference to an rvalue’s address.

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

Sidebar

Related Questions

This can't compile: void Foo() { using (var db = new BarDataContext(ConnectionString)) { //
This can certainly be done using a simple script and reading the database. I
Using this: Can I use Facebook's fb:friend-selector in an iframe? I created a multi-friend
I know this can be done and i have seen it done using some
I know something like this can be accomplished easily using PHP or Perl, but
I read how this can be made to work using forward declarations. class A
I asked this Can I automate creating a .NET web application in IIS? a
I know this can be easily done using PHP's parse_url and parse_str functions: $subject
This can be done in Javascript with isNAN instead of !isset. Using the example
This can be done using a converter wrapper: import scala.collection.JavaConversions._ object ListConverter { def

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.