What does do in a C declaration

0 votes

As a C person, I'm attempting to comprehend some C++ code. 

The declaration of my function is as follows:

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() { ... }

Why are the and here? 

Is there a webpage that outlines the differences between how & is used in C and C++?

Aug 17, 2022 in C++ by Nicholas
• 7,760 points
889 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

Instead of indicating a pointer to an object, the "&" indicates a reference (In your case a constant reference).

The benefit of including a feature like

foo(string const& myname) 

over

foo(string const* myname)

is that because C++ does not permit NULL references, you are assured that myname is not null in the first scenario. 

The object is not cloned because you are passing by reference, exactly like if you were passing a pointer.

Your second illustration

const string &GetMethodName() { ... }

would enable you to return a reference to a member variable, for instance, that is constant. 

This is helpful if you want to ensure that the value returned is non-null but do not want a copy to be returned. 

For illustration, you have immediate, read-only access to the following:

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(); 
}

Naturally, you must use caution to avoid returning faulty references. 

The following will be gladly compiled by compilers (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; 
}

In essence, it is your duty to confirm that the object you are returning a reference to is legitimate.

answered Aug 23, 2022 by Damon
• 4,960 points

edited Mar 5

Related Questions In C++

0 votes
1 answer

What does Tokens do and why they need to be created in C++ programming?

Tokenization is essential in determining what a programme does.  What Bjarne is referring to in respect to C++ code is how tokenization rules alter the meaning of a programme.  We need to know what the tokens are and how they are determined.  Specifically, how can we recognise a single token when it comes among other characters, and how should tokens be delimited if  there is ambiguity? Consider the prefix operators ++ and +, for example. Assume we have just one token + to deal with.  What does the following excerpt mean? int i = 1; ++i; Is the above going to apply unary + on i twice with + only? Or will it only increase it once? Naturally, it's vague.  We require an additional token, thus ++ is introduced as its own "word" in the language. But there is now another (though minor) issue.  What if the programmer just wants to use unary + twice without incrementing?  Rules for token processing are required.  So, if we discover that a white space is always used as a token separator, our programmer may write: int i ...READ MORE

answered Aug 2, 2022 in C++ by Damon
• 4,960 points
1,792 views
0 votes
1 answer

In C++, what is a virtual base class?

When employing multiple inheritance, virtual base classes are used to prevent several "instances" of a particular class from appearing in an inheritance hierarchy. Consider the following example: class A { public: void Foo() {} ...READ MORE

answered Jun 10, 2022 in C++