2010-12-14 2 views
0

나는 C++을 배우고 있으며, 내가하고 싶은 것을 대략 알지만 잘못된 길로하고있어 매우 피곤하다. 가장 좋은 방법은 이런 식으로 뭔가를하고의 (우선 올바른 방법)의 새로운 기능 : 첫째템플릿 및 상속을 사용하여이 C++ 코드를 수정하고 개선하는 방법은 무엇입니까?

// Query.hpp 
class Query { 
public: 
Query(); 

template<typename T> 
std::vector<boost::shared_ptr<BaseResult> > run(); 

private: 
std::string sql; 
}; 

// Firstly, something like this: 
// I would like to do the equivalent of passing in a type T that would be 
// derived from BaseResult and create a new instance of it when adding 
// to vector of BaseResult: 
template<typename T> 
std::vector<boost::shared_ptr<BaseResult> > Query::run() { 
std::vector<boost::shared_ptr<BaseResult> > results; 

// ResultSet is from the mysql c++ connector 
boost::shared_ptr<sql::ResultSet> res(stmt->executeQuery(this->sql)); 

// I want to add a new T to results here 
while (res->next()) { 
    results.push_back(new T(res)); 
} 

// RVO prevents copy in release - yes? 
return results; 
} 

// Query.cpp 
Query::Query() { 
} 


// main.cpp 
void foo(const std::vector<boost::shared_ptr<BaseResult> >& results) { 
// loop through calling virtual method on each item 
} 

int main(int argc, char* argv[]) 
// Determine query type 
ProgramOptions opts(argc, argv); 

// Should this indeed be a pointer because 
// of code below or is it wrong? 
std::vector<boost::shared_ptr<BaseResult> >* results; 

    // Secondly, something like this: 
if (opts.getquerytype() == "type1") { 

    // I'd like to get a 
    // std::vector<boost::shared_ptr<BaseResult> > returned here 
    // containing many instances of a 
    // type derived from BaseResult 

    // I'd like to be able to do something like this 
    // Is this assignment correct? 
     *results = getQuery().run<DerivedResult>(); 
} 
else { 
    // I'd like to get a 
    // std::vector<boost::shared_ptr<BaseResult> > returned here 
    // containing many instances of a 
    // different type derived from BaseResult 

    // I'd like to be able to do something like this 
    // Is this assignment correct? 
     *results = getQuery().run<DifferentDerivedResult>(); 
} 

    foo(results); 
} 

답변

1

, 과제는

*results = getQuery().run<DifferentDerivedResult>(); 

데 참조가 아닌 초기화 포인터가 정확하지 않습니다!

나는, 첫째, 사방 vector<...>를 사용하지 것은, RSI을 유도하는 것입니다 코드에 몇 가지 변경을 할 것 typedef 그것을

class Query { 
public: 

// Query::ResultType 
typedef std::vector<boost::shared_ptr<BaseResult> > ResultType; 

Query(); 

// next, pass in the vector where the results will be stored... 
template<typename T> 
    void run(ResultType& records); 

private: 
std::string sql; 
}; 

이제 구현 : 지금의

template<typename T> 
void Query::run(ResultType& records) { 

// ResultSet is from the mysql c++ connector 
boost::shared_ptr<sql::ResultSet> res(stmt->executeQuery(this->sql)); 

// I want to add a new T to results here 
while (res->next()) { 
    records.push_back(new T(res)); 
} 

// No need to worry about RVO 
} 

당신의 main :

Query::ResultType results; // not a pointer! 

그런 다음 각 t 예를 들어.

getQuery().run<DifferentDerivedResult>(results); 

이, 당신의 디자인에 최소한의 변경을 소개 당신이 BaseResult 유형을 피하기 위해 원하는 경우 완전히 모든 것을 템플릿 수 있습니다 -하지만 난 당신을주고 어떤 기능 BaseResult이 가능한 지 여부를 모르겠어요 ..

관련 문제