Results 1 to 7 of 7

Thread: Please help me to understand the Code !

  1. #1
    Join Date
    Feb 2008
    Posts
    74
    Thanks
    31
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Unhappy Please help me to understand the Code !

    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: -
    Qt Code:
    1. public:
    2. SimpleData(double(*y)(double), size_t size):
    3. d_size(size),
    4. d_y(y)
    5. {
    6. }
    To copy to clipboard, switch view to plain text mode 
    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: -
    Qt Code:
    1. Const int nPoints=100;
    2. cSin->setData(SimpleData(::sin, nPoints));
    3. cCos->setData(SimpleData(::cos, nPoints));
    To copy to clipboard, switch view to plain text mode 
    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.
    Last edited by Krish; 22nd February 2008 at 10:58.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Please help me to understand the Code !

    Quote Originally Posted by Krish View Post
    d_size & d_y are declared globally.
    These aren't global variables, but member variables.

    Quote Originally Posted by Krish View Post
    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
    Quote Originally Posted by Krish View Post
    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.

  3. The following user says thank you to jacek for this useful post:

    Krish (25th February 2008)

  4. #3
    Join Date
    Feb 2008
    Posts
    74
    Thanks
    31
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Exclamation Re: Please help me to understand the Code !

    Hello! Jacek,
    Thanks for taking out time & replying me. But you didn't explain everything in detail like how does the following work: -
    Qt Code:
    1. //Create sin and cos data
    2. const int nPoints = 100;
    3. csin->setdata(SimpleData(::sin,nPoints));
    4. ccos->setdata(SimpleData(::cos,nPoints));
    To copy to clipboard, switch view to plain text mode 
    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.
    Last edited by jpn; 25th February 2008 at 09:04. Reason: missing [code] tags

  5. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Please help me to understand the Code !

    ::sin and ::cos are pointers to functions declared in global namespace.
    J-P Nurmi

  6. The following user says thank you to jpn for this useful post:

    Krish (25th February 2008)

  7. #5
    Join Date
    Feb 2008
    Posts
    74
    Thanks
    31
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Lightbulb Re: Please help me to understand the Code !

    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.

  8. #6
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Please help me to understand the Code !

    Quote Originally Posted by Krish View Post
    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
    Qt Code:
    1. for(int i=0 ; i < d_size; i++)
    2. point[i].setY( (*d_y)(point[i].x()) ); //syntax used to call a function thru pointer to function.
    To copy to clipboard, switch view to plain text mode 
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  9. The following user says thank you to Gopala Krishna for this useful post:

    Krish (25th February 2008)

  10. #7
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,312
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Please help me to understand the Code !

    Quote Originally Posted by Krish View Post
    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

  11. The following user says thank you to Uwe for this useful post:

    Krish (25th February 2008)

Similar Threads

  1. Qt 4.3.0 lots of Bugs!
    By VireX in forum Qt Programming
    Replies: 69
    Last Post: 20th June 2007, 22:05
  2. Qt Cryptographic Architecture
    By vermarajeev in forum Qt Programming
    Replies: 6
    Last Post: 9th February 2007, 13:15
  3. Problem closing a QMainWindow in Qt4.2
    By ian in forum Qt Programming
    Replies: 11
    Last Post: 17th October 2006, 00:49
  4. problem with linking
    By mickey in forum Qt Programming
    Replies: 49
    Last Post: 12th August 2006, 21:41

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.