c++ - dynamic_cast to solve an implementation dilemma -


It's something that has been disappointing for more than a week. I have gone through various threads on Dynamic_Cocasting on this website, but I am still not sure how it is the best way to implement it.

So I have a base class like this:

  class base class {public: class recordbase {public: virtual ~ recordbase () {}}; Virtual ~ Base Class () {}; Allocate Virtual Bull (integer size, recordbase * outrecord) = 0; Virtual Bull Free (RecordBase * Alok Records) = 0; };   

There are two derivative classes in it. A derived class A is like ..

  class DeriveA: public baseClass {public: class derivedRecordA: public baseClass :: recordBase {public: inline ~ derivedRecordA () {}; someClass * obj1; } Bool allocated (integer size, baseClass :: recordBase * outreach); Bull Free (Base Class :: Recordsbase * Alok Record); }   

I have a similar derived class 'DerivedB', which is for my own implementation of a record record and works allocated and free of charge < P> Now at the end I have a square C that uses the above base class.

  Class C {Public: Base Class * Allocator; Allocated (integer size); Free (zero); }   

Now this is my problem, based on some situations, Class C stores either an allocator which is either derived or which is a derivative.

The allocation function for the class looks like C

  C :: allocated (integer size) {// condition where deriveA is needed DerivedA :: derivedRecordA recObj; If (allocate-> allocate (size, & recObj)) {return true; } Second false return; }   

The problem is now that I am forced to use dynamic castings within DeriveA :: allocated implementation so that:

  DeriveA :: Allocate (int size, base class: recordbase * outrecord) {DerivedA :: derivedRecordA * rec = dynamic_cast & lt; DeriveA :: derivedRecordA * & gt; (Outrecord); // allocated mem and store information 'Rick' returns right; }   

How do I use dynamic_casting here? Is there a cleaner solution to this problem?

There is a problem with your base class design, so you are having implementation related issues at the derived level.

If I have an indicator for the base class example (whatever should be the actual type), then the underlying contract of the allocated method is that I should work in any type baseClass :: recordBase and point

If override the derived class function, then they should not narrow the function's requirements for users of the function. This would effectively mean that they are providing overrides which does not satisfy the interface of the base class function. If they need to do this then they should provide a different function with the appropriate interface.

After saying this, I assign a new object to allocation in this case you can override an indicator and return it to an expert (this is a Known as the temporal return type). For example, you can override:

  virtual recordbus * allocated (integer size) = 0;   

with

  virtual generated records * allocated (integer size);            Are not derived objects, which will again be problematic for users of the base class interface.  

You really need to expand your question that your base class functions should be contracted and expected behavior and override there to get better answers.

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -