将所有其他实例的值相加,并将此实例设置为该总和
add the value of all other instances and set this instance to that sum
我想获取某个类的任何任意实例;对于此实例,我想在其他实例中添加一个值。 我不希望此实例的值成为总和的一部分。 我编写的代码似乎没有达到这个目标。
我有一个 C++ 类 foo
class foo
{
private:
static int count ;
int someVar ;
int anotherVar ;
void setCount( newCount )
{
count = newCount ;
}
public:
void doSomething(void)
{
while ( index < count - 1 )
{
// don't do the calculation
// when this instant is the
// one having its version of
// anotherVar updated
if ( foo == this-> ?? )
{
continue ;
}
someVar += anotherVar ;
index++ ;
}
}
} ;
换句话说,我有一个 foo 实例的向量。 我想将任意一个实例从其余实例中添加另一个另一个 Var 到该实例的 someVar 值。
总的来说,我将遍历 foo 的所有实例,以将此 otherVar 设置为当前交互实例的所有其他实例的 someVar。
你不能只是凭空魔术foo
引用,你必须将它们提供给方法。
template<typename Iterator>
foo::doSomthing(Iterator iter, Iterator end)
{
for (; iter != end; ++iter)
{
if (&*iter == this) { continue; }
someVar += iter->otherVar;
}
}
int main ()
{
std::vector<foo> foos = { /* some foos*/ };
foos.front().doSomething(foos.begin(), foos.end());
}
相关文章:
- 如何将 QML 实例化项设置为 QImageProvider
- 是否可以为所有子类设置一个实例?
- 将所有其他实例的值相加,并将此实例设置为该总和
- 在单一实例类中将成员指针设置为 null 的正确方法是什么
- std::在类包装器实例化时设置的数组大小
- 调用该方法时,我可以设置类实例
- C 设置STD :: MAP值为结构实例的指针
- 如何将一个类的两个单独实例设置为彼此相等的 c++
- 如何为每个实例设置非静态线程本地变量
- 当调用析构函数时,是否可以将指向类实例的指针设置为nullptr
- 抛出 'std::runtime_error' what() 的实例:区域设置::facet::_S_create_c_locale 名称无效
- 有没有办法使用显式实例化按类型设置成员变量
- 为什么不能为类实例设置成员值?
- 如何将类实例名称设置为变量(C++)
- 将委托设置为类的实例
- 对象引用未设置为对象的实例 (C++/CLI)
- 设置为实例的静态值在从静态方法设置时有效,但不能从类内的静态方法设置
- 在实例化期间设置 c++ 类变量的变量类型
- 可视C++对象引用未设置为对象的实例
- C++:构造函数:新实例中的父类参数和设置值