为通过引用传递的参数分配默认值

发布于 2025-01-03 21:02:27 字数 527 浏览 2 评论 0原文

我想做这样的事情:

int displayAll(Message *m, string &lastIndex, int &NumPrinted = 0 );

它给了我错误,将 int 抄袭为 int&。

我也尝试过:

int temp =0;

int displayAll(Message *m, string &lastIndex, int &NumPrinted = temp );

仍然给出以下错误:

error: ISO C++ forbids in-class initialization of non-const static member 'temp'

即使 static int temp; 也会出错。

错误:ISO C++ 禁止非 const 静态成员 'temp 的类内初始化'

I want to do something like this:

int displayAll(Message *m, string &lastIndex, int &NumPrinted = 0 );

It gives me error, cribbing about int to int&.

I tried this too:

int temp =0;

int displayAll(Message *m, string &lastIndex, int &NumPrinted = temp );

Still it gives following error:

error: ISO C++ forbids in-class initialization of non-const static member 'temp'

Even static int temp; gives error.

error: ISO C++ forbids in-class initialization of non-const static member 'temp'

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

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

发布评论

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

评论(3

深海不蓝 2025-01-10 21:02:27

您提到的第一行代码的问题是您试图传递对临时变量的引用

class Foo {

    int displayAll(Message *m, bool &moreElements, string &lastIndex, int &NumPrinted = 0 );

};

第二位代码抱怨因为您试图静态初始化类成员。

class Foo {

    int temp =0;

    int displayAll(Message *m, bool &moreElements, string &lastIndex, int &NumPrinted = temp );

};

(我将您的代码放在类声明中,以清楚地了解发生了什么)。

解决不引入静态变量的问题的一个简单方法是显式函数重载:

class Foo {

    inline int displayAll(Message *m, bool &moreElements, string &lastIndex) {
        int dummy = 0;
        return displayAll(m, moreElements, lastIndex, dummy);
    }
    int displayAll(Message *m, bool &moreElements, string &lastIndex, int &NumPrinted);

};

有一些样板文件,但它实现了您想要的。
希望这有帮助。

编辑:更多说明。问题的核心源于这样一个事实:该函数必须引用一些它可以修改的内存。如果你向它传递一个临时变量(临时变量在 C++ 中的含义,而不仅仅是英语术语)(如你的第一行代码),那么它是非法的 C++,因为你通常将临时变量复制到之前的值将它用作函数的参数:

void bar( int someNum = 0 ); // think of this as creating a temporary rvalue 0
                             // and then copying it into the function for use.

// temporary rvalues arise in expressions like
int v = 5 + 5; // the result of 5 + 5 is stored in a temporary rvalue, and then
               // copied into v (which is an lvalue in this case).

所以我们需要一些“左值”,要么是某个全局变量,要么是临时局部变量(在英语语言意义上),正如我在回答中给出的那样。我本来打算使用静态变量编写一个解决方案,但是有一个很大的缺陷 - 由于静态变量将由类的所有实例共享,它将从 0 开始,然后每次调用该方法时都会有所不同(因为它会被之前的调用编辑过)。更糟糕的是,在多线程的情况下,您将从多个处理器读取/写入内存的同一位置,因此该值将完全是垃圾,并且您将破坏处理器核心的缓存,因为每次写入都会使缓存无效所有其他核心。太丑了,请不要这样做。 :P

通过使用我的第一个解决方案,您可以使临时变量非常本地化,而不会对其他任何事情产生太大影响。

The problem with the first line of code you mention is that you are trying to pass a reference to a temporary variable

class Foo {

    int displayAll(Message *m, bool &moreElements, string &lastIndex, int &NumPrinted = 0 );

};

The second bit of code complains because you were trying to initialize a class member statically.

class Foo {

    int temp =0;

    int displayAll(Message *m, bool &moreElements, string &lastIndex, int &NumPrinted = temp );

};

(I am putting your code inside of a class declaration to be clear about what is happening).

An easy way out of your problem that does not introduce a static variable is explicit function overloading:

class Foo {

    inline int displayAll(Message *m, bool &moreElements, string &lastIndex) {
        int dummy = 0;
        return displayAll(m, moreElements, lastIndex, dummy);
    }
    int displayAll(Message *m, bool &moreElements, string &lastIndex, int &NumPrinted);

};

There's a bit of boilerplate, but it achieves what you want.
Hope this helps.

EDIT: Some more clarification. The core of the problem stems from the fact that the function must take a reference to some memory that it can modify. If you pass it a temporary variable (temporary as in the C++ meaning of the term, not just the english language term ) (as in your first line of code), it's illegal C++, since you usually copy a temporary to a value before you use it as an argument to a function:

void bar( int someNum = 0 ); // think of this as creating a temporary rvalue 0
                             // and then copying it into the function for use.

// temporary rvalues arise in expressions like
int v = 5 + 5; // the result of 5 + 5 is stored in a temporary rvalue, and then
               // copied into v (which is an lvalue in this case).

So we need something that is an "lvalue", either some global variable somewhere or a temporary local variable ( in the english language sense ) as I gave in my answer. I was about to write a solution using a static variable, but there is a large flaw- since the static variable will be shared by all instances of your class, it will start out 0 and then be different every time you call the method ( since it would have been edted by the previous call). Even worse, in the case of multiple threads, you would be reading/writing to the same place of memory from several processors, so the value will be complete garbage, and you ill rape your processor cores' caches as each write will invalidate the cache of every other core. It's ugly, please don't do it. :P

By using my first solution you make the temporary variable very local, without much impact on anything else.

狠疯拽 2025-01-10 21:02:27

除非将 temp 声明为静态,否则无法对非常量引用执行此操作:请参阅 stackoverflow 帖子。

You can't do this for a non-const reference unless you declare temp to be static: see this stackoverflow post.

归途 2025-01-10 21:02:27

我也发现了这种有趣的方法来实现这一目标:

class demo {
public:
        void displayAll(int &x, int y = 0 ) {
            int *p;
            if(y)
                p = (int*)y;
            if(p) *p = 10;

        x = 4;
    }
};


int main() {
    int x=0, y=0;
    demo *obj = new demo();
    obj->displayAll((x);
    //obj->temp(x,(int)&y);
    cout << "\n x= " << x << " y " << y;
    return 0;
}

I fpund this interesting way of achieving this too:

class demo {
public:
        void displayAll(int &x, int y = 0 ) {
            int *p;
            if(y)
                p = (int*)y;
            if(p) *p = 10;

        x = 4;
    }
};


int main() {
    int x=0, y=0;
    demo *obj = new demo();
    obj->displayAll((x);
    //obj->temp(x,(int)&y);
    cout << "\n x= " << x << " y " << y;
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文