The following program is related to template...
include <iostream>
using namespace std;
template<class T, int size = 100>
class Array
{
T array[size];
public:
T& operator[](int index)
{
cout<<"Array:--"<<endl;
return array[index];
}
int length() const { return size; }
};
class Number
{
float f;
public:
Number(float ff = 0.0f) : f(ff)
{
cout<<"NUMBER CONST..."<<endl;
}
Number& operator=(const Number& n)
{
cout<<"NUMBER:--"<<endl;
f = n.f;
return *this;
}
operator float() const
{
return f;
}
friend ostream& operator<<(ostream& os, const Number& x)
{
return os << x.f;
}
};
template<class T, int size = 2>
class Holder
{
Array<T, size>* np;
public:
Holder() : np(0) {
cout<<"HOLDER CONST..."<<endl;
}
T& operator[](int i)
{
cout<<"HOLDER:--"<<endl;
if(!np) np = new Array<T, size>;
cout<<"AFTER ALLOCATION..."<<endl;
return np->operator[](i);
}
int length() const
{
return size;
}
~Holder() { delete np; }
};
int main()
{
Holder<Numberh;
for(int i = 0; i < 2; i++)
{
h[i] = i;
}
for(int j = 0; j < 2; j++)
cout << h[j] << endl;
} ///:~
output:
HOLDER CONST...
NUMBER CONST...
HOLDER:--
NUMBER CONST...
NUMBER CONST...
AFTER ALLOCATION...
Array:--
NUMBER:--
NUMBER CONST...
HOLDER:--
AFTER ALLOCATION...
Array:--
NUMBER:--
HOLDER:--
AFTER ALLOCATION...
Array:--
0
HOLDER:--
AFTER ALLOCATION...
Array:--
1
I want to know the working or flow of this program as per the output
that i have mentioned above.
Yashwant 7 1341
yashwant pinge wrote:
<snip>
>
output:
HOLDER CONST...
NUMBER CONST...
HOLDER:--
NUMBER CONST...
NUMBER CONST...
AFTER ALLOCATION...
Array:--
NUMBER:--
NUMBER CONST...
HOLDER:--
AFTER ALLOCATION...
Array:--
NUMBER:--
HOLDER:--
AFTER ALLOCATION...
Array:--
0
HOLDER:--
AFTER ALLOCATION...
Array:--
1
I want to know the working or flow of this program as per the output
that i have mentioned above.
What doesn't the output tell you?
--
Ian Collins.
In debug mode: run step by step.
"yashwant pinge" <ya***********@gmail.com>
??????:11*********************@p77g2000hsh.googleg roups.com...
The following program is related to template...
include <iostream>
using namespace std;
template<class T, int size = 100>
class Array
{
T array[size];
public:
T& operator[](int index)
{
cout<<"Array:--"<<endl;
return array[index];
}
int length() const { return size; }
};
class Number
{
float f;
public:
Number(float ff = 0.0f) : f(ff)
{
cout<<"NUMBER CONST..."<<endl;
}
Number& operator=(const Number& n)
{
cout<<"NUMBER:--"<<endl;
f = n.f;
return *this;
}
operator float() const
{
return f;
}
friend ostream& operator<<(ostream& os, const Number& x)
{
return os << x.f;
}
};
template<class T, int size = 2>
class Holder
{
Array<T, size>* np;
public:
Holder() : np(0) {
cout<<"HOLDER CONST..."<<endl;
}
T& operator[](int i)
{
cout<<"HOLDER:--"<<endl;
if(!np) np = new Array<T, size>;
cout<<"AFTER ALLOCATION..."<<endl;
return np->operator[](i);
}
int length() const
{
return size;
}
~Holder() { delete np; }
};
int main()
{
Holder<Numberh;
for(int i = 0; i < 2; i++)
{
h[i] = i;
}
for(int j = 0; j < 2; j++)
cout << h[j] << endl;
} ///:~
output:
HOLDER CONST...
NUMBER CONST...
HOLDER:--
NUMBER CONST...
NUMBER CONST...
AFTER ALLOCATION...
Array:--
NUMBER:--
NUMBER CONST...
HOLDER:--
AFTER ALLOCATION...
Array:--
NUMBER:--
HOLDER:--
AFTER ALLOCATION...
Array:--
0
HOLDER:--
AFTER ALLOCATION...
Array:--
1
I want to know the working or flow of this program as per the output
that i have mentioned above.
Yashwant
On Mar 29, 10:14 am, "mos" <mmosqu...@163.comwrote:
In debug mode: run step by step.
"yashwant pinge" <yashwantpi...@gmail.com>
??????:1175141914.939198.78...@p77g2000hsh.googleg roups.com...
The following program is related to template...
include <iostream>
using namespace std;
template<class T, int size = 100>
class Array
{
T array[size];
public:
T& operator[](int index)
{
cout<<"Array:--"<<endl;
return array[index];
}
int length() const { return size; }
};
class Number
{
float f;
public:
Number(float ff = 0.0f) : f(ff)
{
cout<<"NUMBER CONST..."<<endl;
}
Number& operator=(const Number& n)
{
cout<<"NUMBER:--"<<endl;
f = n.f;
return *this;
}
operator float() const
{
return f;
}
friend ostream& operator<<(ostream& os, const Number& x)
{
return os << x.f;
}
};
template<class T, int size = 2>
class Holder
{
Array<T, size>* np;
public:
Holder() : np(0) {
cout<<"HOLDER CONST..."<<endl;
}
T& operator[](int i)
{
cout<<"HOLDER:--"<<endl;
if(!np) np = new Array<T, size>;
cout<<"AFTER ALLOCATION..."<<endl;
return np->operator[](i);
}
int length() const
{
return size;
}
~Holder() { delete np; }
};
int main()
{
Holder<Numberh;
for(int i = 0; i < 2; i++)
{
h[i] = i;
}
for(int j = 0; j < 2; j++)
cout << h[j] << endl;
} ///:~
output:
HOLDER CONST...
NUMBER CONST...
HOLDER:--
NUMBER CONST...
NUMBER CONST...
AFTER ALLOCATION...
Array:--
NUMBER:--
NUMBER CONST...
HOLDER:--
AFTER ALLOCATION...
Array:--
NUMBER:--
HOLDER:--
AFTER ALLOCATION...
Array:--
0
HOLDER:--
AFTER ALLOCATION...
Array:--
1
I want to know the working or flow of this program as per the output
that i have mentioned above.
Yashwant
I dont know detail about GDB
Would you tell me the steps or commands to debug the program into gdb
* yashwant pinge:
>>Yashwant
I dont know detail about GDB
Would you tell me the steps or commands to debug the program into gdb
That's off-topic in this group. Please READ THE FAQ before posting.
--
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?
Why is the Number class constructor called during the statement h[i]=i
in main function
This is actually an unteresting question
this is what got using gdb step by step.
(gdb) c
Continuing.
Breakpoint 2, main () at 4.cpp:68
68 Holder<Numberh;
(gdb) c
Continuing.
HOLDER CONST...
Breakpoint 3, main () at 4.cpp:69
69 for(int i = 0; i < 2; i++)
(gdb) c
Continuing.
Breakpoint 4, main () at 4.cpp:71
71 h[i] = i;
(gdb) c
Continuing.
Breakpoint 5, Number (this=0x1, ff=-1.99943781) at 4.cpp:22
22 Number(float ff = 0.0f) : f(ff)
(gdb) c
Continuing.
Breakpoint 6, Number (this=0xbfffed28, ff=0) at 4.cpp:24
24 cout<<"NUMBER CONST..."<<endl;
(gdb) c
Continuing.
NUMBER CONST...
Breakpoint 7, Holder<Number, 2>::operator[](int) (this=0x1,
i=-1073746540) at 4.cpp:53
53 {
(gdb)
as you can see the code snippet
for(int i = 0; i < 2; i++)
h[i] = i; calls the constructor for class NUMBER
twice.i dont understand this myself why a constructor would be called
during template initialization .
pratap wrote:
This is actually an unteresting question
this is what got using gdb step by step.
(gdb) c
Continuing.
Breakpoint 2, main () at 4.cpp:68
68 Holder<Numberh;
(gdb) c
Continuing.
HOLDER CONST...
Breakpoint 3, main () at 4.cpp:69
69 for(int i = 0; i < 2; i++)
(gdb) c
Continuing.
Breakpoint 4, main () at 4.cpp:71
71 h[i] = i;
(gdb) c
Continuing.
Breakpoint 5, Number (this=0x1, ff=-1.99943781) at 4.cpp:22
22 Number(float ff = 0.0f) : f(ff)
(gdb) c
Continuing.
Breakpoint 6, Number (this=0xbfffed28, ff=0) at 4.cpp:24
24 cout<<"NUMBER CONST..."<<endl;
(gdb) c
Continuing.
NUMBER CONST...
Breakpoint 7, Holder<Number, 2>::operator[](int) (this=0x1,
i=-1073746540) at 4.cpp:53
53 {
(gdb)
as you can see the code snippet
for(int i = 0; i < 2; i++)
h[i] = i; calls the constructor for class NUMBER
twice.i dont understand this myself why a constructor would be called
during template initialization .
It is because h[i] stores Number types, and you are trying to store
ints. At that line, a type conversion occurs, and the Number constructor
has to be called to create object of the Number type This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: zhou |
last post by:
Hi there,
We have a compiler specific issue which requires us to force template instantiation. This works fine. The problem comes
when I try using std:find() on vector. Since vector has no member...
|
by: Surya Kiran |
last post by:
Hi all,
I've written a function template. say
template <class T>
fn (T var) { ... }
Is there any way, from within the function, can we check what type of
argument we've passed on to the...
|
by: Thomas Matthews |
last post by:
Hi,
I am converting my table and record classes into templates.
My issue is the syntax of declaring a friend class within
the template. I have searched the C++ FAQ Lite (web),
the C++...
|
by: Lionel B |
last post by:
Greetings,
I am trying to implement "element-wise" arithmetic operators for a class
along the following lines (this is a simplified example):
// ----- BEGIN CODE -----
struct X
{
int a,b;
|
by: Eric Lilja |
last post by:
Ok, this code doesn't compile:
#include <iostream>
#include <ostream> /* Just for you, Mike :-) */
template<typename T>
class Couple
{
public:
Couple(const T& ax, const T& ay) : x(ax), y(ay)...
|
by: Sven Groot |
last post by:
This was posted by someone in comp.lang.c++, and later in
microsoft.public.vstudio.general, but since I know Carl is in this group,
and he's the one that should read this, I've reposted it here....
|
by: mathieu |
last post by:
Hello,
I did read the FAQ on template(*), since I could not find an answer
to my current issue I am posting here. I have tried to summarize my
issue in the following code (**).
Basically I am...
|
by: Peter Olcott |
last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a
I think that the operator() member function does not work correctly, does
anyone else know how to make a template for making two...
|
by: bogus1one |
last post by:
Hi All
Given the following:
// NamespaceTemplate.cpp : Defines the entry point for the console
application.
//
#include "stdafx.h"
|
by: Lawrence Spector |
last post by:
I ran into a problem using g++. Visual Studio 2005 never complained
about this, but with g++ I ran into this error. I can't figure out if
I've done something wrong or if this is a compiler bug. ...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
| |