Connecting Tech Pros Worldwide Forums | Help | Site Map

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

lizhuo
Guest
 
Posts: n/a
#1: Dec 12 '06
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?


Alf P. Steinbach
Guest
 
Posts: n/a
#2: Dec 12 '06

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


* lizhuo:
Quote:
>
[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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Victor Bazarov
Guest
 
Posts: n/a
#3: Dec 12 '06

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


Alf P. Steinbach wrote:
Quote:
* lizhuo:
Quote:
>>
>[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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


Stuart Redmann
Guest
 
Posts: n/a
#4: Dec 12 '06

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


lizhuo wrote:
Quote:
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::one 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
Alf P. Steinbach
Guest
 
Posts: n/a
#5: Dec 12 '06

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


* Victor Bazarov:
Quote:
Alf P. Steinbach wrote:
Quote:
>* lizhuo:
Quote:
>>[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

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Closed Thread