template issue

Y

yashwant pinge

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<Number> h;
for(int i = 0; i < 2; i++)
{
h = 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

Ian Collins

yashwant pinge wrote:

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?
 
Y

yashwant pinge

In debug mode: run step by step.

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<Number> h;
for(int i = 0; i < 2; i++)
{
h = i;
}
for(int j = 0; j < 2; j++)
cout << h[j] << endl;
} ///:~

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.


I dont know detail about GDB
Would you tell me the steps or commands to debug the program into gdb
 
A

Alf P. Steinbach

* yashwant pinge:
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.
 
Y

yashwant pinge

Why is the Number class constructor called during the statement h=i
in main function
 
P

pratap

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<Number> h;
(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;
(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>::eek:perator[](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; calls the constructor for class NUMBER
twice.i dont understand this myself why a constructor would be called
during template initialization .
 
A

anon

pratap said:
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<Number> h;
(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;
(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>::eek:perator[](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; 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 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,742
Latest member
AshliMayer

Latest Threads

Top