473,495 Members | 2,058 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Wierd template class

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
5 1701
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3903
by: Sebastian Faust | last post by:
Hi, I have 4 questions related to templates. I wanna do something like the following: template<typename T> class Template { public: Template_Test<T>()
1
3317
by: Oplec | last post by:
Hi, I'm learning C++ as a hobby using The C++ Programming Language : Special Edition by Bjarne Stroustrup. I'm working on chpater 13 exercises that deal with templates. Exercise 13.9 asks for me...
6
2143
by: Nobody | last post by:
This is sort of my first attempt at writing a template container class, just wanted some feedback if everything looks kosher or if there can be any improvements. This is a template class for a...
7
1253
by: yilled_fred | last post by:
Hi can anyone tell me why the following code cannot/doesn't compile? I get the following error while compiling (g++ -c ): "In member function `void derivedclass<S>::foo()': test.h:20 error:...
9
1816
by: Protoman | last post by:
I'm trying to print a complex number and i get this as output: Phi: (1.618034,0.000000). What does it mean? How do I get it to print normally? Here's the code: ----------fib.hpp-------------...
2
1995
by: Rudy Ray Moore | last post by:
Whenever I get any error with Vc++7.1/.net/2003, it is followed by huge ammounts of "template assistance" error messaging referencing template code (MTL) that has nothing to do with the error. ...
2
4486
by: Alfonso Morra | last post by:
I have a class declared as ff: class __declspec(dllexport) A { public: A() ; A(const A&) A& operator=(const A&) ; ~A() ; void doThis(void) ;
3
3736
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
45
2839
by: charles.lobo | last post by:
Hi, I have recently begun using templates in C++ and have found it to be quite useful. However, hearing stories of code bloat and assorted problems I decided to write a couple of small programs...
0
7120
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6991
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7196
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7373
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5456
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4897
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1405
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
286
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.