Results 1 to 8 of 8

Thread: C++ templates - design question

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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 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.

  2. #2
    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 

  3. #3
    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 

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

    TorAn (1st August 2010)

  5. #4
    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.