Question

I have a newbie question about how to assign class member (setter). I am used to scripting and mostly there it's done via (in python)

def set_mymember(mymember):
     self.mymeber = mymember

My coworker told me "self" and "this" are not needed in C++ , "this" exists and it's not wrong in this context but that would be hard to understand for me so he said I should not care. So I first tried according to his advice:

My class definition: - (it should create a sql query string)

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
};

ANd here's one of the setter methods: I call them with filled string and vector, double check it in this code

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

But when my app later calls query.build_query()

the variables are empty

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;
   (...)

EDIT: So here's part of the app code which calls 1.setoptions, and 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();
Was it helpful?

Solution

Can't really tell whats going on without the full code, but I suspect that you're returning a query object from query.build_query that isn't a full copy of the query object, if that makes sense? Can you include the full text of build_query?

Also, I'd make the build_query method void, and not try to assign a fresh Query object back to a second Query object (q2) at all (unless you really need to, again, can't really tell without the full code), something like this:

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();
}

Also, just being pedantic here, but given that you're providing default args for all the options, I'd be inclined to initialise them in the constructor like this:

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

And then instead of a setOptions method, have setters for each individual variable:

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; }

which you call only when you need to..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top