简体   繁体   中英

Default argument for structure reference in C++

I want to give a default value to a function parameter, which is reference to a structure. What can I give as the default value?

First solution:

If it is a reference to a struct, then you've to make it const reference, and do this:

struct A
{
    //etc
    A(int, int);
};  

void f(int a, const A & = A(10,20) ) //const is necessary
{
    //etc
}

Its not that good for the obvious reasons: it makes the parameter const (you may not want it), and your struct needs to have constructor (you may not have it).

Second solution:

So if you don't want to make it const , or if you don't want to have a constructor in the struct, then you can do this:

struct Point
{
     int x, y, z;
};  

Point g_default_point = {10,20,30};
void g(int a, Point & p = g_default_point )
{
    //etc
}

Still not good. Defining a global variable is not a great idea.


Best solution: define an overload function

void g(int a, Point & p)
{
    //your code
}
void g(int a) //this function would behave as if you opt for default value!
{
     Point default_value = {10,20,30};
     g(a, default_value);
}

Now it doesn't require you to make the parameter const , neither does it force you to have constructor in your struct.

This works in C++0x:

#include <iostream>

struct structure { int x, y ; } ;

int f(int a, structure &s = structure{10,20}) {
  return s.x + s.y ;
}

int main() {
  std::cout << f (99) << std::endl ;
}

Note that s does not have to be const . See http://ideone.com/sikjf .

You can't do that.

All references must be initialized when declared. A reference is just another name of a variable. So you can't assign a default value to parameter which is reference to other variable.

If you must do that, you can use pointer instead. In that case you can assign 0 as default value to the parameter.

An object (which it can take a reference to), an existing reference. If your parameter happens to be a const reference, then an expression resulting in that type.

To illustrate:

struct X
{
    X operator+(const X&) const;
};

X x1;
X& x2;

void f(X& x = x1);
void f(X& x = x2);
void f(const X& x = x1 + x1);

You can do that if you passing object. int rowCount(const QModelIndex &parent = QModelIndex()) const; executes this way:

  • First step is evaluating of in-function expressions, where object constructor is called and reference initialized with it.

  • Second step is execution of function body, where object exists.

  • Third step is going to next function, objects created at first step runs out of scope, so their destructors called.

Even const is not nessesary here, but most compilers will give you warning that you initialize reference with temporary.

That solution won't work with types, which have no constructors, int test (int &i=int()); will fail (at least on VisualStudio 2008 it says error C2440: 'default argument': cannot convert from 'int' to 'int &' )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM