我如何从 C++ 的子 c-tor 中初始化超类参数?

发布于 2024-10-01 22:45:09 字数 424 浏览 6 评论 0原文

看下面的例子:

class A {
public:
    A(int param1, int param2, int param3) {
        // ...
    }
};

class B : public A {
public:
    B() : m_param1(1), m_param(2), m_param(3), A(m_param1, m_param2, m_param3) {
        // ...
    }
};

B b;

显然,当创建“b”时,A的ctor将在B的参数初始化之前被调用。

这条规则阻止我创建简化类初始化的“包装”类。

这样做的“正确方法”是什么?

谢谢, Amir

PS:在我的特定情况下,参数不是基元,这个例子只是帮助我解释自己。

Watch the following example:

class A {
public:
    A(int param1, int param2, int param3) {
        // ...
    }
};

class B : public A {
public:
    B() : m_param1(1), m_param(2), m_param(3), A(m_param1, m_param2, m_param3) {
        // ...
    }
};

B b;

Obviously, when "b" will be created, A's ctor will be called before the parameters of B will be initialized.

This rule prevents me from creating "wrapper" classes which simplify the class's initialization.

What is the "right way" for doing it?

Thanks,
Amir

PS: In my particular case, the parameters are not primitives, this example just helped me to explain myself.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小伙你站住 2024-10-08 22:45:09

只需调用 A 的构造函数即可:

class B : public A
{
public:
    B() : A(1 ,2, 3)
    {
    }; // eo ctor
}; // eo class B

编辑:

只需阅读您对原始帖子的评论即可。弄清楚这些事情很重要:)无论如何,如果您想在 B 中创建新数据,在 B 中跟踪它,然后将其传递给 A,这个答案仍然成立:

class Object
{
private:
    int i;
public:
    Object(int _i) : i(_i){};
};

class A
{
public:
    A(Object* _1, Object* _2, Object* _3)
    {
    };
};

class B : public A
{
private:
    Object* _1;
    Object* _2;
    Object* _3;

public:
    B() : A(_1 = new Object(1), _2 = new Object(2), _3 = new Object(3))
    {
    };
};

Just call A's constructor:

class B : public A
{
public:
    B() : A(1 ,2, 3)
    {
    }; // eo ctor
}; // eo class B

EDIT:

Just read your comment to your original post. It's important to be clear about these things :) Anyway, this answer still holds true if you want to create new data in B, track it in B, and pass it to A:

class Object
{
private:
    int i;
public:
    Object(int _i) : i(_i){};
};

class A
{
public:
    A(Object* _1, Object* _2, Object* _3)
    {
    };
};

class B : public A
{
private:
    Object* _1;
    Object* _2;
    Object* _3;

public:
    B() : A(_1 = new Object(1), _2 = new Object(2), _3 = new Object(3))
    {
    };
};
你怎么这么可爱啊 2024-10-08 22:45:09

“参数不是基元”。那么你有这样的东西吗?

class Param { /*...*/ };
class A {
public:
  A(const Param& param1, const Param& param2, const Param& param3);
};

class B : public A {
public:
  B();
private:
  Param m_param1;
  Param m_param2;
  Param m_param3;
};

并且您希望将 B 的成员传递给 A 的构造函数。这个怎么样?

class B_params {
protected:
  B_params(int v1, int v2, int v3);
  Param m_param1;
  Param m_param2;
  Param m_param3;
};
class B : private B_params, public A {
public:
  B();
};

B_params::B_params(int v1, int v2, int v3)
  : m_param1(v1), m_param2(v2), m_param3(v3) {}
B::B() : B_params(1,2,3), A(m_param1, m_param2, m_param3) {}

只需确保在 B 的继承类列表中 B_params 位于 A 之前。

"The parameters are not primitives". So you have something like this?

class Param { /*...*/ };
class A {
public:
  A(const Param& param1, const Param& param2, const Param& param3);
};

class B : public A {
public:
  B();
private:
  Param m_param1;
  Param m_param2;
  Param m_param3;
};

And you want to pass the members of B to the constructor of A. How about this?

class B_params {
protected:
  B_params(int v1, int v2, int v3);
  Param m_param1;
  Param m_param2;
  Param m_param3;
};
class B : private B_params, public A {
public:
  B();
};

B_params::B_params(int v1, int v2, int v3)
  : m_param1(v1), m_param2(v2), m_param3(v3) {}
B::B() : B_params(1,2,3), A(m_param1, m_param2, m_param3) {}

Just make sure B_params comes before A in the list of B's inherited classes.

自控 2024-10-08 22:45:09

不确定我是否明白你的问题。

如果您只想帮助您使用某些给定参数初始化 A,则应该使用具有默认值的 A 构造函数:

class A {
public:
    A(int param1 = 1, int param2 = 2, int param3 =3) {
        // ...
    }
};

Not sure I'm getting your question.

If you just want something that helps you to initialize A with some given parameters, you should use an A constructor with default values:

class A {
public:
    A(int param1 = 1, int param2 = 2, int param3 =3) {
        // ...
    }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文