Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

#11 Missing const method in ConfigElementAbstract

closed
Carle Cote
None
5
2006-11-18
2006-11-18
Fernyqc
No

Consider the following code :

bool configure (const ConfigElementAbstract& config)
{
// MyVisitor is inherited from VisitorAbstract
MyVisitor visitor;
config.accept(visitor);
....
}

That code don't compile because the accept method is not const. However, semantically I don't want the configure method to modify the config element.

The solution to that problem is to add const to the accept method or to overload the method (one const and the other not const).

Discussion

  • Carle Cote
    Carle Cote
    2006-11-18

    Logged In: YES
    user_id=699938
    Originator: NO

    - In C++, two methods cannot differs only by const qualifier
    - accept method must accept visitors that can modify the structure

    2 potential solutions :

    1) create a copy on the config object to avoid modification.
    2) use an acceptRO (read only) and acceptRW(read/write)

    From my point of view, the second solution adds unecessary complexity to the design since most of the time there's no strict requirement to prevent modifications.

    What do you think? Other potential solutions?

     
  • Carle Cote
    Carle Cote
    2006-11-18

    • assigned_to: nobody --> carle_cote
    • summary: Missing const method if ConfigElementAbstract --> Missing const method in ConfigElementAbstract
     
  • Fernyqc
    Fernyqc
    2006-11-18

    Logged In: YES
    user_id=1138095
    Originator: YES

    I forgot that in C++, two methods cannot differs only by a const qualifier.

    I share your point on view on potential solution 2. Solution 1 is fine with me.

     
  • Carle Cote
    Carle Cote
    2006-11-18

    • status: open --> closed