Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences between 2 pointers of a template

Tags:

c++

I want to know if there is any difference between 2 pointers (as data members) in a template class. For example:

template <typename E>
class Link
{
private:
    E element;
    Link* a;
    Link<E>* b;
};

Is "a" analogous to "b", I wonder?

like image 367
Tki Lio Avatar asked Jul 30 '18 09:07

Tki Lio


People also ask

Can we compare 2 pointers?

We can compare pointers if they are pointing to the same array. Relational pointers can be used to compare two pointers. Pointers can't be multiplied or divided.

What happens when you compare two pointers?

When two pointers are compared, the result depends on the relative locations in the address space of the objects pointed to. If two pointers to object types both point to the same object, or both point one past the last element of the same array object, they compare equal.

What is a 2 pointer approach?

Two pointer algorithm is one of the most commonly asked questions in any programming interview. This approach optimizes the runtime by utilizing some order (not necessarily sorting) of the data. It is generally applied on lists (arrays) and linked lists. This is generally used to search pairs in a sorted array.

How do you know if two pointers are the same?

Equality operator (==,!=) Pointers of the same type (after pointer conversions) can be compared for equality. Two pointers of the same type compare equal if and only if they are both null, both point to the same function, or both represent the same address (3.9. 2).


1 Answers

Both forms are valid. When the name of a class template is used inside the class template scope without template arguments, it resolves to the name of the "current" template specialization.

a and b have the same type.

It's the same rule that makes it possible for us to write for example:

template<typename T>
Foo<T>::Foo(Foo const&) = default;

... instead of the more verbose:

template<typename T>
Foo<T>::Foo(Foo<T> const&) = default;
like image 156
StoryTeller - Unslander Monica Avatar answered Sep 20 '22 15:09

StoryTeller - Unslander Monica