我尝试使用StructureMaker类的函数初始化Structure类的成员变量。它给出了错误。它有什么问题?我不能这样做吗?那该怎么办呢?
MyMemberNotToInitThroughConstructor.h
#ifndef MYMEMBERNOTTOINITTHROUGHCONSTRUCTOR_H_
#define MYMEMBERNOTTOINITTHROUGHCONSTRUCTOR_H_
namespace my {
class MyMemberNotToInitThroughConstructor {
public:
MyMemberNotToInitThroughConstructor(int no1);
virtual ~MyMemberNotToInitThroughConstructor();
//private:
int no;
};
} /* namespace my */
#endif /* MYMEMBERNOTTOINITTHROUGHCONSTRUCTOR_H_ */
MyMemberNotToInitThroughConstructor.cpp
#include "MyMemberNotToInitThroughConstructor.h"
namespace my {
MyMemberNotToInitThroughConstructor::MyMemberNotToInitThroughConstructor(int no1) {
no = no1;
}
MyMemberNotToInitThroughConstructor::~MyMemberNotToInitThroughConstructor() {
// TODO Auto-generated destructor stub
}
} /* namespace my */
Structure.h
#ifndef STRUCTURE_H_
#define STRUCTURE_H_
#import "MyMemberNotToInitThroughConstructor.h"
namespace my {
class Structure {
public:
Structure();
void setElements1(int id1);
void setElements2(MyMemberNotToInitThroughConstructor* member1);
virtual ~Structure();
//private:
int id;
MyMemberNotToInitThroughConstructor*member;
};
} /* namespace my */
#endif /* STRUCTURE_H_ */
Structure.cpp
#include "Structure.h"
namespace my {
Structure::Structure() {
}
void Structure::setElements1(int id1)
{
id = id1;
}
void Structure::setElements2(MyMemberNotToInitThroughConstructor* member1)
{
member = member1;
}
Structure::~Structure() {
// TODO Auto-generated destructor stub
}
} /* namespace my */
StructureMaker.h
#ifndef STRUCTUREMAKER_H_
#define STRUCTUREMAKER_H_
#import "Structure.h";
#import "MyMemberNotToInitThroughConstructor.h"
namespace my {
class StructureMaker {
public:
StructureMaker();
void makeStructure(Structure*st);
void innerMake(Structure*st);
virtual ~StructureMaker();
};
} /* namespace my */
#endif /* STRUCTUREMAKER_H_ */
StructureMaker.cpp
#include "StructureMaker.h"
namespace my {
StructureMaker::StructureMaker() {
// TODO Auto-generated constructor stub
}
void StructureMaker::makeStructure(Structure*st)
{
int id1 = 123;
st->setElements1(id1);
innerMake(st);
}
void StructureMaker::innerMake(Structure*st)
{
int no1 = 987;
MyMemberNotToInitThroughConstructor my(no1);
st->setElements2(&my);
}
StructureMaker::~StructureMaker() {
// TODO Auto-generated destructor stub
}
} /* namespace my */
Test.cc
#include <stdio.h>
#include<iostream>
#include"StructureMaker.h"
#include"Structure.h"
using namespace std;
using namespace my;
int main()
{
StructureMaker stm;
Structure*st;
stm.makeStructure(st);
//cout << st->member->no << endl;
return 0;
}
答案 0 :(得分:0)
问题在于以下功能:
void StructureMaker::innerMake(Structure*st)
{
int no1 = 987;
MyMemberNotToInitThroughConstructor my(no1);
st->setElements2(&my);
}
my
是函数中的局部变量。您正在将st
的成员变量设置为指向该局部变量的指针。一旦该函数返回,指针变为无效,如果您尝试使用该指针访问其成员变量或使用它来调用成员函数,您将遇到未定义的行为区域。
最好从堆中分配一个对象并使用它。
void StructureMaker::innerMake(Structure*st)
{
int no1 = 987;
st->setElements2(new MyMemberNotToInitThroughConstructor(no1));
}
请记住Structure
没有接管管理该指针的责任。您必须确保在Structure
中的某处取消分配内存。对于这样一个类,你需要确保你有一个复制构造函数和一个做正确事情的复制赋值操作符。
建议绝对必须阅读:The Rule of Three