简体   繁体   English

从不同的类访问向量对象

[英]Accessing an vector-Object from a different class

I created a vector with Pointers and I create new Objects from the class DigOut derived from Modul in one method called 我用Pointers创建了一个向量,并使用一种方法从Modul派生的DigOut类创建了新Objects

BOOL Cbeckhoff_frontendDlg::OnInitDialog()
{
//...
std::vector<Modul*> arrDigOut;
arrDigOut.push_back(new DigOut(IDC_CHECK1, this,"GVL.DigOut1",pAddr));
//...
for(iNumDO = 0;iNumDO<1;iNumDO++) arrDigOut[iNumDO]->InitCheck(this);
//...
}

How can I access the vector from a different method like: 如何从其他方法访问向量,例如:

void Cbeckhoff_frontendDlg::OnBnClickedButton3()
{
for(iNumDO = 0;iNumDO<1;iNumDO++) arrDigOut[iNumDO]->SetID();
}

I thought about using public pointers or setters and getters, 我考虑过使用公共指针或setter和getter,
but I don't get to create membervariables like this: 但是我不能像这样创建成员变量:

std::vector<Modul*> *   parrDigOut;

where it's complaining, that Modul is not declared. 在抱怨的地方,没有声明Modul。

如果我正确理解,请将其公开,并在其前面添加以下行:

class Modul;

Your example gives the impression you are declaring the vector at function scope. 您的示例给人的印象是您在函数范围内声明了vector Its lifetime ends at the end of the function call (and all memory is leaked). 它的生命周期在函数调用结束时结束(并且所有内存都被泄漏)。 Store it as a class member and a member functions begin and end that forward to the begin and end member functions of the vector . 将其存储为类成员,并且成员函数的beginend指向vectorbeginend成员函数。 Possibly wrap them in a dereference_iterator to hide the fact that they are pointers. 可能将它们包装在dereference_iterator以隐藏它们是指针的事实。

class foo {
public:
  foo() { 
    // add things to s_
  }

  ~foo() { 
    // dont forget to delete everything in s_
  }
  typedef std::vector<my_stuff*>::iterator iterator;
  typedef std::vector<my_stuff*>::const_iterator const_iterator;
  iterator begin() { return s_.begin(); }
  iterator end() { return s_.end(); }
  const_iterator begin() const { return s_.begin(); }
  const_iterator end() const { return s_.end(); }

  // or to hide the pointers
  typedef boost::indirect_iterator< std::vector<my_stuff*>::iterator > iterator;
  iterator begin() { return boost::make_indirect_iterator(s_.begin()); }
  iterator end() { return boost::make_indirect_iterator(s_.end()); }
private:
  std::vector<my_stuff*> s_;
};

You could define a member variable as you described. 您可以按照说明定义成员变量。 On top of the class declaration just provide a forward declaration, 在类声明之上,只需提供一个前向声明,

class Modul;

This is to let the compiler know that such a class is going to get defined somewhere down the line. 这是为了让编译器知道将在某个地方定义此类。

The member variables need not be public as it is accessed from another function in the same class. 成员变量不需要是公共的,因为可以从同一类中的另一个函数访问它。

And just declare the member as: 并将成员声明为:

std::vector<Modul*>   parrDigOut; //No need of pointers

However, you could think of using a smart pointer to make sure that it gets deleted when the parent class gets out of scope 但是,您可以考虑使用智能指针来确保当父类超出范围时将其删除

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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