c ++私有成员变量在另一个函数中未知

时间:2011-07-26 05:30:15

标签: c++ class-members

我有一个关于如何分配类成员(setter)的新手问题。我习惯于编写脚本,而且大部分是通过(在python中)完成的

def set_mymember(mymember):
     self.mymeber = mymember

我的同事告诉我“自我”和C ++中不需要“这个”,“这个”存在并且在这种背景下没有错,但这对我来说很难理解,所以他说我不应该关心。所以我首先按照他的建议尝试:

我的类定义: - (它应该创建一个sql查询字符串)

class Query
{

public:
   Query() { }
   ~Query() { }

   void setoptions( std::string qtext_where="", bool qtext_erl=true, std::vector<std::string> kids=std::vector<std::string>() );
   Query build_query( );

   void set_db_table( std::string db_table );
   void set_db_key( std::string db_key );
   void set_m_qtext( std::string m_qtext );
   void set_foo( std::string foo );

   std::string sql();
   std::string get_sql_update();

private:
   std::string m_db_table;       // Tabellenname
   std::string m_db_key;         // Tabellen-key

   std::string m_qtext_where;    // add.optionale where clause
   std::string m_qtext;          // fertiger SELECT
   std::string m_sql_update;     // fertiger UPDATE
   bool m_erl;                   // nur erledigte waehlen?
   std::vector<std::string> m_kids;    // Liste von keys zu selecten
};

这里是一个setter方法:我用填充的字符串和向量调用它们,在这段代码中仔细检查它

void Query::setoptions( string qtext_where, bool erl, vector<string> kids ) {
   m_qtext_where  = qtext_where;
   m_erl          = erl;
   m_kids = kids;     
}

但是当我的应用稍后调用query.build_query()

变量是空的

Query Query::build_query( ) {
   cout << "kids size" << m_kids.size() << endl;
   cout << "m_qtext_where " << m_qtext_where << endl;
   // Query zur auswahl der zu uebertragenden Datensaetze
   string sql_update = "UPDATE " + m_db_table;

   string qtext = "SELECT * FROM " + m_db_table;
   string qtext_order = " ORDER BY " + m_db_key;
   (...)

编辑:所以这是app代码的一部分,它调用1.setoptions和2.build_query

       // read file line by line into vector of strings
       vector<string> text_file;
        ifstream ifs( input );
        string temp;
        while( getline( ifs, temp ) ) {
           if (temp.substr(0,1) == "#" ) {
              cout << "COMMENT: " << temp << endl;
              continue;
           }
           cout << temp << endl;
           text_file.push_back( temp );
        }
        // check: yes, vector has a size = number of lines
        cout << "text_file size " << text_file.size() << endl;

        // create Query object
        Query query = Query();
        // set the members, bool erl = true
        query.setoptions( "", erl, text_file );
        // call 2nd method         
        q2 = query.build_query();

1 个答案:

答案 0 :(得分:2)

如果没有完整的代码,真的无法告诉你什么,但是我怀疑你从query.build_query返回一个查询对象,它不是查询对象的完整副本,如果这有意义的话?你能包含build_query的全文吗?

另外,我将build_query方法设为void,而不是尝试将一个新的Query对象分配回第二个Query对象(q2)(除非你真的需要,再次,如果没有完整代码),像这样:

void Query::build_query( ) {
    std::cout << "kids size" << m_kids.size() << std::endl;
    std::cout << "m_qtext_where " << m_qtext_where << std::endl;
}

main
{
    ...
    Query query = Query();
    // set the members, bool erl = true
    query.setoptions( "", true, text_file );
    // call 2nd method         
    query.build_query();
}

另外,在这里只是迂腐,但鉴于你为所有选项提供默认args,我倾向于在构造函数中初始化它们:

Query::Query() 
    : m_qtext_where("")
    , qtext_erl(true)
    , kids (std::vector<std::string>()
{}

然后代替setOptions方法,为每个单独的变量设置setter:

void setWhere(std::string qtext_where) {m_qtext_where = qtext_where ;}
void setErl(bool query_erl) { m_erl = query_erl; }
void setKids(std::vector<std::string> kids) { m_kids = kids; }

只有在你需要时才打电话。