如何正确获取基类的类型?

huangapple go评论100阅读模式
英文:

How to get base class's types correctly?

问题

Coming from other languages with type system, I assumed C++ would be no different from the rest but I cannot get the types of LList class. In DLList when I type this-> head I do not get any intellisense for it which makes me uncomfortable. So my question is "Is this normal in CPP?"

  1. template <class T>
  2. struct Node
  3. {
  4. T data;
  5. Node<T> *next;
  6. Node<T> *prev;
  7. };
  8. template <class T>
  9. class LList
  10. {
  11. protected:
  12. Node<T> *head;
  13. Node<T> *tail;
  14. int count;
  15. public:
  16. LList()
  17. {
  18. head = tail = nullptr;
  19. count = 0;
  20. };
  21. virtual void push(T value) = 0;
  22. virtual void pop() = 0;
  23. virtual void insert(int index, T value) = 0;
  24. virtual void remove(int index) = 0;
  25. virtual void find(const char *value) = 0;
  26. virtual void filter(int level) = 0;
  27. };
  28. template <class T>
  29. class DLList : public LList<T>
  30. {
  31. public:
  32. ~DLList()
  33. {
  34. while (!(this->count == 0))
  35. {
  36. pop();
  37. }
  38. }
  39. void push(T value);
  40. void insert(int index, T value){};
  41. void pop(){};
  42. void remove(int index){};
  43. void find(const char *value){};
  44. void filter(int level){};
  45. };
  46. template <class T>
  47. void DLList<T>::push(T value)
  48. {
  49. Node<T> *newNode = new Node<T>;
  50. newNode->data = value;
  51. newNode->prev = nullptr;
  52. newNode->next = this->head; // <- I expected head to be Node<T> type but it says its unknown
  53. }
  54. int main()
  55. {
  56. DLList<Programmer *> programmers;
  57. Programmer *p1 = new Programmer("Smith", "John", "johnsmith@gmail.com", "johnsmith", "johnsmith", "Python", "Google", 7);
  58. Programmer *p2 = new Programmer("Doe", "Jane", "janedoe@gmail.com", "janedoe", "janedoe", "C++", "Facebook", 8);
  59. Programmer *p3 = new Programmer("Builder", "Bob", "bob123@gmail.com", "bobthebuilder", "bobthebuilder", "Rust", "Netflix", 9);
  60. programmers.push(p1);
  61. return 0;
  62. }
英文:

Coming from other languages with type system, I assumed C++ would be no different from the rest but I cannot get the types of LList class. In DLList when I type this-> head I do not get any intellisense for it which makes me uncomfortable. So my question is "Is this normal in CPP?"

  1. template &lt;class T&gt;
  2. struct Node
  3. {
  4. T data;
  5. Node&lt;T&gt; *next;
  6. Node&lt;T&gt; *prev;
  7. };
  8. template &lt;class T&gt;
  9. class LList
  10. {
  11. protected:
  12. Node&lt;T&gt; *head;
  13. Node&lt;T&gt; *tail;
  14. int count;
  15. public:
  16. LList()
  17. {
  18. head = tail = nullptr;
  19. count = 0;
  20. };
  21. virtual void push(T value) = 0;
  22. virtual void pop() = 0;
  23. virtual void insert(int index, T value) = 0;
  24. virtual void remove(int index) = 0;
  25. virtual void find(const char *value) = 0;
  26. virtual void filter(int level) = 0;
  27. };
  28. template &lt;class T&gt;
  29. class DLList : public LList&lt;T&gt;
  30. {
  31. public:
  32. ~DLList()
  33. {
  34. while (!(this-&gt;count == 0))
  35. {
  36. pop();
  37. }
  38. }
  39. void push(T value);
  40. void insert(int index, T value){};
  41. void pop(){};
  42. void remove(int index){};
  43. void find(const char *value){};
  44. void filter(int level){};
  45. };
  46. template &lt;class T&gt;
  47. void DLList&lt;T&gt;::push(T value)
  48. {
  49. Node&lt;T&gt; *newNode = new Node&lt;T&gt;;
  50. newNode-&gt;data = value;
  51. newNode-&gt;prev = nullptr;
  52. newNode-&gt;next = this-&gt;head // &lt;- I expected head to be Node&lt;T&gt; type but it says its unknown
  53. }
  54. int main()
  55. {
  56. DLList&lt;Programmer *&gt; programmers;
  57. Programmer *p1 = new Programmer(&quot;Smith&quot;, &quot;John&quot;, &quot;johnsmith@gmail.com&quot;, &quot;johnsmith&quot;, &quot;johnsmith&quot;, &quot;Python&quot;, &quot;Google&quot;, 7);
  58. Programmer *p2 = new Programmer(&quot;Doe&quot;, &quot;Jane&quot;, &quot;janedoe@gmail.com&quot;, &quot;janedoe&quot;, &quot;janedoe&quot;, &quot;C++&quot;, &quot;Facebook&quot;, 8);
  59. Programmer *p3 = new Programmer(&quot;Builder&quot;, &quot;Bob&quot;, &quot;bob123@gmail.com&quot;, &quot;bobthebuilder&quot;, &quot;bobthebuilder&quot;, &quot;Rust&quot;, &quot;Netflix&quot;, 9);
  60. programmers.push(p1);
  61. return 0;
  62. }

答案1

得分: 1

The reason why you don't get autocomplete inside the DLList class is that LList<T> can be specialized, so there is no way for the compiler to know the actual body of LList<T> where T is a template parameter.

I've seen some IDEs where you can specify a certain type for T to use in autocomplete, but that is specific to the IDE, and I don't know if it can do that in your specific case anyway.

I can see other solutions to this, like suggesting completions from the base template or suggesting an aggregate of members from all found specializations, but I don't recall seeing these implemented.


To see what I mean:

  1. template <class T> struct Base
  2. {
  3. int foo;
  4. };
  5. template <> Base<int>
  6. {
  7. int* foo;
  8. };
  9. template <> Base<float>
  10. {
  11. using foo = int;
  12. };
  13. template <> Base<char>
  14. {
  15. };
  16. template <class T>
  17. struct Derived : Base<T>
  18. {
  19. void bar()
  20. {
  21. this-> // can't autocomplete with members from `Base<T>`
  22. }
  23. };
英文:

The reason whey you don't get autocomplete inside the DLList class is that LList&lt;T&gt; can be specialized so there is not way for the compiler to know the actual body of LList&lt;T&gt; where T is a template parameter.

I've seen some IDE's where that let you specify a certain type for T to use in autocomplete, but that is specific to the IDE and I don't know if it can do that in your specific case anyway.

I can see other solutions to this like suggesting completions from the base template or suggesting an aggregate of members from all found specializations, but I don't recall seeing these implemented.


To see what I mean:

  1. template &lt;class T&gt; struct Base
  2. {
  3. int foo;
  4. };
  5. template &lt;&gt; Base&lt;int&gt;
  6. {
  7. int* foo;
  8. };
  9. template &lt;&gt; Base&lt;float&gt;
  10. {
  11. using foo = int;
  12. };
  13. template &lt;&gt; Base&lt;char&gt;
  14. {
  15. };
  16. template &lt;class T&gt;
  17. struct Derived : Base&lt;T&gt;
  18. {
  19. void bar()
  20. {
  21. this-&gt; // can&#39;t autocomplete with members from `Base&lt;T&gt;`
  22. }
  23. };

huangapple
  • 本文由 发表于 2023年5月20日 23:36:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76296009.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定