Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. More basic C++ questions - may I ask?

More basic C++ questions - may I ask?

Scheduled Pinned Locked Moved C / C++ / MFC
c++comgraphicshelpquestion
9 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Vaclav_
    wrote on last edited by
    #1

    I am still not too comfortable with passing variables to constructor, but eventually I'll get over it. But this got me stumped - never heard of having const used this way. Why? So how do you read this -X(x) with relation to following function double x()? Please if this is too basic for you just let someone else answer, OK? I am asking for help and have no intent to waste your time on trivia. Thanks class Point2d{ public: Point2d() {} Point2d(double x, double y) : X(x), Y(y) {} double x() const { return X; } double y() const { return Y; } /** * Returns the norm of this vector. * @return the norm */ double norm() const { return sqrt( X * X + Y * Y ); } void setCoords(double x, double y) { X = x; Y = y; }

    M L R 3 Replies Last reply
    0
    • V Vaclav_

      I am still not too comfortable with passing variables to constructor, but eventually I'll get over it. But this got me stumped - never heard of having const used this way. Why? So how do you read this -X(x) with relation to following function double x()? Please if this is too basic for you just let someone else answer, OK? I am asking for help and have no intent to waste your time on trivia. Thanks class Point2d{ public: Point2d() {} Point2d(double x, double y) : X(x), Y(y) {} double x() const { return X; } double y() const { return Y; } /** * Returns the norm of this vector. * @return the norm */ double norm() const { return sqrt( X * X + Y * Y ); } void setCoords(double x, double y) { X = x; Y = y; }

      M Offline
      M Offline
      Midi_Mick
      wrote on last edited by
      #2

      Using const that way in the declaration of the method tells the compiler that the method is able to be called on constant instances of that object. It indicates that the object is not altered in any way by calling that method. Examples using the class defined above:

      const Point2d p1(2,3);
      Point2d p2(4,5);

      p1.setCoords(3,2); // Compiler error. setCoords not declared as const method
      p2.setCoords(5,4); // OK

      double n = p1.norm(); // OK - norm declared as const method

      Cheers, Mick ------------------------------------------------ It doesn't matter how often or hard you fall on your arse, eventually you'll roll over and land on your feet.

      1 Reply Last reply
      0
      • V Vaclav_

        I am still not too comfortable with passing variables to constructor, but eventually I'll get over it. But this got me stumped - never heard of having const used this way. Why? So how do you read this -X(x) with relation to following function double x()? Please if this is too basic for you just let someone else answer, OK? I am asking for help and have no intent to waste your time on trivia. Thanks class Point2d{ public: Point2d() {} Point2d(double x, double y) : X(x), Y(y) {} double x() const { return X; } double y() const { return Y; } /** * Returns the norm of this vector. * @return the norm */ double norm() const { return sqrt( X * X + Y * Y ); } void setCoords(double x, double y) { X = x; Y = y; }

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        It's all in the documentation: https://msdn.microsoft.com/en-us/library/07x6b05d.aspx[^].

        V 1 Reply Last reply
        0
        • L Lost User

          It's all in the documentation: https://msdn.microsoft.com/en-us/library/07x6b05d.aspx[^].

          V Offline
          V Offline
          Vaclav_
          wrote on last edited by
          #4

          The link is broken. Thanks anyway

          L 1 Reply Last reply
          0
          • V Vaclav_

            The link is broken. Thanks anyway

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            It was working this morning, because I read the content to see that it answered the question. Most likely a problem at Microsoft's end; keep trying. Or just use Google to find an alternate page.CodeProject bug, link now fixed.

            G 1 Reply Last reply
            0
            • L Lost User

              It was working this morning, because I read the content to see that it answered the question. Most likely a problem at Microsoft's end; keep trying. Or just use Google to find an alternate page.CodeProject bug, link now fixed.

              G Offline
              G Offline
              Graham Breach
              wrote on last edited by
              #6

              There's a colon missing between the "https" and "//" in your link - but it's there in the link text. Maybe this a CodeProject bug?

              L 1 Reply Last reply
              0
              • G Graham Breach

                There's a colon missing between the "https" and "//" in your link - but it's there in the link text. Maybe this a CodeProject bug?

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Thanks, there is a message in Bugs & Suggestions about this, but it is supposed to be fixed now. I will edit my response.

                1 Reply Last reply
                0
                • V Vaclav_

                  I am still not too comfortable with passing variables to constructor, but eventually I'll get over it. But this got me stumped - never heard of having const used this way. Why? So how do you read this -X(x) with relation to following function double x()? Please if this is too basic for you just let someone else answer, OK? I am asking for help and have no intent to waste your time on trivia. Thanks class Point2d{ public: Point2d() {} Point2d(double x, double y) : X(x), Y(y) {} double x() const { return X; } double y() const { return Y; } /** * Returns the norm of this vector. * @return the norm */ double norm() const { return sqrt( X * X + Y * Y ); } void setCoords(double x, double y) { X = x; Y = y; }

                  R Offline
                  R Offline
                  Ratul Thakur
                  wrote on last edited by
                  #8

                  *i am also new to C++* i'm not able to figure out where are X and Y defined i mean the capital ones.

                  D 1 Reply Last reply
                  0
                  • R Ratul Thakur

                    *i am also new to C++* i'm not able to figure out where are X and Y defined i mean the capital ones.

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #9

                    Ratul Thakur wrote:

                    i'm not able to figure out where are X and Y defined

                    They aren't. The OP's code is incomplete and thus will not compile.

                    "One man's wage rise is another man's price increase." - Harold Wilson

                    "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                    "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups