简体   繁体   中英

getting direct access to data stored in vector from one class to another

I have a class called table, It has a vector as public member, this table class calls a method which adds data into the vector. Now i want to access these data stored in vector from table class to NextClass. I tried to use inheritance, for instance NextClass:table which works fine but in reality NextClass is not a table class it is an "has a relation to Table class", I also tried to make class instance of the table class into NextClass, but doing so will not allow me to access data in the vector, as a resutl it returns an empty vector. Please see an abstract code of what I am trying to achieve

table class:

class Table
{    
    public:            
        Table();
        vector<vector<float> > vec; //innitializing vector 
        void addTableData(); //method to add data in vector
}

Now I have another class NextClass

    class NextClass
        public:
            NextClass();    
           vector<vector<float> > nextVector; //new vector to store value from above class
vector<float> innervec;
           void copyVectorToThisClass(); // method to copy vector from table class to this class
           private:
           Table table;  //also tried with making pointer Table *table but no luck

A method to copy vector data from table to Next class

void NextClass::copyVectorToThisClass(){   
 for( int i = 0; i<vec.size();i++)
        for (unsigned int ii=0; ii<table.vec.size();ii++)
            {
                innervec.assign(table.vec[i].begin()+ii,table.vec[i].end());
//also tried with vec.pushback
            }
            nextVector.push_back(innervec);
}

There is no error while compiling, but when I tried to check if the newVector holds any data then it returns empty that means no data transfer in the new Vector, If there is a way to manipulate pointer to point at the data inside the class table then would be great to finish this piece of code..

Edited code

table class:

class Table
{    
    public:            
        Table();
        void addTableData(); //method to add data in vector
      private:
        vector<vector<float> > vec; //innitializing vector             
}

Now I have another class NextClass

class NextClass
public:
NextClass();    
vector<vector<float> > nextVector; //new vector to store value from above class
vector<float> innervec;
           void copyVectorToThisClass(); // method to copy vector from table class to this class              
           Table table;  //also tried with making pointer Table *table but no luck    
A method to copy vector data from table to Next class   
void NextClass::copyVectorToThisClass(){   
for( int i = 0; i<vec.size();i++)
        for (unsigned int ii=0; ii<table.vec.size();ii++)
            {
                innervec.assign(table.vec[i].begin()+ii,table.vec[i].end());
//also tried with vec.pushback
            }
            nextVector.push_back(innervec);
}

With much appreciation.

您可以在函数的开头初始化向量,然后对for循环vec.size()进行操作,使其为0,因为向量中没有任何内容。

You seem to have some conceptual errors:

  • You have a table as member of nextClass, so why to copy it from table to nextVector? That seem duplicated data and it is usually very bad.
  • You make the vec property in table public, which is usually a bad idea.

I suggest you a following structure:

change the copyVector to table, because as vec is part of table, it is who have the final decision about copying or not his data.

class table
{
....
    void getVector( const vector<float>& next ) const
}

void table::getVector( vector<float>& next ) const
{
    next = vec; //default = operator
}

And then, you may use it inside NextClass like that:

void NextClass::copyVectorToThisClass( const table& t)
{ 
    t.getVector( nextVector );
}

(Well, the code is made "on the fly", there is maybe a typo)

  1. Create an instance of class Table inside the constructor of class NextClass
  2. Using the object of class table call function addTableData() to retrieve data from file
  3. Then call the function copyVectorToThisClass()
struct Table {
  vector<vector<float> > vec;
  vector<vector<float> > & getDataRef() { return vec; }
};

class NextClass {
  vector<vector<float> > nextClassVec;
  public NextClass(Table t) {
    nextClassVec = t.getDataRef(); // method #1, maybe?
  }

  public readTable (const struct Table& tbl) { // method 2 ..?
    const vector<vector<float>> &tblVecRow = tbl.vec;
    for(int i = 0; i < tblVecRow.size();++i) {
      const vector<float> & tblVecData = tblVecRow[i];
      vector<float> nextClassVecData;
      for(int j = 0; j < tblVecData.size(); ++j) {
        nextClassVecData.push_back(tblVecData[j]);
      }
      nextClassVec.push_back(nextClassVecData);
    }
  }
};

This does a deep copy of the vector data (at least that's the idea - I haven't compiled this). There is [very] likely to be much better solutions than this (like using the copy() algorithm); if so the folks are welcome to improve it.

Other notes:

  • It really doesn't have to be this cumbersome IMO. But I am resisting wanting to improve your question
  • your vec.assign in its present form is repeatedly running on items already processed. I take it to just mean in pseudocode that you want to copy elements

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM