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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. What is the difference between following declarations ?

What is the difference between following declarations ?

Scheduled Pinned Locked Moved C / C++ / MFC
question
5 Posts 4 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.
  • P Offline
    P Offline
    PrafullaVedante
    wrote on last edited by
    #1

    1 )

    int a;
    a = 5;

    2 )

    int a(5);

    3 )

    int a = 5;

    Prafulla Vedante

    L O 2 Replies Last reply
    0
    • P PrafullaVedante

      1 )

      int a;
      a = 5;

      2 )

      int a(5);

      3 )

      int a = 5;

      Prafulla Vedante

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

      In case 1 you have declared the variable a but it has no value; in reality it will but its contents will be random. The second statement assigns the value 5 to variable a. These two statements do not need to be together, as a can have its value set or reset anywhere it is in scope. Cases 2 and 3 are the same, the variable is declared and initialised at the same time.

      One of these days I'm going to think of a really clever signature.

      1 Reply Last reply
      0
      • P PrafullaVedante

        1 )

        int a;
        a = 5;

        2 )

        int a(5);

        3 )

        int a = 5;

        Prafulla Vedante

        O Offline
        O Offline
        Orjan Westin
        wrote on last edited by
        #3

        1. Declaration (after which the variable will be uninitialised, with an unknown value) followed by assignment. 2. Declaration with initialisation. For POD (Plain Old Data, built-in) types, this is effectively no different to 3. but with a class/struct type this would call a different constructor (taking an int parameter, rather than the default constructor). 3. Declaration followed by assignment. Again, for a POD type no different from 2. but with a class/struct type the value 5 could be implicitly converted to the class/struct type in a custom constructor before the assignment, or an assignment operator taking an int could be called. In questions of declaration, initialisation, assignment and so on, what POD types do is well known and specified in the standard, but what class/struct types do depend on their implementation to a much larger extent.

        S 1 Reply Last reply
        0
        • O Orjan Westin

          1. Declaration (after which the variable will be uninitialised, with an unknown value) followed by assignment. 2. Declaration with initialisation. For POD (Plain Old Data, built-in) types, this is effectively no different to 3. but with a class/struct type this would call a different constructor (taking an int parameter, rather than the default constructor). 3. Declaration followed by assignment. Again, for a POD type no different from 2. but with a class/struct type the value 5 could be implicitly converted to the class/struct type in a custom constructor before the assignment, or an assignment operator taking an int could be called. In questions of declaration, initialisation, assignment and so on, what POD types do is well known and specified in the standard, but what class/struct types do depend on their implementation to a much larger extent.

          S Offline
          S Offline
          Stefan_Lang
          wrote on last edited by
          #4

          Not quite correct. Cases 2 and 3 are equivalent - the assignment operator is, in fact, not used in case 3, not even for a custom class with an override for operator=(). See GotW#1 for further explanations. I have to admit I wouldn't have known the (lack of) difference before checking that site.

          O 1 Reply Last reply
          0
          • S Stefan_Lang

            Not quite correct. Cases 2 and 3 are equivalent - the assignment operator is, in fact, not used in case 3, not even for a custom class with an override for operator=(). See GotW#1 for further explanations. I have to admit I wouldn't have known the (lack of) difference before checking that site.

            O Offline
            O Offline
            Orjan Westin
            wrote on last edited by
            #5

            Ah, thank you.

            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