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. Initializing an Array inside a Class?

Initializing an Array inside a Class?

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structures
5 Posts 3 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.
  • C Offline
    C Offline
    CreepingFeature
    wrote on last edited by
    #1

    Hi everyone! Got a quick, yet stupid question! Why cant you init anything inside a class unless its static const int? Actualy the question is how can you initialize an array inside class private: to be const. I know that it is possible to declare an array and then use a constructor to init it. But how do I make it const if I was to do it thru the constructor. Thanks.

    A 1 Reply Last reply
    0
    • C CreepingFeature

      Hi everyone! Got a quick, yet stupid question! Why cant you init anything inside a class unless its static const int? Actualy the question is how can you initialize an array inside class private: to be const. I know that it is possible to declare an array and then use a constructor to init it. But how do I make it const if I was to do it thru the constructor. Thanks.

      A Offline
      A Offline
      Arsalan Malik
      wrote on last edited by
      #2

      You can declare an array inside private block, e.g. class A { private: static const int array[10]; }; and initialize outside class as: const A::array[10] = {0}; As far as I know, you cannot initialize an const array using constructor list. Arsalan Malik

      R C 2 Replies Last reply
      0
      • A Arsalan Malik

        You can declare an array inside private block, e.g. class A { private: static const int array[10]; }; and initialize outside class as: const A::array[10] = {0}; As far as I know, you cannot initialize an const array using constructor list. Arsalan Malik

        R Offline
        R Offline
        Rory Solley
        wrote on last edited by
        #3

        Although the former is valid C++ syntax, I think the VC6 compiler will have issues with it.

        A 1 Reply Last reply
        0
        • R Rory Solley

          Although the former is valid C++ syntax, I think the VC6 compiler will have issues with it.

          A Offline
          A Offline
          Arsalan Malik
          wrote on last edited by
          #4

          I have successfully compiled this syntax with VC6, and found no issues. Arsalan Malik

          1 Reply Last reply
          0
          • A Arsalan Malik

            You can declare an array inside private block, e.g. class A { private: static const int array[10]; }; and initialize outside class as: const A::array[10] = {0}; As far as I know, you cannot initialize an const array using constructor list. Arsalan Malik

            C Offline
            C Offline
            CreepingFeature
            wrote on last edited by
            #5

            Thanks a lot!!! :-D

            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