Results 1 to 8 of 8

Thread: C++ templates - design question

  1. #1
    Join Date
    Nov 2009
    Location
    US, Midwest
    Posts
    215
    Thanks
    62
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default C++ templates - design question

    Here is my situation:

    Qt Code:
    1. class basepolicy
    2. {
    3. public:
    4. virtual int v1(int) const;
    5. virtual int v2(int, int) const;
    6. };
    7.  
    8. class policy1 : public basepolicy
    9. {
    10. public:
    11. int v1(int) const;
    12. };
    13.  
    14. class policy2 : public basepolicy
    15. {
    16. public:
    17. int v1(int) const;
    18. };
    To copy to clipboard, switch view to plain text mode 

    derived classes policy1 and policy2 are reimplementing member function v1 and fall back to the implementation of v2 in the base class basepolicy.

    Now, classes policy1 and policy2 themselves are an template parameter to another class policySelector.

    Qt Code:
    1. template<typename T> class policySelector
    2. {
    3. public:
    4. void select()
    5. {
    6. _t.v1(1); // ok
    7. _t.v2(1,1); // fails to compile, requires implementation in policy1 and policy2
    8. }
    9. private:
    10. T _t;
    11. }
    12.  
    13. void main()
    14. {
    15. policySelector<policy1> p1;
    16. policySelector<policy2> p2;
    17.  
    18. p1.select();
    19. p2.select();
    20. }
    To copy to clipboard, switch view to plain text mode 

    Question: in my project I have many policyX classes, and only few of them require implementation of "v2" member, all
    others are OK with default implementation of "v2" in the base class.

    According to the compiler I have to provide implementation of v2 in every derived policy class if this class is used
    as a template parameter to "policySelector".

    Is there a way to restructure the design so I don't have to pollute the code with the identical implementation of v2 in all policy classes?

  2. #2
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: C++ templates - design question

    You don't need to reimplement the function. Just declare it, then in the body make a call to the parents implementation:

    Qt Code:
    1. derivedPolicy::v2(int i, int j)
    2. {
    3. basePolicy(i, y);
    4. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Nov 2009
    Location
    US, Midwest
    Posts
    215
    Thanks
    62
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: C++ templates - design question

    This is exactly the problem. I have 30+ policyX classes. So, I have to have 30 identical "v2" declaration in each of policyX class.

    I'd like to know if there is a pattern that will allow automatic fall to basepolicy.v2 implementation without declaring "v2" method in each and every policyX class.

  4. #4
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: C++ templates - design question

    Your code (the one that you posted should work)
    Check your actual project (see if you didn't forgot a public when inherit some classes, or other issues)

    Also tell us the actual error that compiler is giving you at that function call.

  5. The following user says thank you to Zlatomir for this useful post:

    TorAn (31st July 2010)

  6. #5
    Join Date
    Nov 2009
    Location
    US, Midwest
    Posts
    215
    Thanks
    62
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: C++ templates - design question

    Thank you for checking it out. Indeed, it does compile. I failed to carefully check my example against the real code. I'll make changes to match my situation exactly, test it, get my compile error and post modified example here.

  7. #6
    Join Date
    Nov 2009
    Location
    US, Midwest
    Posts
    215
    Thanks
    62
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: C++ templates - design question

    Here is the example that does not compile.

    Qt Code:
    1. #include "stdafx.h"
    2. #include <iostream>
    3. #include <vector>
    4.  
    5. class A
    6. {
    7. };
    8.  
    9. class B
    10. {
    11. };
    12.  
    13. class Rule
    14. {
    15. public:
    16. enum ruleStatus
    17. {
    18. rsPassed = 1,
    19. rsFailed = 2,
    20. rsIncompleteData = 3
    21. };
    22.  
    23. Rule() {}
    24.  
    25. virtual ~Rule() {}
    26.  
    27. virtual enum ruleStatus isValid(int i, int j, const int* tempval, const A& cfd) const;
    28. virtual enum ruleStatus isValid(int i, int j, const B& pair,const int* tempval, const A& cfd) const;
    29. protected:
    30. };
    31.  
    32. template <typename rulePolicy> class btRuleGeneric : public Rule
    33. {
    34. public:
    35. btRuleGeneric(): Rule(), _policy(*this){}
    36.  
    37. enum ruleStatus isValid(int i, int j, const int* tempval, const A& cfd) const
    38. {
    39. return _policy.isValid(i, j, tempval,cfd);
    40. }
    41.  
    42. enum ruleStatus isValid(int i, int j, const B& pair, const int* tempval, const A& cfd) const
    43. {
    44. return _policy.isValid(i, j, pair, tempval,cfd);
    45. }
    46. private:
    47. rulePolicy _policy;
    48. };
    49.  
    50.  
    51. class basePolicy
    52. {
    53. public:
    54. basePolicy(const Rule& rule) : _rule(rule) {};
    55. virtual Rule::ruleStatus isValid(int i, int j, const int* tempval, const A& cfd) const;
    56. virtual Rule::ruleStatus isValid(int i, int j, const B& pair, const int* tempval, const A& cfd) const;
    57. protected:
    58. mutable std::string _lasterror;
    59. const Rule& _rule;
    60. };
    61.  
    62. class myPolicy : public basePolicy
    63. {
    64. public:
    65. myPolicy(const Rule& rule) : basePolicy(rule) {}
    66. Rule::ruleStatus isValid(int i, int j,
    67. const int* tempval, const A& cfd) const;
    68.  
    69. // compiles OK if this method is uncommented
    70. // Rule::ruleStatus isValid(int i, int j, const B& pair,
    71. // const int* tempval, const A& cfd) const;
    72. };
    73.  
    74. typedef std::vector<Rule*> coll;
    75.  
    76. int _tmain(int argc, _TCHAR* argv[])
    77. {
    78. coll lst;
    79.  
    80. Rule* rule = NULL;
    81. lst.push_back(new btRuleGeneric<myPolicy>());
    82.  
    83. return 0;
    84. }
    To copy to clipboard, switch view to plain text mode 

  8. #7
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: C++ templates - design question

    Aha, you have overloaded functions in there (i should have guessed that)

    You will need to override all the overloads of a function, or introduce all of them (their implementation from the Base class) with the using directive.
    Something like: <i didn't test it, i don't have VS right now (and saw stdafx... and other stuff that probably i should "hack" to work with Creator ), but it should work fine>
    Qt Code:
    1. class myPolicy : public basePolicy
    2. {
    3. public:
    4. myPolicy(const Rule& rule) : basePolicy(rule) {}
    5.  
    6. using basePolicy::isValid; //this is what you need to do in every derived class, then you will have your desired behavior
    7.  
    8. Rule::ruleStatus isValid(int i, int j,
    9. const int* tempval, const A& cfd) const;
    10.  
    11. // compiles OK if this method is uncommented
    12. // Rule::ruleStatus isValid(int i, int j, const B& pair,
    13. // const int* tempval, const A& cfd) const;
    14. };
    To copy to clipboard, switch view to plain text mode 

  9. The following user says thank you to Zlatomir for this useful post:

    TorAn (1st August 2010)

  10. #8
    Join Date
    Nov 2009
    Location
    US, Midwest
    Posts
    215
    Thanks
    62
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: C++ templates - design question

    Thank you. You taught me the aspect of "using" directive that I did not encounter before.

    A using declaration in a definition of a class A allows you to introduce a name of a data member or member function from a base class of A into the scope of A.

Similar Threads

  1. MDI design question
    By mooreaa in forum Qt Programming
    Replies: 8
    Last Post: 8th April 2011, 09:42
  2. Design question.
    By RurouniJones in forum Newbie
    Replies: 2
    Last Post: 14th March 2010, 15:16
  3. Design question regarding dll
    By MarkoSan in forum Qt Programming
    Replies: 1
    Last Post: 11th December 2008, 16:49
  4. templates / Q_OBJECT question
    By _borker_ in forum Qt Programming
    Replies: 6
    Last Post: 19th December 2007, 20:35
  5. Design problem/question
    By Valheru in forum Qt Programming
    Replies: 2
    Last Post: 27th September 2006, 21:45

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.