【问题标题】:C++ : call another method for default method argumentC++:为默认方法参数调用另一个方法
【发布时间】:2012-02-27 10:35:25
【问题描述】:

我在我的项目中使用我的 Settings 类的默认参数,因此我声明的方法要少一些。

例如,我声明了这些方法:

class Settings
{
    // [..]

    int getCurrentUserID(); // returns current user id

    // you specify the user id
    int setSetting( int value, int user_id ); 
    // no user specified, use the current one, overloads the previous when called
    // with only 1 argument
    int setSetting( int value ); 
}

我想要的是这个简化版:

class Settings
{
    // [..]

    int getCurrentUserID(); // returns current user id

    // automatically selects the current user if no ID is provided
    int setSetting( int value, int user_id = getCurrentUserID() ); 
}

但我在编译时收到此错误:

cannot call member function ‘int Settings::getCurrentUserID()’ without object

如何告诉编译器使用 Setting 对象的当前实例(可通过 this 获得)来获取默认值? 顺便说一句,这是授权的吗?

【问题讨论】:

  • “我怎么能……”——你不能。标准规定 this 不能在这种情况下隐式或显式使用(8.3.6/8、8.3.6/9)。
  • ltjax: 是的,我自己没找到……我怀疑这应该已经在 SO :)

标签: c++ dynamic methods default-value overloading


【解决方案1】:

你有两个不错的选择。首先,假设-1不是合法的user_id

class Settings
{
    // [..]

    int getCurrentUserID(); // returns current user id

    // automatically selects the current user if no ID is provided
    int setSetting( int value, int user_id = -1); 
};

int Settings::setSetting( int value, int user_id )
{
    if(user_id == -1) user_id = getCurrentUserID();
 ...

或者:

class Settings
{
    // [..]

    int getCurrentUserID(); // returns current user id

    int setSetting( int value ) { return setSetting(value, getCurrentUserID() ); }

    int setSetting( int value, int user_id ); 
};

【讨论】:

    【解决方案2】:

    方法getCurrentUserID()不是静态的,所以只能通过对象调用。一种选择是创建 2 个方法,如下所示,然后调用您需要的方法:

    int setSetting(int value);
    
    int setSetting(int value, int user_id);
    

    另一种选择:假设 ID 始终为正,您可以在方法上给出默认的负 ID,并在方法内部验证是否使用 ID 调用方法。像这样的:

    int setSetting(int value, int user_id = -1) {
        if (user_id == -1) {
            this->setSetting(value, this.getCurrentUserID());
            return 0; //don't know what the function returns, but handle it the way you need
        }
    
        // rest of the function will be called recursively, but it's OK
        // because the ID is not -1!
    }
    

    【讨论】:

    • 我认为将if (user_id == -1) user_id = getCurrentUserID()); 作为第一行然后继续该函数而不是调用自身会更好。如果 getCurrentUserID() 恰好是 -1,这也可以避免无限递归。
    • 好的,我会接受你的:它显示了原因(非静态函数)和解决方案!谢谢!
    【解决方案3】:
    1. 只有静态方法可以用作默认参数值。此示例有效(已验证):

      class Settings
      {
          // [..]
      
          static int getCurrentUserID(); // returns current user id
      
          // automatically selects the current user if no ID is provided
          int setSetting( int value, int user_id = getCurrentUserID() ); 
      };
      
    2. 应避免在 C++ 中使用默认参数值,以免代码复杂化。

    3. 重载方法是一种很好的做法。 我认为您的第一个示例看起来比另一个具有默认值的示例更好更简单。

    【讨论】:

    • 不幸的是,由于许多实现定义和未定义的行为,在 C++ 中编译不是验证。
    猜你喜欢
    • 1970-01-01
    • 2019-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多