关于 c++ 的推导问题

发布于 2024-08-01 23:05:35 字数 686 浏览 11 评论 0原文

为什么我无法访问基类 A 的 B 类初始化列表中的成员?

   class A
    {
    public:
        explicit A(int a1):a(a1)
        {
        }
        explicit A()
        {
        }

    public:
        int a; 

    public:
        virtual int GetA()
        {
            return a;
        }
    };

    class B : public A
    {
    public:
        explicit B(int a1):a(a1) // wrong!, I have to write a = a1 in {}. or use A(a1)
        {
        }
        int GetA()
        {
            return a+1;
        }
    };

    class C : public A
    {
    public:
        explicit C(int a1):a(a1)
        {
        }
        int GetA()
        {
            return a-1;
        }
    };

Why I can't access base class A's a member in class B initialization list?

   class A
    {
    public:
        explicit A(int a1):a(a1)
        {
        }
        explicit A()
        {
        }

    public:
        int a; 

    public:
        virtual int GetA()
        {
            return a;
        }
    };

    class B : public A
    {
    public:
        explicit B(int a1):a(a1) // wrong!, I have to write a = a1 in {}. or use A(a1)
        {
        }
        int GetA()
        {
            return a+1;
        }
    };

    class C : public A
    {
    public:
        explicit C(int a1):a(a1)
        {
        }
        int GetA()
        {
            return a-1;
        }
    };

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

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

发布评论

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

评论(3

像你 2024-08-08 23:05:35

A 的构造函数在 B 的构造函数之前运行,并且隐式或显式地,前者构造了 A 的所有实例,包括 a 成员。 因此 B 不能在 a 上使用构造函数,因为该字段已经构造完毕。 您尝试使用的符号准确地表明在 a 上使用构造函数,而此时这是不可能的。

A's constructor runs before B's, and, implicitly or explicitly, the former construct all of A's instance, including the a member. Therefore B cannot use a constructor on a, because that field is already constructed. The notation you're trying to use indicates exactly to use a constructor on a, and at that point it's just impossible.

心凉怎暖 2024-08-08 23:05:35

要以 Alex 的答案为基础,您可以通过控制其构造来初始化基类的 "a" 成员,如下所示:

class B : public A
{
public:
    explicit B(int a1) : A(a1) { }  // This initializes your inherited "a"
    ...
};

请注意,我正在构造基类(大写 "A" ),而不是尝试直接初始化其继承的成员(小写“a”,取自您的示例)。

To build on Alex' answer, you can initialize the base class' "a" member by controlling its construction, like so:

class B : public A
{
public:
    explicit B(int a1) : A(a1) { }  // This initializes your inherited "a"
    ...
};

Note that I'm constructing the base class (capital "A") above, rather than attempting to directly initialize its inherited member (lowercase "a", drawing from your example).

同展鸳鸯锦 2024-08-08 23:05:35

为了进一步构建 pilcrow 的答案,您可以通过在 B 类中覆盖它来轻松地初始化 A 成员,就像您想要的那样:

class B : public A
{
public:
    int a; // override a

    explicit B(int a1) : a(a1) // works now
    {
    }

    ...
};

不过,我不一定推荐这样做;)

To build even further on pilcrow's answer, you could easily initialize the A member like you want by overriding it in your B class:

class B : public A
{
public:
    int a; // override a

    explicit B(int a1) : a(a1) // works now
    {
    }

    ...
};

Though, I wouldn't necessarily recommend this ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文