[Gecode] A nice gotcha to be prevented...

Christian Schulte schulte at imit.kth.se
Tue May 18 16:00:41 CEST 2004


Dear all,

the following left me quite puzzled and after solving the puzzle I am once
more horrified by C++.

I did the following class hierarchy (where only the virtual priority member
functions matter):

class Base {
  ...
public:
   Base(...) {
      x.subscribe(this,PC_INT_BND);
   }
   // No priority 
};

class A : public Base {
public:
   ...
   A(...) : Base(...) {}
   virtual PropPrio priority(void) { ... }
}

(The reason to define priority only in A is to have also a B, C, and so on
with different code for priority).

Now, what happens is: pure virtual function called...

How come: when constructing an A, first Base::Base(..) is executed assuming
an object of type Base. And here we do subscribe, which actually also might
call priority! And, of course, it will call priority for an object of type
Base... 

So in this situation one also has to define a priority function in Base.
This one is thean actually only called during construction, later the
virtual priority of A will be called.

Yes, C++ is a very simple language...

Christian

--
Christian Schulte, http://www.imit.kth.se/~schulte/ 




More information about the gecode-users mailing list