如何将方法结果作为参数传递给C ++中的基类构造函数? [英] How to pass method result as parameter to base class constructor in C++?

查看:232
本文介绍了如何将方法结果作为参数传递给C ++中的基类构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图实现这样的:

class Base
{
  public:

  Base(string S) 
  {
  ...
  };
}

class Derived: Base
{
public:
  int foo;
  string bar()
  {
    return stringof(foo); // actually, something more complex
  };

  Derived(int f) : foo(f), Base(bar()) 
  {
  };
}

现在,这不工作,因为bar在foo初始化之前在Derived构造函数中调用。

Now, this doesn't work as I want, because bar() is called in the Derived constructor before foo is initialized.

我考虑添加一个类似于bar()的静态函数,它使用foo作为参数,并在初始化列表中使用它,但我想问我是否有任何其他可以用来挖掘自己的技术...

I considered adding a static function similar to bar() which takes foo as a parameter - and using that in the initialization list, but thought I'd ask if there were any other techniques that could be used to dig myself out of this one...

编辑:感谢您的反馈 - 以处理静态函数。不确定静态和非静态函数之间的重载是否过于聪明,但... ...

Edit: Thanks for feedback - here's how I was going to handle the static function. Not sure if the overload between a static and non-static function is too clever, but...

class Derived: Base
{
public:
  int foo;

  static string bar(int f)
  {
    return stringof(f); // actually, something more complex
  }

  string bar()
  {
    return bar(foo); 
  };

  Derived(int f) :  Base(bar(f)) , foo(f)
  {
  };
}


推荐答案

静态类方法或常规函数)以foo 作为参数并返回字符串是一个很好的解决方案。您可以从Derived :: bar调用此相同的函数,以防止代码重复。所以,你的构造函数应该是这样的:

Yes, using a function (static class method or regular function) that takes foo as a parameter and returns a string is a good solution. You can call this same function from Derived::bar to prevent code duplication. So, your constructor would look like this:

Derived(int f) : Base(stringof(f)), foo(f) {}

我在列表中首先调用Base构造函数,以强调发生初始化。初始化器列表的顺序没有效果,因为所有的类成员都按它们在类体中声明的顺序初始化。

I place the call to the Base constructor first in the list to emphasize the order in which the initializations occur. The ordering of the initializer list has no effect as all class members are initialized in the order that they are declared in the class body.

这是一个很干净,功能的方法来解决问题。但是,如果您仍然想衡量替代方案,请考虑使用组合,而不是继承关系在Derived和Base类之间:

This is a very clean, functional approach to the problem. However, if you still would like to weigh alternatives then consider using composition instead of inheritance for the relationship between the Derived and Base classes:

class Base {
public:
    Base(string S) {  ...  }
    void bat() { ... }
};

class Derived {
    Base *base;
    int foo;

public:
    Derived(int f) : base(NULL), foo(f) {
        base = new Base(bar());
    }
    ~Derived() {
        delete base;
    }

    string bar() {
        return stringof(foo); // actually, something more complex
    }

    void bat() {
        base->bat();
    }
};

您需要考虑职业和利益为您的具体情况。使用Derived保存对Base的引用,您可以更好地控制初始化顺序。

You will need to consider the pros and cons for your specific situation. With Derived holding a reference to Base you gain greater control over the initialization order.

这篇关于如何将方法结果作为参数传递给C ++中的基类构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆