PDA

View Full Version : Please help me to understand the Code !

Krish
22nd February 2008, 10:09
Hello! Everybody,
I started to unuderstand the codings in simple_plot example wherein i tried to understand simple.cpp code but had few problems. Those are: -

1.) In this code: -

public:
SimpleData(double(*y)(double), size_t size):
d_size(size),
d_y(y)
{
}
Here i came to know that its a constructor with 2 variables, d_size & d_y are declared globally. But main point is i didn't understand " double(*y)(double) " declaration. Also is size_t a new variable introduced in Qwt, if yes what are its limitations?

2.) In this code: -

Const int nPoints=100;
cSin->setData(SimpleData(::sin, nPoints));
cCos->setData(SimpleData(::cos, nPoints));

Here i came to know that nPoints is an int variable with value=100. Also following 2 lines are setting data for those 2 curves. But main point is what type of arguments are sent here like "::sin" "::cos"& which subroutine is called to set the data actually?

Can anyone please help me out in understanding them.----> I will be obliged.

Thanks in Advance.

jacek
22nd February 2008, 15:11
d_size & d_y are declared globally.
These aren't global variables, but member variables.

But main point is i didn't understand " double(*y)(double) " declaration.

\$ cdecl explain "double(*y)(double)"
declare y as pointer to function (double) returning double

Also is size_t a new variable introduced in Qwt, if yes what are its limitations?
It isn't a variable, but a type. It's declared in the standard library.

Krish
25th February 2008, 09:01
Hello! Jacek,
Thanks for taking out time & replying me. But you didn't explain everything in detail like how does the following work: -

//Create sin and cos data
const int nPoints = 100;
csin->setdata(SimpleData(::sin,nPoints));
ccos->setdata(SimpleData(::cos,nPoints));

As per me after this the constructor is called but what type of argument is "::sin" or ::cos"????
after this i think we get d_size = 100, and d_y = ????
Also how do we get the data to be ploted like actual calculation?

And as you said that in "double(*y)(double)" y is set as pointer to the function (double ), but usually function is declared as "double();" way or not?

I will be obliged if helped or please tell me a book where i can find explanation's of these kind of statements.

Thanks in advance.

jpn
25th February 2008, 09:05
::sin and ::cos are pointers to functions declared in global namespace.

Krish
25th February 2008, 09:26
Hello! Jpn,
Thanks for replying. Ok so if those are pointers then after constructor: -
d_size = 100, & d_y = ::sin or ::cos?

But buddy how actually data is created like value of sinc & cos at 100? Please tell me where can i read about such complex routines or functions or declarations. Or is there any book. I will be obliged.--------> Thanks in Advance.

Gopala Krishna
25th February 2008, 09:41
Hello! Jpn,
Thanks for replying. Ok so if those are pointers then after constructor: -
d_size = 100, & d_y = ::sin or ::cos?

But buddy how actually data is created like value of sinc & cos at 100? Please tell me where can i read about such complex routines or functions or declarations. Or is there any book. I will be obliged.--------> Thanks in Advance.

When you call setdata, i think(coz i haven't used qwt before) there will be an internal loop which call the function d_y on each element in array/container.
something like

for(int i=0 ; i < d_size; i++)
point[i].setY( (*d_y)(point[i].x()) ); //syntax used to call a function thru pointer to function.

Uwe
25th February 2008, 10:52
But buddy how actually data is created like value of sinc & cos at 100? Please tell me where can i read about such complex routines or functions or declarations.
QwtData is an abstract API to connect data to a curve. It returns the number of points and the x/y values of each point. QwtPlotCurve requests these points one by one, when it paints itsself.

The SimpleData class demonstrates an implementation of a QwtData class, that calculates its values on the fly without storing anything. It is initialized to return 100 points, where f.e the 6th point is ( 0.5, sin(0,5) ).

Function pointers were used in good old C very often and because sin/cos are C-functions it is used here too.

Uwe