By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,853 Members | 1,027 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,853 IT Pros & Developers. It's quick & easy.

Wierd template class

P: n/a
I am confused about the use of the template parameter "E" in the below
class. Since when is it allowed to use these parameters like "E(1)" and
what does it mean (where can I read more about this kind of use)?
template <typename E>
class Mytest {
public:
Mytest(int n) {
s = E(0);
a.resize(n,E(0));

}
void operator()() {
std::cout << "wierd operator" << std::endl;

}

int primal() {

return 34;
}

private:
E s;
std::vector<Ea;

};
The class above has a constructor "Mytest(int n)". But it also has the
freak of nature function: void operator()().

It seems that it is declaring a new operator "()" that in this case
prints "wierd operator" when called on an instance of the class, like:

Mytest<intmy(4);
my(); // executes cout in void operator block.

But if I change it to:

void operator()@ {

std::cout << "wierd operator" << std::endl;

}

I get:

main.cpp:46: error: stray @ in program

So what does the "operator" keyword really do?
Apr 25 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
desktop wrote:
I am confused about the use of the template parameter "E" in the below
class. Since when is it allowed to use these parameters like "E(1)"
and what does it mean (where can I read more about this kind of use)?
template <typename E>
class Mytest {
public:
Mytest(int n) {
s = E(0);
a.resize(n,E(0));

}
I would have written this constructor this way:

Mytest(int n) : s(0), a(n, E(0)) {}
>

void operator()() {
std::cout << "wierd operator" << std::endl;

}

int primal() {

return 34;
}

private:
E s;
std::vector<Ea;

};
The class above has a constructor "Mytest(int n)". But it also has the
freak of nature function: void operator()().
It's a "function call operator".
>
It seems that it is declaring a new operator "()" that in this case
prints "wierd operator" when called on an instance of the class, like:

Mytest<intmy(4);
my(); // executes cout in void operator block.

But if I change it to:

void operator()@ {
That's not C++.
>
std::cout << "wierd operator" << std::endl;

}

I get:

main.cpp:46: error: stray @ in program

So what does the "operator" keyword really do?
It designates the function as an overloaded operator of certain
notation.

What book are you reading that doesn't explain operator overloading?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 25 '07 #2

P: n/a
desktop wrote:
I am confused about the use of the template parameter "E" in the below
class. Since when is it allowed to use these parameters like "E(1)" and
what does it mean (where can I read more about this kind of use)?
It means construct an E with a constructor that takes an integer as a param.

If MyTest is instantiated with a class that does not have such a
constructor, the compiler should complain.

>

template <typename E>
class Mytest {
public:
Mytest(int n) {
s = E(0);
a.resize(n,E(0));

}
void operator()() {
std::cout << "wierd operator" << std::endl;

}

int primal() {

return 34;
}

private:
E s;
std::vector<Ea;

};
Apr 25 '07 #3

P: n/a
Victor Bazarov wrote:
desktop wrote:
>I am confused about the use of the template parameter "E" in the below
class. Since when is it allowed to use these parameters like "E(1)"
and what does it mean (where can I read more about this kind of use)?
template <typename E>
class Mytest {
public:
Mytest(int n) {
s = E(0);
a.resize(n,E(0));

}

I would have written this constructor this way:

Mytest(int n) : s(0), a(n, E(0)) {}
>>
void operator()() {
std::cout << "wierd operator" << std::endl;

}

int primal() {

return 34;
}

private:
E s;
std::vector<Ea;

};
The class above has a constructor "Mytest(int n)". But it also has the
freak of nature function: void operator()().

It's a "function call operator".
>It seems that it is declaring a new operator "()" that in this case
prints "wierd operator" when called on an instance of the class, like:

Mytest<intmy(4);
my(); // executes cout in void operator block.

But if I change it to:

void operator()@ {

That's not C++.
>std::cout << "wierd operator" << std::endl;

}

I get:

main.cpp:46: error: stray @ in program

So what does the "operator" keyword really do?

It designates the function as an overloaded operator of certain
notation.

What book are you reading that doesn't explain operator overloading?

V
Ok the void operator()(){} now contains this code:
void operator()() {
typedef typename std::vector<E>::iterator R;
R answer = NAMESPACE::find(a.begin(), a.end(), s);
}

But R is not defined as a template parameter in the template class
definition:

template <typename E>
class Mytest {
public:
Mytest(int n) {
s = E(0);
a.resize(n,E(0));

}

void operator()() {
typedef typename std::vector<E>::iterator R;
R answer = ::find(a.begin(), a.end(), s);
}

private:
E s;
std::vector<Ea;

};

In this context I assume R is just a name for the iterator, but how can
an iterator have 2 names: "R answer = ...."?
Apr 26 '07 #4

P: n/a
desktop wrote:
>
Ok the void operator()(){} now contains this code:
void operator()() {
typedef typename std::vector<E>::iterator R;
R answer = NAMESPACE::find(a.begin(), a.end(), s);
}

But R is not defined as a template parameter in the template class
definition:
Why should it be? Remember R is a new name for the type
std::vector<E>::iterator (the type itself is defined in std::vector). It
is *not* the name of a variable of that type.

In this context I assume R is just a name for the iterator, but how can
an iterator have 2 names: "R answer = ...."?
See above.

Regards,
Sumit.
--
Sumit Rajan <su*********@gmail.com>
Apr 26 '07 #5

P: n/a
desktop wrote:
[..]
Ok the void operator()(){} now contains this code:
void operator()() {
typedef typename std::vector<E>::iterator R;
R answer = NAMESPACE::find(a.begin(), a.end(), s);
}

But R is not defined as a template parameter in the template class
definition:
Uh... It's defined on the line *immediately preceding* the line on
which it's used. R is a typedef-id.
>
template <typename E>
class Mytest {
public:
Mytest(int n) {
s = E(0);
a.resize(n,E(0));

}

void operator()() {
typedef typename std::vector<E>::iterator R;
R answer = ::find(a.begin(), a.end(), s);
}

private:
E s;
std::vector<Ea;

};

In this context I assume R is just a name for the iterator, but how
can an iterator have 2 names: "R answer = ...."?
What book are you reading that doesn't explain how 'typedef' works?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 26 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.