home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 31,783 of 33,346   
   Kelvin Chung to Dave Abrahams   
   Re: Mixing static and dynamic polymorphi   
   06 Jan 12 13:07:46   
   
   From: kelvSYC@mac.com   
      
   On 2012-01-06 05:25:26 +0000, Dave Abrahams said:   
      
   > on Thu Jan 05 2012, Kelvin Chung  wrote:   
   >   
   >> How do I resolve this?   
   >   
   > Isn't this just another version of the same problem you raised in your   
   > "Creating type erasure" thread?  If not, I'll be happy to take another   
   > look.  Otherwise, I think we should stick to one thread for this issue.   
      
   The two are related problems, but they come from different parts of the   
   same program I am writing.  The common thing is that I have a Base   
   class and subclasses, as well as DerivedProperty, which essentially   
   encapsulates metadata about each subclass.  Base is an abstract class   
   with a pure virtual function, and at least one subclass is essentially   
   an implementation of the composite design pattern, hence it uses a   
   homogeneous std::vector> as its backing.   
      
   Specific to this problem is in implementing foo().  Here, I'm passing   
   in a boost::shared_ptr (say it's from a homogeneous container),   
   and what I want to do is vary certain portions of foo() based on the   
   DerivedProperty<>::value of the actual subclass the thing is pointing   
   to (eg. if the parameter is in fact a boost::shared_ptr, I   
   want to do something based on DerivedProperty::value, which   
   may be different then if I had a Derived2 pointer, say).  Again,   
   because I passed in a Base pointer, the proper DerivedProperty::value   
   (if it even is well-defined at all) is lost.  One thought to get it   
   back at runtime is to modify Base, change the constructor to a   
   constructor template, and add a virtual function (would this be CRTP,   
   but at the "base class constructor" level rather than the whole class?)   
   returning DerivedProperty<>::type (which I assume to be the same for   
   all subclasses).  Yet to do this, it would appear that I have to employ   
   some type-erasure tactic (hence the two problems being related), and I   
   haven't even considered what happens if DerivedProperty isn't defined   
   for a particular subclass (boost::optional?).   
      
   The other problem is essentially type erasure at the "class template"   
   level.  (Think of Base as a builder class and Result as the builder   
   output - to the outside world it doesn't matter what the actual type of   
   the pointer is, as long as I can get some DerivedProperty information   
   from Result's methods and the ability to store the pointer in a   
   homogeneous container).  I believe that you provided something more   
   elegant than what I envisioned there.   
      
   Having said that, I do now see how my two issues can be treated as two   
   instances of the same problem, in the sense that the solution is the   
   same for both.  This thing that was envisioned (just copy the property   
   value in Result) had me thinking about fixing this problem by   
   redesigning foo() so that I can use a wrapper construct, but upon   
   evaluating other parts of the problem, it might not be necessary after   
   all.  It's something I have to look at.   
      
      
   --   
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca