Wednesday 15 September 2010

c++ - Class Method with specialized Parameter -


It is possible to create a template method with flexible parameter lists, such as:

  class test A {public: Test A (int i, nt j) {...}} Class test {public: test b (study :: string A} {...}} class base {public: template  

with the PARA replaced by the parameter list. Class test A and Test B know what parameters they need (how many parameters and types) to make it possible with VAR?

Is there a better way to do this?

  base.add & lt; Testa & gt; (1,2); Base.add & lt; TestB & gt; ("Abc");  

Yes, they are looking for you, they will give you the methodology you want:

  template & lt; Class T, Class ... Args & gt; Bool Base :: Add (RGS & amp; ... args) {// ... t myT (std :: forward & lt; Ergas & gt; (Args) ...); // ...}  

Note that if you do not have any rvalue references, then both of you have both the Args & amp; Amp; & Amp; and should use std: Forwarded (unless you explicitly want to apply a copy) These will be required in the appropriate reference types.


1 comment: