英文:
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 <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;
}
答案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<T>
can be specialized so there is not way for the compiler to know the actual body of LList<T>
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 <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>`
}
};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论