typename D<T>::E e; // this->E would not be valid syntax

L

lizhuo

hi all:
I reading "C++ Templates: The Complete Guide "
Part II: Templates in Depth
he write:

template<typename T>
class B {
public:
enumE{e1=6,e2=28,e3=496};
virtual void zero(E e = e1);
virtual void one(E&);
};

template<typename T>
class D : public B<T> {
public:
void f() {
typename D<T>::E e; // this->E would not be valid syntax
this->zero(); // D<T>::zero() would inhibit virtuality
one(e); // one is dependent because its argument
} // is dependent
};

who can tell me ,"this->E would not be valid syntax "
why?
 
A

Alf P. Steinbach

* lizhuo:
[with E a type]
who can tell me ,"this->E would not be valid syntax "
why?

this->SomeType is never valid syntax, because an object can't contain a
type: a class can contain a type, an object (instance of the class) can't.
 
V

Victor Bazarov

Alf said:
* lizhuo:
[with E a type]
who can tell me ,"this->E would not be valid syntax "
why?

this->SomeType is never valid syntax, because an object can't contain
a type: a class can contain a type, an object (instance of the class)
can't.

struct SomeType { int a; };
struct OtherType { double a; };
struct blah : SomeType, OtherType { void foo(); };

void blah::foo() {
this->SomeType ::a;
// ^^^^^^^^^^^^^^
}

V
 
S

Stuart Redmann

lizhuo said:
hi all:
I reading "C++ Templates: The Complete Guide "
Part II: Templates in Depth
he write:

template<typename T>
class B {
public:
enumE{e1=6,e2=28,e3=496};
virtual void zero(E e = e1);
virtual void one(E&);
};

template<typename T>
class D : public B<T> {
public:
void f() {
typename D<T>::E e; // this->E would not be valid syntax
this->zero(); // D<T>::zero() would inhibit virtuality
one(e); // one is dependent because its argument
} // is dependent
};

who can tell me ,"this->E would not be valid syntax "
why?

The operator-> is called the member selection operator. It is used to
select a member (non-static method or variable) of an object. Thus
this->E is invalid, as E is not the name of a method or variable of
class D. As Victor pointed out, this->B::eek:ne would be legal, as B:: in
this expression is used to denote the name scope of class B for looking
up the member one (which would mean that you want to invoke the method
'one' as it is defined for class B).

Regards,
Stuart
 
A

Alf P. Steinbach

* Victor Bazarov:
Alf said:
* lizhuo:
[with E a type]
who can tell me ,"this->E would not be valid syntax "
why?
this->SomeType is never valid syntax, because an object can't contain
a type: a class can contain a type, an object (instance of the class)
can't.

struct SomeType { int a; };
struct OtherType { double a; };
struct blah : SomeType, OtherType { void foo(); };

void blah::foo() {
this->SomeType ::a;
// ^^^^^^^^^^^^^^
}

:) Yeah yeah, but, for other readers, that's "this->SomeType::a",
referring to a non-type.

Cheerio,

- Alf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top