具有构造函数的C ++类成员 [英] C++ class member with constructor

查看:45
本文介绍了具有构造函数的C ++类成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这很可能是一个极端的新手问题,但这是我不太了解的问题.这可能很简单,但我对c ++的经验不足,正在尝试学习.

This is an extreme newbie question most likely but it's something I don't quite understand. It is probably something stupidly simple, but I don't have a lot of experience with c++ and am trying to learn.

我试图用我想要理解的东西的简化版本来解释它.

I've tried to explain it with a much simplified version of what I am trying to understand.

我有一堂课,叫做Foo.它的构造函数带有两个参数.

I have a class called Foo. It's constructor takes two arguments.

我有一个名为Bob的类,在其中我想要Foo的私有实例,但是,直到我进入Bob的构造函数之内,我才知道要传递给foo构造函数的参数.

I have a class called Bob in which I want a private instance of Foo, but, I won't know the arguments to pass to the foo constructor till I am inside the constructor of Bob.

class Foo
{
public:
    Foo(int a, int b);
    ~Foo();

    int getA();
    int getB();
};

class Bob
{
public:
   Bob();
    ~Bob();

    void DoSomethingHere();
private:
    Foo foo;
};

编译代码时出现错误.

error: no matching function for call to 'Foo::Foo()'

基于此,我猜想它正在尝试自动为foo调用构造函数,但不知道怎么做.在Bob类中的代码中,我想要做类似这样的事情.(bob.cpp)

Based on that, I guess that it is trying to automatically call the constructor for foo, but doesn't know how. In the code within the Bob class I was wanting then to do something like this. (bob.cpp)

Bob::Bob() {

    // do stuff to calculate the values of a and b

    int a = 12; int b = 23;

    // Initialize foo with the calculated values
}

鉴于我不知道要传递给它的参数,直到我进入Bob的代码中,我该如何处理?

Given that I won't know the arguments to pass to it until I am within the code in Bob, how do I handle that?

推荐答案

最新版本的C ++标准C ++ 11和C ++ 14具有多种功能,例如嵌套构造函数,可在此处使用..但是,有几种替代方法,您应该提供一个更详细的示例以说明要完成的工作.

There are various features in the latest revision of the C++ standard, C++11 and C++14, such as nested constructors, which can be used here. However, there are several alternative approaches, and you should give a more detailed example of what you're trying to accomplish.

我将提供一个使用嵌套构造函数的简单答案.假设 Foo 的构造函数的两个参数必须来自外部函数first_foo_parameter()和second_foo_parameter():

I'll provide a simplistic answer that makes use of nested constructors. Suppose that the two parameters to Foo's constructor must come from external functions, first_foo_parameter(), and second_foo_parameter():

extern int first_foo_parameter(), second_foo_parameter();

然后,默认构造函数可以像这样工作:

Then, the default constructor can work like this:

class Bob
{
public:
   Bob() : Bob(first_foo_parameter(), second_foo_parameter()) {}

    ~Bob();

    void DoSomethingHere();
private:

    Bob(int a, int b) : foo(a, b) {}

    Foo foo;
};

如果要以某种更复杂的方式派生私有类成员的构造函数参数,则可以沿同一行使用其他不同的技术.有时答案是重构类的层次结构,例如首先初始化的私有超类,其构造函数有空的手可以进行所有需要的计算,然后在需要构造子类的成员时,所有其构造函数都将被重新构造.这些参数在完全构造的超类中很容易获得.

If the private class member's constructor parameters are to be derived in some more complicated way, there are other different techniques that can be used along the same lines. Sometimes the answer is to refactor the class hierarchy, such as a private superclass that gets initialized first, whose constructor has a free hand to make all the needed calculations, then by the time the subclass's members need to be constructed, all of their constructors' parameters are readily available in the fully-constructed superclass.

答案实际上取决于具体细节.通用方法是使用嵌套的构造函数或私有超类来帮助确定所有成员构造函数的参数.

The answer really depends upon the specific details. The general approach is to use nested constructors, or private superclasses, to help in determining the parameters to all member constructors.

这篇关于具有构造函数的C ++类成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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