友元函数调用派生类的静态成员。没有得到预期的输出

发布于 2024-12-28 11:18:39 字数 1443 浏览 4 评论 0原文

我的第一篇文章:)

我在使用以下 C++ 代码时遇到问题。我有一个 ABC 类 A,以及两个派生类 B 和 C。它们都有一个名为 id 的静态成员:

using std::cout;

class A
{
private:
    friend int bar(A& a);
    static const int id = 1;

    virtual void foo() = 0;
};

class B : public A
{
private :
    friend int bar(A& a);
    static const int id = 2;

    void foo() { /*Do something*/ }
};

class C : public A
{
private:
    friend int bar(A& a);
    static const int id = 3;

    void foo() { /*Do something*/ }
};

int bar(A& a)
{
    return a.id;
}

int main()
{
    B b;
    C c;

    cout << bar(b) << "\n";
    cout << bar(c) << "\n";

    return 0;
}

我期望这段代码打印出 2 和 3 - 相反,它打印出 1 和 1 (bar() 总是使用 A::id)。我做错了什么?有什么想法吗?


根据下面的评论,这是我使用的最终代码。它有效,但很想听到更多想法:)

#include <iostream>

using std::cout;

class A
{
private:
    virtual void foo() = 0;
};

class B : public A
{
private:
    template <typename T>
    friend int bar(T& t);

    static const int id = 2;
    void foo() { /*do something*/ }
};

class C : public A
{
private:
    template <typename T>
    friend int bar(T& t);

    static const int id = 3;
    void foo() { /*do something*/ }
};


template <typename T>
int bar(T& t)
{
    return t.id;
}


int main()
{
    B b;
    C c;

    cout << bar(b) << "\n";
    cout << bar(c) << "\n";

    return 0;
}

My first post here :)

I am having a problem with the following C++ code. I have an ABC class A, and two derived classes B and C. All of them have a static member called id:

using std::cout;

class A
{
private:
    friend int bar(A& a);
    static const int id = 1;

    virtual void foo() = 0;
};

class B : public A
{
private :
    friend int bar(A& a);
    static const int id = 2;

    void foo() { /*Do something*/ }
};

class C : public A
{
private:
    friend int bar(A& a);
    static const int id = 3;

    void foo() { /*Do something*/ }
};

int bar(A& a)
{
    return a.id;
}

int main()
{
    B b;
    C c;

    cout << bar(b) << "\n";
    cout << bar(c) << "\n";

    return 0;
}

I was expecting this code to print out 2 and 3 - rather it prints out 1 and 1 (bar() is always using A::id). What am I doing wrong? Any ideas?


Based on the comments below, this the final code I am using. It works, but would love to hear more thoughts :)

#include <iostream>

using std::cout;

class A
{
private:
    virtual void foo() = 0;
};

class B : public A
{
private:
    template <typename T>
    friend int bar(T& t);

    static const int id = 2;
    void foo() { /*do something*/ }
};

class C : public A
{
private:
    template <typename T>
    friend int bar(T& t);

    static const int id = 3;
    void foo() { /*do something*/ }
};


template <typename T>
int bar(T& t)
{
    return t.id;
}


int main()
{
    B b;
    C c;

    cout << bar(b) << "\n";
    cout << bar(c) << "\n";

    return 0;
}

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

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

发布评论

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

评论(2

影子是时光的心 2025-01-04 11:18:40

a.id 将在编译时定义为 A::id。您需要在类 A 中定义一个虚拟成员(非静态)函数,并在 BC 中重写它以返回它们各自的值id 并在 bar 中调用此函数。

a.id will be defined at compile-time as A::id. You would need to define a virtual member (non-static) function in class A and have it overridden in B and C to return their respective ids and call this function in bar.

○闲身 2025-01-04 11:18:40

有什么方法可以避免编写int foo() { return id; } 对于所有派生类?

是的,使用模板。例如:

template <typename T>
int foo (T& x)
{
    return x.id;
}

但是,如果 id 是私有的,则这不会减少太多代码。

Is there any way to avoid writing int foo() { return id; } for all the derived classes?

Yes, using templates. For example:

template <typename T>
int foo (T& x)
{
    return x.id;
}

However, if id is private, this doesn't reduce the code by all that much.

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