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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:40:11+00:00 2026-05-14T03:40:11+00:00

Say, i have a function which returns a reference and i want to make

  • 0

Say, i have a function which returns a reference and i want to make sure that the caller only gets it as a reference and should not receive it as a copy.
Is this possible in C++?

In order to be more clear. I have a class like this.

class A
{
private:
    std::vector<int>  m_value;
    A(A& a){ m_value = a.m_value; }

public:
    A() {}
    std::vector<int>& get_value() { return m_value; }
};

int main()
{
    A a;
    std::vector<int> x = a.get_value();
    x.push_back(-1);
    std::vector<int>& y = a.get_value();
    std::cout << y.size();

    return 0;
}

Thanks,
Gokul.

  • 1 1 Answer
  • 2 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-14T03:40:11+00:00Added an answer on May 14, 2026 at 3:40 am

    You can do what you want for your own classes by making the class non copyable.

    You can make an class non copyable by putting the copy constructor and operator= as private or protected members.

    class C
    {
    private:
      C(const C& other); 
      const C& operator=(const C&);
    
    };
    

    There is a good example of making a NonCopyable class here that you can derive from for your own types.

    If you are using boost you can also use boost::noncopyable.

    Alt solution:

    Another solution is to have a void return type and make the caller pass their variable by reference. That way no copy will be made as you’re getting a reference to the caller’s object.

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

Sidebar

Related Questions

I have a temporary table (or, say, a function which returns a table of
Say I have a function foo that I want to call n times. In
Say I have some function fn1() in Erlang which returns {ok, Result} if the
Lets say I have a function which takes some input structure and returns some
I'm sometimes confused to using which one of them, say I have a function
Say I have a decorator which causes the function to print out any exceptions
I have a parent component, say P, which has function, say Pfunc. I also
Say I have a function that does the following in Vb.net For i as
Say I have a function func(i) that creates an object for an integer i,
Suppose there exists a function which returns a message say of the following format:

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.