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. Is one way better than the other?

Is one way better than the other?

Scheduled Pinned Locked Moved C / C++ / MFC
testingbeta-testingquestion
12 Posts 7 Posters 1 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.
  • M Maximilien

    Huh ?

    CString strText("Testing");

    or did I missed something ?


    Maximilien Lincourt "Never underestimate the bandwidth of a station wagon filled with backup tapes." ("Computer Networks" by Andrew S Tannenbaum )

    D Offline
    D Offline
    dog_spawn
    wrote on last edited by
    #3

    You have not missed anything :) That guy is clearly a nutjob.

    R 1 Reply Last reply
    0
    • M Maximilien

      Huh ?

      CString strText("Testing");

      or did I missed something ?


      Maximilien Lincourt "Never underestimate the bandwidth of a station wagon filled with backup tapes." ("Computer Networks" by Andrew S Tannenbaum )

      R Offline
      R Offline
      RobJones
      wrote on last edited by
      #4

      I was just trying to make the question simple.. I have a structure that contains chars, int's etc... I send the structure across a socket.. when it's received on the other side I I want to put the char (from the structure) into a CString. so my question was is it ok to to do a CString strTest = sTest or should I use the format function provided by CString? I guess I just tried to over simplify the question... Rob Whoever said nothing's impossible never tried slamming a revolving door!

      B 1 Reply Last reply
      0
      • D dog_spawn

        You have not missed anything :) That guy is clearly a nutjob.

        R Offline
        R Offline
        RobJones
        wrote on last edited by
        #5

        Thanks, your post really helps. I love it when people are nice. Whoever said nothing's impossible never tried slamming a revolving door!

        D 1 Reply Last reply
        0
        • R RobJones

          I was just trying to make the question simple.. I have a structure that contains chars, int's etc... I send the structure across a socket.. when it's received on the other side I I want to put the char (from the structure) into a CString. so my question was is it ok to to do a CString strTest = sTest or should I use the format function provided by CString? I guess I just tried to over simplify the question... Rob Whoever said nothing's impossible never tried slamming a revolving door!

          B Offline
          B Offline
          Brad Sokol
          wrote on last edited by
          #6

          The following two lines are equivalent: std::string s1("one"); // Example 1 std::string s2 = "two"; // Example 2 If I recall correctly, there is a standard "short-cut" that compilers can (must?) take to make example 2 above work like example 1. In other words, declaration with assignment is optimised to a constructor provided that an appropriatly overloaded constructor exists. If no constructor, you get a compiler error. Either would likely be faster then default constructor followed by Format() for two reasons. First, it's two steps. Second, Format() is probably slow with all that string parsing. Of course, YMMV. Brad

          M 1 Reply Last reply
          0
          • B Brad Sokol

            The following two lines are equivalent: std::string s1("one"); // Example 1 std::string s2 = "two"; // Example 2 If I recall correctly, there is a standard "short-cut" that compilers can (must?) take to make example 2 above work like example 1. In other words, declaration with assignment is optimised to a constructor provided that an appropriatly overloaded constructor exists. If no constructor, you get a compiler error. Either would likely be faster then default constructor followed by Format() for two reasons. First, it's two steps. Second, Format() is probably slow with all that string parsing. Of course, YMMV. Brad

            M Offline
            M Offline
            Maximilien
            wrote on last edited by
            #7

            will #2 generate a copy constructor ? or will it be converted into constructor ?


            Maximilien Lincourt "Never underestimate the bandwidth of a station wagon filled with backup tapes." ("Computer Networks" by Andrew S Tannenbaum )

            M 1 Reply Last reply
            0
            • M Maximilien

              will #2 generate a copy constructor ? or will it be converted into constructor ?


              Maximilien Lincourt "Never underestimate the bandwidth of a station wagon filled with backup tapes." ("Computer Networks" by Andrew S Tannenbaum )

              M Offline
              M Offline
              Michael Dunn
              wrote on last edited by
              #8

              std::string s1("one"); // Example 1 std::string s2 = "two"; // Example 2 Those two lines are identical. They both call a one-parameter ctor. Even though line 2 uses =, it does not call operator = --Mike-- Ericahist | Homepage | RightClick-Encrypt | 1ClickPicGrabber Ericahist updated Aug 30!

              1 Reply Last reply
              0
              • R RobJones

                Hello, Just wondering if one of these ways is better than the other? And why?

                char sTest[256];
                lstrcpy(sTest,"testing");
                CString strTest = sTest;

                or

                CString strTest;
                strTest.Format("%s",sTest);

                Thanks, Rob Whoever said nothing's impossible never tried slamming a revolving door!

                M Offline
                M Offline
                Michael Dunn
                wrote on last edited by
                #9

                Format() in this case is the worse choice of the two, because it's a bit harder to read. With the plain assignment, it's clearer what the code is doing. Format() also will run a bit slower because it has to parse the format string and the other arguments in order to calculate the length of the resulting string, although the slowness is nothing a person would even notice. --Mike-- Ericahist | Homepage | RightClick-Encrypt | 1ClickPicGrabber Ericahist updated Aug 30!

                1 Reply Last reply
                0
                • R RobJones

                  Thanks, your post really helps. I love it when people are nice. Whoever said nothing's impossible never tried slamming a revolving door!

                  D Offline
                  D Offline
                  dog_spawn
                  wrote on last edited by
                  #10

                  No problem. Stupid question deserves a stupid reply :)

                  A 1 Reply Last reply
                  0
                  • D dog_spawn

                    No problem. Stupid question deserves a stupid reply :)

                    A Offline
                    A Offline
                    Alvaro Mendez
                    wrote on last edited by
                    #11

                    Well, we'd rather have the stupid people making the stupid replies shut the f*ck up. Then once they become smart enough to realize that acting like an asshole is stupid, their contributions will be more than welcome to those who need help. Regards, Alvaro


                    Hey! It compiles! Ship it.

                    1 Reply Last reply
                    0
                    • R RobJones

                      Hello, Just wondering if one of these ways is better than the other? And why?

                      char sTest[256];
                      lstrcpy(sTest,"testing");
                      CString strTest = sTest;

                      or

                      CString strTest;
                      strTest.Format("%s",sTest);

                      Thanks, Rob Whoever said nothing's impossible never tried slamming a revolving door!

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

                      M. Dunn has the right response. If you are just assigning a simple string of characters to the CString object, using the assignment operator is the easiest. Employ the use of Format() only when variables/values need to be substituted.

                      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