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

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

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?"

template <class T>
struct Node
{
    T data;

    Node<T> *next;
    Node<T> *prev;
};

template <class T>
class LList
{
protected:
    Node<T> *head;
    Node<T> *tail;

    int count;

public:
    LList()
    {
        head = tail = nullptr;
        count = 0;
    };

    virtual void push(T value) = 0;
    virtual void pop() = 0;
    virtual void insert(int index, T value) = 0;
    virtual void remove(int index) = 0;
    virtual void find(const char *value) = 0;
    virtual void filter(int level) = 0;
};

template <class T>
class DLList : public LList<T>
{
public:
    ~DLList()
    {
        while (!(this->count == 0))
        {
            pop();
        }
    }

    void push(T value);
    void insert(int index, T value){};
    void pop(){};
    void remove(int index){};
    void find(const char *value){};
    void filter(int level){};
};

template <class T>
void DLList<T>::push(T value)
{
    Node<T> *newNode = new Node<T>;

    newNode->data = value;

    newNode->prev = nullptr;
    newNode->next = this->head; // <- I expected head to be Node<T> type but it says its unknown
}

int main()
{
    DLList<Programmer *> programmers;

    Programmer *p1 = new Programmer("Smith", "John", "johnsmith@gmail.com", "johnsmith", "johnsmith", "Python", "Google", 7);
    Programmer *p2 = new Programmer("Doe", "Jane", "janedoe@gmail.com", "janedoe", "janedoe", "C++", "Facebook", 8);
    Programmer *p3 = new Programmer("Builder", "Bob", "bob123@gmail.com", "bobthebuilder", "bobthebuilder", "Rust", "Netflix", 9);

    programmers.push(p1);

    return 0;
}
英文:

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?"


template &lt;class T&gt;
struct Node
{
T data;
Node&lt;T&gt; *next;
Node&lt;T&gt; *prev;
};
template &lt;class T&gt;
class LList
{
protected:
Node&lt;T&gt; *head;
Node&lt;T&gt; *tail;
int count;
public:
LList()
{
head = tail = nullptr;
count = 0;
};
virtual void push(T value) = 0;
virtual void pop() = 0;
virtual void insert(int index, T value) = 0;
virtual void remove(int index) = 0;
virtual void find(const char *value) = 0;
virtual void filter(int level) = 0;
};
template &lt;class T&gt;
class DLList : public LList&lt;T&gt;
{
public:
~DLList()
{
while (!(this-&gt;count == 0))
{
pop();
}
}
void push(T value);
void insert(int index, T value){};
void pop(){};
void remove(int index){};
void find(const char *value){};
void filter(int level){};
};
template &lt;class T&gt;
void DLList&lt;T&gt;::push(T value)
{
Node&lt;T&gt; *newNode = new Node&lt;T&gt;;
newNode-&gt;data = value;
newNode-&gt;prev = nullptr;
newNode-&gt;next = this-&gt;head // &lt;- I expected head to be Node&lt;T&gt; type but it says its unknown 
}
int main()
{
DLList&lt;Programmer *&gt; programmers;
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);
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);
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);
programmers.push(p1);
return 0;
}

答案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:

template <class T> struct Base
{
    int foo;
};

template <> Base<int>
{
    int* foo;
};

template <> Base<float>
{
    using foo = int;
};

template <> Base<char>
{
};

template <class T>
struct Derived : Base<T>
{
    void bar()
    {
         this-> // can't autocomplete with members from `Base<T>`
    }
};
英文:

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:

template &lt;class T&gt; struct Base
{
int foo;
};
template &lt;&gt; Base&lt;int&gt;
{
int* foo;
};
template &lt;&gt; Base&lt;float&gt;
{
using foo = int;
};
template &lt;&gt; Base&lt;char&gt;
{
};
template &lt;class T&gt;
struct Derived : Base&lt;T&gt;
{
void bar()
{
this-&gt; // can&#39;t autocomplete with members from `Base&lt;T&gt;`
}
};

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:

确定