473,786 Members | 2,410 Online
Bytes | Software Development & Data Engineering Community
+ 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 1720
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*********@gm ail.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
3917
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
3344
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 to turn a previously made String class that deals with char's into a templated String class that uses the template parameter C instead of char. I thought it would be fairly simple to do this exercise, but I encoutered many errors for my...
6
2161
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 binary search tree. Note there is a requirement for this to be a Win32/MFC "friendly" class, thus the use of CObject and POSITION. There is also a requirement for there not to be a separate iterator class. template <class TYPE, class ARG_TYPE =...
7
1266
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: parse error before `;' token Here is the code, am I missing something? #include <stdio.h> #include <stdlib.h>
9
1842
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------------- #ifndef FIB_HPP #define FIB_HPP #include <iostream> #include <iomanip> #include <cstdlib>
2
2020
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. This makes it difficult to spot errors. For example, F4 to "jump to next error" just jumps ot the next "template assistance" message. And since there are hundreds of them, this is quite obnoxious. Can I disable these things? Why is MSVC...
2
4507
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
3758
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 thawed it out. I built a console app using Microsoft Visual C++ 6 (VC++) and it worked great. Only one line in the header file had to be commented out. I built a console app using Borland C++ Builder 5. The linker complained of references to...
45
2935
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 to check. What I expected was that there would be minor code bloat and some speed improvement when using templates. However... I wrote a basic list container (using templates), and a list container (using virtual derived classes). I also tried...
0
9647
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9492
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10163
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10108
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8988
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7510
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4064
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 we have to send another system

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.