返回向量<object>

Returning a vector<object>

本文关键字:object gt lt 向量 返回      更新时间:2023-10-16

我在网上搜索了如何返回矢量对象,但我找不到最简单的一个。首先,我不是C++专家,几周前我才开始C++。返回对象向量不行吗?

我有这样的东西。

我通过这个把它推到某个地方:

主文件:

int main()
{   
    XIniFile *f = new XIniFile();
    int result = 0;
    int v = 0;
    char *val;
    result = f->open("doc2.ini");
    if (INI_FILE_RES_OK == result) {
    }
    else
        printf("Error[%d]n", result);
}

CPP 文件:

XKey::XKey()
{
}
XKey::~XKey()
{
}
XSection::XSection()
{
}
XSection::~XSection()
{
}
XKey *XSection::addKey(const char *k, const char *val)
{
    XKey *nk = new XKey;
    nk->setName(k);
    nk->setValue(val);
    m_vkey.push_back(*nk);
    return nk;
}
void XSection::showKey()
{
    vector<XKey>::iterator ik;
    for (ik = m_vkey.begin(); ik != m_vkey.end(); ik++)
        printf("%s = %sn", ik->getName(), ik->getValue());
}
XIniFile::XIniFile()
{
    m_modified = false;
}
int XIniFile::open(const char *fn)
{   
    XSection *cs;
    char *sn;
    char *kn;
    char *val;
    int i = 0;
    FILE *f;
    f = fopen(fn, "r");
    if (!f)
        return INI_FILE_ERROR;
    struct stat file_stat;
    stat(fn, &file_stat);
    int size = file_stat.st_size;
    m_name = strdup(fn);
    char *d = (char *)malloc(size * sizeof(char *) + 1);
    fread(d, size, 1, f);
    while (i < size) {
        while (d[i] != '[') i++;
        if (d[i] == '[') {
            i++;
            while (isspace(d[i])) i++;
            sn = &d[i];
            while (d[i] != ']')
                i++;
            d[i++] = 0;
            cs = addSection(sn);
            while (isspace(d[++i]));
            while(d[i] != '[') {
                while (isspace(d[i])) i++;
                kn = &d[i];
                while (d[i] != '=') i++;
                d[i-1] = 0;
                i++;
                while (isspace(d[i])) i++;
                if (d[i] == '[') {
                    i++;
                    val = &d[i];
                    while (isspace(d[i])) i++;
                    d[i-1] = 0;
                }
                else {
                    val = &d[i];
                    while (d[i] != 'n') i++;
                    d[i] = 0;
                }
                i++;
                cs->addKey(kn, val);
                while (isspace(d[i])) {
                    i++;
                    if (i >= size) break;
                }
                if (i >= size) break;
            }
        }
    }    
    free(d);
    vector<XSection>::iterator is;
    for (is = m_vsection.begin(); is != m_vsection.end(); is++) {
        printf("[%s]n", is->getName());
        printf("is->getSize()[%d]n", is->getSize());
    }
    fclose(f);
    return INI_FILE_RES_OK;
}
XIniFile::~XIniFile()
{
    delete m_name;   
}
XSection *XIniFile::addSection(const char *s)
{
    XSection *ns = new XSection;
    ns->setName(s);
    m_vsection.push_back(*ns);
    return ns;
}
void XIniFile::showSection()
{
    vector<XSection>::iterator is;
    for (is = m_vsection.begin(); is != m_vsection.end(); is++)
        printf("[%s]n", is->getName());
    printf("Endn");
}

头文件:

class XKey
{   
public:
    XKey();
    virtual ~XKey();
    void *setName(const char *k) {m_name = strdup(k);}
    void *setValue(const char *v) {m_value = strdup(v);}
    char *getName(){return m_name;}
    char *getValue(){return m_value;}
private:
    char *m_name;
    char *m_value;
};
class XSection
{   
public:
    XSection();
    virtual ~XSection();  
    void *setName(const char *n) {m_name = strdup(n);}
    char *getName() {return m_name;}
    XKey *addKey(const char *k, const char *v);
    vector<XKey> getKey() {return m_vkey;}
    int getSize() {return m_vkey.size();}
    void showKey();
private:    
    char *m_name;
    vector<XKey> m_vkey;
};
class XIniFile
{
public:
    XIniFile();
    virtual ~XIniFile();
    int open(const char *);
    int readString(const char *, const char *, char **);
    int readInt(const char *, const char *, int *);
    XSection *addSection(const char *);
    void showSection();
private:
    char *m_name;
    vector<XSection> m_vsection;
    bool m_modified;
};

这里的问题是,在 CPP 文件下,即使我在addKey方法上m_vkey上使用push_backis->getSize()也不会增加。

这是因为您正在修改的对象与您存储的对象不同。

看这里:

XSection *XIniFile::addSection(const char *s)
{
    XSection *ns = new XSection;   // Create a new XSection
    ns->setName(s);
    m_vsection.push_back(*ns);     // Store a copy of ns
    return ns;                     // Return the original ns
}

现在,当您对返回值执行某些操作时,m_vsection中的对象不受影响。

最快的解决方法是将要返回的指针存储在m_vsection中,并且与部分对象类似。
尽管您可能应该开始使用std::shared_ptr(更不用说std::string了(,如果可能的话。

相关文章: