我在网上搜索了如何返回一个矢量对象,但我找不到最简单的一个。首先,我不是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 = %s\n", 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("End\n");
}
标题文件:
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文件is->getSize()
下,即使我在push_back
m_vkey
上使用了addKey
,也无法增加{{1}}方法。
答案 0 :(得分:1)
这是因为您修改的对象与您正在存储的对象不同。
看这里:
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
)了。