c ++操作符重载内存问题 [英] c++ operator overloading memory question

查看:148
本文介绍了c ++操作符重载内存问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在c ++中,您可以在堆和堆栈上创建一个类的新实例。当重载一个操作符时,你能够以一种有意义的方式在栈上实例化。



根据我的理解,一个位于堆栈上的实例会被移除该函数完成执行。这使得它似乎返回一个新的实例坐在堆栈将是一个问题。



我在写这篇文章,知道有一种方法,但我不知道最好的做法是什么。
如果我有一些类被设计为总是驻留在堆栈中我如何去操作符重载?



任何信息都会有所帮助, p>

{EDIT}
我正在重载+运算符。
现在我使用这个代码

  Point Point :: operator +(Point a)
{
Point * c = new Point(this-> x + ax,this-> y + ay);
return * c;
}

我对如下例子感到怀疑:

 点c(this-> x + ax,this-> y,ay); 

,因为它会将它分配给堆栈。我担心的是,一旦这个函数完成执行,堆栈指针将改变,并且实例将不再是安全的,因为任何新的局部变量定义可以擦除它。这不是一个问题吗?

解决方案

如果你正在谈论例如 operator + ,其中返回的对象不是那些输入,那么答案是你在栈上实例化并返回值:

  struct SomeClass {
int value;
};

SomeClass操作符+(const SomeClass& lhs,const SomeClass& rhs){
SomeClass retval;
retval.value = lhs.value + rhs.value;
return retval;
}

  class SomeClass {
int value;
public:
SomeClass operator +(const SomeClass& rhs)const {
SomeClass retval;
retval.value = this-> value + rhs.value;
return retval;
}
};

或甚至:

  class SomeClass {
int value;
public:
SomeClass(int v):value(v){}
friend SomeClass operator +(const SomeClass& lhs,const SomeClass& rhs){
return SomeClass lhs.value + rhs.value);
}
};

编译器然后担心实际存储返回值的位置>

它将例如应用返回值优化,如果可以,但原则上发生的是as-if的工作,你构造一些值在你的运算符重载的堆栈,然后在返回时,这被复制到任何它需要是下一个。如果调用者分配返回值,则将其复制到那里。如果调用者将它传递给其他函数的值,它将被复制到调用约定所要求的地方,以便成为该函数参数。如果调用者接受一个const引用,那么它被复制到一个临时隐藏在堆栈上。


In c++ you can create new instances of a class on both the heap and stack. When overloading an operator are you able to instantiate on the stack in a way that makes sense?

As I understood it an instance that sits on the stack is removed as soon as the function is done executing. This makes it seems as though returning a new instance sitting on the stack would be a problem.

I am writing this knowing there has to be a way, but I am not sure what the best practice is. If I have some class that is designed to always reside in the stack how do I go about operator overloading?

Any info would be helpful, thanks

{EDIT} I am overloading the + operator. Right now I use this code

Point Point::operator+ (Point a)
{
Point *c = new Point(this->x+a.x,this->y+ a.y);
return *c;
}

I was skeptical about instantiating c like so:

Point c(this->x + a.x, this->y, a.y);

because that would allocate it to the stack. My concern is that the stack pointer is going to change once this function finishes executing, and the instance will no longer be safe since any new local variables defined could erase it. Is this not a concern?

解决方案

If you're talking about for example operator+, where the object returned is not either of those input, then the answer is you instantiate on the stack and return by value:

struct SomeClass {
    int value;
};

SomeClass operator+(const SomeClass &lhs, const SomeClass &rhs) {
    SomeClass retval;
    retval.value = lhs.value + rhs.value;
    return retval;
}

or

class SomeClass {
    int value;
public:
    SomeClass operator+(const SomeClass &rhs) const {
        SomeClass retval;
        retval.value = this->value + rhs.value;
        return retval;
    }
};

or even:

class SomeClass {
    int value;
public:
    SomeClass(int v) : value(v) {}
    friend SomeClass operator+(const SomeClass &lhs, const SomeClass &rhs) {
        return SomeClass(lhs.value + rhs.value);
    }
};

The compiler then worries about where (on the stack) the return value is actually stored.

It will for example apply return-value optimizations if it can, but in principle what's happening is "as-if" the work you do constructs some value on the stack of your operator overload, and then at return this is copied to wherever it needs to be next. If the caller assigns the return value, it's copied there. If the caller passes it by value to some other function, it's copied wherever the calling convention says it needs to be in order to be that function parameter. If the caller takes a const reference, then it's copied to a temporary hidden away on the stack.

这篇关于c ++操作符重载内存问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆