helllo, i've got a problem with the code below. I think the problem lies with the const command . I understand you have to use const_iterator with variables that are passed as const. However in the code i->printSummary() it reports a problem? I've been reading about the const command and has to be used with some care. Is there anything wrong with the function?. The prototype has identical parameter listing.

void eligableParents(  const list < mutantType>  &tempList)
{ 
 std::list < mutantType> ::  const_iterator i;   

 for ( i = tempList.begin();
              i != tempList.end();
               ++ i )
               i->printSummary();
}

printSummary() is a member of mutantType

Recommended Answers

All 5 Replies

Const iterators are used to indicated that the underlying object data members will not change.

Const iterators are used to indicated that the underlying object data members will not change.

so .. are you saying the const statement is in the wrong place in the parameter list of the function title?

so .. are you saying the const statement is in the wrong place in the parameter list of the function title?

No not at all...Passing the object as const informs the compiler that this reference won't change the object.

Does printSummary() change the object?

No, the printSummary() doesn't change anything. Do you think my function looks ok then ?

make sure that the method "printSummary()" is also defined as const, like this:

class MutantType {
  //stuff here...
  public:
    void printSummary() const { //notice the const after the function prototype.
      //..print stuff but cannot change any data member value.
    };
};

otherwise, the compiler will throw an error saying you are calling non-const method on a const object.

For the rest, your use of constness (const reference to list and const iterator) is correct.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.