重载 C++ 时运行特定代码运营商

发布于 2024-10-08 08:40:59 字数 1176 浏览 6 评论 0原文

我有一个类,我们称之为 Foo,它包含以下 3 个方法(重载 左关联 < 二元运算符):

... operator<(A a) { return *this; }
... operator<(B b) { return *this; }
... operator<(C c) { return *this; }

A、B、C 类不是以任何方式相关(如果是的话,有什么关系)。

现在在我的程序中我只有以下两种情况:

A a = new A();
B b = new B();
C c = new C();

(First Case): new Foo() < a < b;

或者

(Second Case): new Foo() < a < b < c;

每当我有第一种情况(以 b 结尾)时,我想在阅读后执行函数 run() (我知道) b 实例。所以我的想法是,我将在 Foo 类中添加以下代码:

... operator<(B b)
{
    run();
}

现在,当我获得第一种情况的代码时,将执行 run()

问题是,当我有类似于第二种情况的代码时(以 c 结尾)。 我想再次执行 run() 函数,但直到我知道 c 是什么。 因此,如果我有前面的代码 run() 将在执行 时被调用b 这不是我想要的,因为我还不知道c。 如果我在 operator<(C c) 中添加 run(),我将调用 run() 两次。

简而言之,我想要完成的是当第一种情况在 operator<(B b) 处调用 run() 时,当我有第二种情况时仅调用 run在操作符<(C c)处。

关于如何解决这个问题有什么想法(如果可以的话)?

I have a class, let's call it Foo, which contains the 3 following methods (overloading the left-associative < binary operator):

... operator<(A a) { return *this; }
... operator<(B b) { return *this; }
... operator<(C c) { return *this; }

A, B, C classes are not related in any way(if that, is of any matter).

Now In my program I only have the 2 following cases:

A a = new A();
B b = new B();
C c = new C();

(First Case): new Foo() < a < b;

or

(Second Case): new Foo() < a < b < c;

Whenever I have the first case (which ends with b), I want to execute a function run() when I have read(I know) the b instance. So the idea is that I would have the following code in the Foo class:

... operator<(B b)
{
    run();
}

Now when I have the code of the first case, run() is being executed.

The problem is that when I have code like in the second case(which ends in c).
I want to execute the run() function again but NOT until I know what c is.
So If I have the previous piece of code run() will be called when doing < b
which is not what I want as I don't know c yet.
If I add run() in operator<(C c) I will call run() twice.

In a few words what I want to accomplish is when having the first case call run() at operator<(B b) and when I have the second case ONLY call run at operator<(C c).

Any ideas on how this can be solved(if it can)?

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

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

发布评论

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

评论(3

橘香 2024-10-15 08:40:59

您可以创建单独的代理对象返回类型,因此当您为该代理对象定义运算符时,您知道在它之前有哪些类型。

You could create separate proxy object return types, so when you define an operator for that proxy object, you know which types have gone before it.

满身野味 2024-10-15 08:40:59
class PreFoo {
public:
    PreFoo() { ... }
    PreFoo & operator<<(A a) { ...; return *this; }
    PreFoo & operator<<(B b) { ...; return *this; }
    PreFoo & operator<<(C c) { ...; return *this; }

    int aa, bb, cc; // save your information
};

PreFoo MakeFoo() { return PreFoo(); }

class Foo {
public:
    Foo(const PreFoo & pre) { run(); } // implicit conversion

    void run() {} // your run!!
};


void g() {
  A a; B b; C c;
  // implicit conversion at the end
  Foo foo1 = MakeFoo() << a << b;
  Foo foo2 = PreFoo() << a << b << c;
}
class PreFoo {
public:
    PreFoo() { ... }
    PreFoo & operator<<(A a) { ...; return *this; }
    PreFoo & operator<<(B b) { ...; return *this; }
    PreFoo & operator<<(C c) { ...; return *this; }

    int aa, bb, cc; // save your information
};

PreFoo MakeFoo() { return PreFoo(); }

class Foo {
public:
    Foo(const PreFoo & pre) { run(); } // implicit conversion

    void run() {} // your run!!
};


void g() {
  A a; B b; C c;
  // implicit conversion at the end
  Foo foo1 = MakeFoo() << a << b;
  Foo foo2 = PreFoo() << a << b << c;
}
走野 2024-10-15 08:40:59

您必须先创建一个语法树,然后对其进行评估。因此,您正在创建一种 DSL(领域特定语言)。

读这个
http: //codeidol.com/cpp/cpp-template-metaprogramming/Domain-Specific-Embedded-Languages/-10.5.-Blitz-and-Expression-Templates/

或者您可以使用 boost::proto

You have to create a syntax tree before, and than evaluate it. So you are creating a DSL, domain specific language.

read this
http://codeidol.com/cpp/cpp-template-metaprogramming/Domain-Specific-Embedded-Languages/-10.5.-Blitz-and-Expression-Templates/

or you can use the boost::proto

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