我想改写这个问题,以便更清楚。我有一种感觉,我的方法通常是错误的
它是否破坏了依赖注入的意图,将共享指针向量的引用传递给构造函数,目的是创建一个新的共享指针并将其推送到向量?
我知道不应在对象内使用新命令。在这种情况下,它创建一个对象但将其存储在外部。
有没有更好的方法来解决这个问题,以便“新”可以留在外面?
斑点标记.h
#ifndef SPOTTINGMARKER_H_INCLUDE
#define SPOTTINGMARKER_H_INCLUDE
class SpottingMarker
{
public:
SpottingMarker() {;}
~SpottingMarker(){;}
void blah();
double mPosition;
double mDuration;
char* mDescription;
};
#endif
斑点标记.cpp
#include "SpottingMarker.h"
void blah() {;}
CSVSpottingNotes.h
#ifndef CSVSPOTTINGNOTES_H_INCLUDED
#define CSVSPOTTINGNOTES_H_INCLUDED
#include "SpottingMarker.h"
#include <boost/shared_ptr.hpp>
#include <vector>
typedef boost::shared_ptr<SpottingMarker> spottingMarker_ptr;
class CsvSpottingNotes
{
public:
CsvSpottingNotes(const char* filename, std::vector<spottingMarker_ptr> &SpottingMarkerSet);
~CsvSpottingNotes(){;}
const char* mFilename;
const char field_terminator;
const char line_terminator;
const char enclosure_char;
};
#endif
CSVSpottingNotes.cpp
#include "CsvSpottingNotes.h"
#include "SpottingMarker.h"
#include <csv_parser/csv_parser.hpp>
#include <boost/shared_ptr.hpp>
CsvSpottingNotes::CsvSpottingNotes(const char* filename, std::vector<spottingMarker_ptr> &SpottingMarkerSet) :
field_terminator(','),
line_terminator('\n'),
enclosure_char('"')
{
spottingMarker_ptr aSpottingMarker(new SpottingMarker());
SpottingMarkerSet.push_back(aSpottingMarker);
}
主文件
#include "CsvSpottingNotes.h"
#include "SpottingMarker.h"
#include <vector>
int main(int argc, char ** argv)
{
typedef boost::shared_ptr<SpottingMarker> spottingMarker_ptr;
const char* filename = "AT_92.csv";
std::vector<spottingMarker_ptr> spottingMarkerSet;
CsvSpottingNotes structure(filename, spottingMarkerSet);
}