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

  • Home
  • SEARCH
  • 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 528025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:56:39+00:00 2026-05-13T08:56:39+00:00

I am a C guy and I’m trying to understand some C++ code. I

  • 0

I am a C guy and I’m trying to understand some C++ code. I have the following function declaration:

int foo(const string &myname) {
  cout << "called foo for: " << myname << endl;
  return 0;
}

How does the function signature differ from the equivalent C:

int foo(const char *myname)

Is there a difference between using string *myname vs string &myname? What is the difference between & in C++ and * in C to indicate pointers?

Similarly:

const string &GetMethodName() { ... }

What is the & doing here? Is there some website that explains how & is used differently in C vs C++?

  • 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-13T08:56:39+00:00Added an answer on May 13, 2026 at 8:56 am

    The “&” denotes a reference instead of a pointer to an object (In your case a constant reference).

    The advantage of having a function such as

    foo(string const& myname) 
    

    over

    foo(string const* myname)
    

    is that in the former case you are guaranteed that myname is non-null, since C++ does not allow NULL references. Since you are passing by reference, the object is not copied, just like if you were passing a pointer.

    Your second example:

    const string &GetMethodName() { ... }
    

    Would allow you to return a constant reference to, for example, a member variable. This is useful if you do not wish a copy to be returned, and again be guaranteed that the value returned is non-null. As an example, the following allows you direct, read-only access:

    class A
    {
      public:
      int bar() const {return someValue;}
      //Big, expensive to copy class
    }
    
    class B
    {
    public:
     A const& getA() { return mA;}
    private:
     A mA;
    }
    void someFunction()
    {
     B b = B();
     //Access A, ability to call const functions on A
     //No need to check for null, since reference is guaranteed to be valid.
     int value = b.getA().bar(); 
    }
    

    You have to of course be careful to not return invalid references.
    Compilers will happily compile the following (depending on your warning level and how you treat warnings)

    int const& foo() 
    {
     int a;
    
     //This is very bad, returning reference to something on the stack. This will
     //crash at runtime.
     return a; 
    }
    

    Basically, it is your responsibility to ensure that whatever you are returning a reference to is actually valid.

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

Sidebar

Related Questions

Some guy showed me a piece of javascript code where there's a function called
I am a Java guy, and trying to implement some code in C#. I
I'm a C# guy trying to learn Java. I understand the syntax and the
i'm a .net guy doing some work/learning RoR. i have a handful of environment-specific
I have recently attended a presentation where guy showed that .NET obfuscated code can
I'm a php guy, but I have to do some small project in JSP.
Hey guy's I can't figure out why my code doesn't work when I have
Hi guy it's possible put in the application Badge some letter? Something like ON
I have an SEO guy that is confusing me. He mentioned that in Google
So there is the guy on the internet who wrote a ton of code

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.