使用友元函数重载新运算符

时间:2017-05-21 14:47:38

标签: c++ operator-overloading new-operator

我将'new'运算符重载为我的类Array的成员函数。我需要帮助将其重载为朋友功能。请参阅以下计划:

#include<iostream>
using namespace std;

class Array {
    private:
    int *arr;
    int size;

    public:

    void * operator new (size_t size)
    {
        void *ptr = ::new Array;
        return ptr;
    }

    void operator delete(void *ptr)
    {
        ::delete ptr;
    }

    Array(int n=5)
    {
        this->size = n;
        this->arr = new int[n];
    }

    void input()
    {
        cout<<"Enter the values"<<endl;
        for(int i=0; i<size; i++)
            cin>>arr[i];
    }

    void show()
    {
        for(int i=0; i<size; i++)
            cout<<arr[i]<<" ";

        cout<<endl;
    }
};

int main()
{
    Array *A = new Array(4);
    A->input();
    A->show();
    return 0;
}

我在朋友的功能中尝试做什么:

void * operator new (size_t size) throw (std::bad_alloc)
{
   void * ptr = ::new Array;
   return ptr;
}

我得到了一个无限循环。谁能在这帮助我?

编辑:语法

2 个答案:

答案 0 :(得分:2)

只能有一个::operator new(size_t)。通常标准库提供一个。如果你提供一个,它替换(不会重载)标准库提供的那个。

您的版本无条件地调用自己(没有其他::operator new(size_t))因此存在无限递归和堆栈溢出。

您无法提供operator new并希望它与系统提供的enum foo {bar}; void* operator new (std::size_t size, foo) { ... } ... Array* arr = new (bar) Array; 一起使用,但仅适用于您的班级。

使全局重载Array的唯一方法是给它一个不同的签名,例如。

filter_queryset

但你可以使用它来分配任何类型,而不仅仅是// Create an array of recipients for the email. NSArray* emailRecipients = @[@"example@email.com", @"example2@email.com"]; // Create a mutable string to hold all of the recipient email addresses and add the first one. NSMutableString* emailTo = [[NSMutableString alloc] initWithString:emailRecipients[0]]; // Loop through all of the email recipients except for the first one. for (int index = 1; index < emailRecipients.count; index++) { // Add a semicolon and then the email address at the current index. [emailTo appendFormat:@";%@", emailRecipients[index]]; } // Get the email subject from the subject text field. NSString *emailSubject = @"Your Email Subject"; // Encode the string for URL. NSString *encodedSubject = [emailSubject stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]]; // Define your image's size NSString *htmlBody = (@"<div style=\"width:450px;height:797px;\"><img src=\"http://your_website.com/your_image.jpg\" style=\"width:100%;height:100%;\"></div>"); // Encode the string for URL. NSString* encodedBody = [htmlBody stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]]; // See if the subject or body are empty. if (![emailSubject length] || ![emailBody length]) { // Exit. return; } // Create a string with the URL scheme and email properties. NSString *stringURL = [NSString stringWithFormat:@"ms-outlook://compose?to=%@&subject=%@&body=%@", emailTo, encodedSubject, encodedBody]; // Convert the string to a URL. NSURL *url = [NSURL URLWithString:stringURL]; // Open the app that responds to the URL scheme (should be Outlook). [[UIApplication sharedApplication] openURL:url]; ,无论哪个类决定与它成为朋友。

答案 1 :(得分:0)

因此,只需添加上面的答案,你就可以重载你的运算符new并以下面的方式删除,这将有效,因为一个内部构造函数将调用operator new []而不是operator new.Also也不需要成为这个朋友,因为你没有访问此函数中的任何私有数据成员:

#include <iostream>
#include <string>
using namespace std;
class Array {
    private:
    int *arr;
    int size;

    public:             
    Array(int n=5)
    {
        this->size = n;
        this->arr = new int[n];
    }
    void input()
    {
        cout<<"Enter the values"<<endl;
        for(int i=0; i<size; i++)
            cin>>arr[i];
    }
    void show()
    {
        for(int i=0; i<size; i++)
            cout<<arr[i]<<" ";

        cout<<endl;
    }
};
void * operator new (size_t size)
{
   void *ptr = std::malloc(size);
   return ptr;
}
void operator delete(void *ptr)
{
   std::free(ptr);
}
int main()
{
    Array *A = new Array(4);
    A->input();
    A->show();
    return 0;
}