如何使用智能指针?

时间:2012-05-02 09:59:44

标签: c++

在下面的示例中,我使用指针mySession到类OpenStackSession中的结构OpenStack,我通过分配和取消分配来填充和使用它来通过两种不同的方法手动记忆。如何通过使用智能指针来完成我在这里做同样的事情?我根本不知道如何使用智能指针。请通过更改使用smartpointer的代码来显示它。 谢谢。

请注意:我必须使用命名空间std和库字符串来完成此操作。我不知道它对智能指针是否重要,但我认为值得通知。

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

struct OpenStackSession
{
    int limit;
    char prefix[256];
    char marker[256];
    char delimiter[256];
    char format[256];
    char path[256];
};

class OpenStack
{

public:
    OpenStackSession* mySession;
    void requestbegin(const char*);
    void requestpayload();
};

void OpenStack::requestbegin(const char* url)
{
    mySession=new OpenStackSession();
    char urlCopy[2000];
    strcpy(urlCopy,url);
    urlCopy[strlen(url)]='\0';
    char* questionMarkPointer=strchr(urlCopy,'?');
    int questionMarkPosition=questionMarkPointer-urlCopy;
    char query[2000];
    strncpy(query,urlCopy+questionMarkPosition+1,strlen(urlCopy)-questionMarkPosition-1);
    query[strlen(urlCopy)-questionMarkPosition-1]='\0';
    int lenForEqSign=0;
    int plusAndSignPosition=0;
    int andSignPosition=0;
    bool check=true;
    while(check == true)
    {

        char* equalSignPointer=strchr(query+plusAndSignPosition,'=');
        int lenForEqSign=equalSignPointer-query;
        char* andSignPointer=strchr(query+plusAndSignPosition,'&');
        if(andSignPointer!=NULL)
        {
            andSignPosition=andSignPointer-query;
        }
        else
        {
            andSignPosition = strlen(query);
            check=false;
        }
        if(strncmp(query+plusAndSignPosition,"limit",5) == 0)
        {
            char temp[256];
            strncpy(temp,query+lenForEqSign+1,andSignPosition-lenForEqSign-1);
            temp[andSignPosition-lenForEqSign-1]='\0';
            mySession->limit=atoi(temp);
        }
        else if(strncmp(query+plusAndSignPosition,"prefix",6) == 0)
        {
            strncpy(mySession->prefix,query+lenForEqSign+1,andSignPosition-lenForEqSign-1);
            mySession->prefix[andSignPosition-lenForEqSign-1]='\0';
        }
        else if(strncmp(query+plusAndSignPosition,"marker",6) == 0)
        {

            strncpy(mySession->marker,query+lenForEqSign+1,andSignPosition-lenForEqSign-1);
            mySession->marker[andSignPosition-lenForEqSign-1]='\0';
        }
        else if(strncmp(query+plusAndSignPosition,"delimiter",9) == 0)
        {

            strncpy(mySession->delimiter,query+lenForEqSign+1,andSignPosition-lenForEqSign-1);
            mySession->delimiter[andSignPosition-lenForEqSign-1]='\0';
        }
        else if(strncmp(query+plusAndSignPosition,"path",4) == 0)
        {
            strncpy(mySession->path,query+lenForEqSign+1,andSignPosition-lenForEqSign-1);
            mySession->path[andSignPosition-lenForEqSign-1]='\0';   

        }
        else if(strncmp(query+plusAndSignPosition,"format",6) == 0)
        {
            strncpy(mySession->format,query+lenForEqSign+1,andSignPosition-lenForEqSign-1);
            mySession->format[andSignPosition-lenForEqSign-1]='\0'; 

        }
        plusAndSignPosition=andSignPosition+1;  
    }
}

void OpenStack::requestpayload()
{
    printf("Limit %d\n",mySession->limit);
    printf("Prefix %s\n",mySession->prefix);
    printf("Marker %s\n",mySession->marker);
    printf("Delimiter %s\n",mySession->delimiter);
    printf("Path %s\n",mySession->path);
    printf("Format %s\n",mySession->format);
    delete mySession;
}

void main()
{
    const char* url="/v1/AUTH_abb52a71-fc76-489b-b56b-732b66bf50b1/images/a/plusAndSignPosition/c/andSignPosition/e?limit=100000&prefix=this-is-prefixdelimiter&marker={}marker&path=/temp/path/marko/it&delimiter=/delimiter&format=xmlformat"  ;
    OpenStack open;
    open.requestbegin(url);
    open.requestpayload();
}

0 个答案:

没有答案