43933

Can I have a function/method passing an argument by reference and an overload passing it by value in

Question:

This question already has an answer here:

<ul><li> <a href="/questions/5465293/function-overloading-based-on-value-vs-const-reference" dir="ltr" rel="nofollow">Function Overloading Based on Value vs. Const Reference</a> <span class="question-originals-answer-count"> 5 answers </span> </li> </ul>

In C# this is certainly possible, as this compilable example can show:

static void Teste(int x) { } static void Teste(ref int x) { } static void Teste() { int i = 0; Teste(i); Teste(ref i); }

<br /> But can it be done in C++(/CLI) with a constructor? See the example below:

class Foo { Foo(int bar) { // initializing "Foo" instance... } Foo(int &bar) { // initializing "Foo" instance... } //... }

Although this class does compile with these constructors I can't see how to choose when I apply one are the other, that is, the call is ambiguos, as there is no keyword I know for this purpose as "ref" in C#. I tried it in a constructor, where the name must be the same as the class (of course I can add a useless parameter, but I want to know if I can <strong>not</strong> do it).

BTW, I googled and only got things like "what's the difference between passing by ref and by value?" but nothing covering overloading like this. And I guess that as workarounds I can use pointer, thanks to the "take the address of" (&); or have, as mentioned above, an extra useless parameter. But what I want to know is: can I have overloads like these (by ref/by value)?

Thanks in advance,

Answer1:

You can accomplish something similar in C++ by providing an explicit cast to the desired type.

struct Foo { Foo(int /*bar*/) {} Foo(int &/*bar*/) {} }; int main() { int value = 5; Foo foo(static_cast<const int&>(value)); return 0; }

The cast to const will cause overload resolution to ignore the constructor taking a non-const reference and will settle on passing by value.

Recommend

  • how to make my program to wait for a particular thread and also not affecting my GUI's interact
  • how C++ Implicitly convert c style string to a string object?
  • declaring device constant in terms of another constant
  • Why is new Number(8) not exactly equal to 8?
  • CakePHP 3: change order in dateWidget
  • Why I can't use uniform1f instead of uniform4f for setting a vec4 uniform?
  • Validation fired but Red Border does not appear with User Control in Silverlight 4
  • custom string delimiters stringtemplate-4
  • How to retrieve information from antrun back to maven?
  • SyntaxError: (irb):26: both block arg and actual block given
  • NUnit 3.0 TestCase const custom object arguments
  • How do I display a dialog that asks the user multi-choice questıon using tkInter?
  • Groovy: Unexpected token “:”
  • Meteor helpers not available in Angular template
  • Exception “firebase.functions() takes … no argument …” when specifying a region for a Cloud Function
  • Highlight one bar in a series in highcharts?
  • Scrapy recursive link crawler
  • How to rebase a series of branches?
  • Read text file and split every line in MSBuild
  • Align navbar back button on right side
  • Window Size for Mac application
  • Build own AppleScript numerical error handling
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Calling of Constructors in a Java
  • Circular dependency while pushing http interceptor
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • PHP: When would you need the self:: keyword?
  • Linker errors when using intrinsic function via function pointer
  • JTable with a ScrollPane misbehaving
  • Angular 2 constructor injection vs direct access
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Java static initializers and reflection
  • Android Google Maps API OnLocationChanged only called once
  • Qt: Run a script BEFORE make
  • unknown Exception android
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • java string with new operator and a literal
  • How to push additional view controllers onto NavigationController but keep the TabBar?