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. How to initialize a variable in class

How to initialize a variable in class

Scheduled Pinned Locked Moved C / C++ / MFC
data-structurestutorialquestion
5 Posts 5 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.
  • 9 Offline
    9 Offline
    9ine
    wrote on last edited by
    #1

    How to initialize a variable array in a class? it should be static defined? class{ ... char array[] = "asdhajsdjasgdj"; ... }

    9ine

    P T H Z 4 Replies Last reply
    0
    • 9 9ine

      How to initialize a variable array in a class? it should be static defined? class{ ... char array[] = "asdhajsdjasgdj"; ... }

      9ine

      P Offline
      P Offline
      prasad_som
      wrote on last edited by
      #2

      9ine wrote:

      How to initialize a variable array in a class?

      Use member initialization list. But in above case varaible is array, so cant use member initialization list. In constructor body you can initialize(re) it.

      class A
      {
      int i;
      char array[1];
      public:
      A():i(0)
      {
      array[0]=0;
      }

      };

      9ine wrote:

      it should be static defined?

      Not necessary.

      Prasad Notifier using ATL

      1 Reply Last reply
      0
      • 9 9ine

        How to initialize a variable array in a class? it should be static defined? class{ ... char array[] = "asdhajsdjasgdj"; ... }

        9ine

        T Offline
        T Offline
        toxcct
        wrote on last edited by
        #3

        in C++ (by opposition to C# or java), almost all the members should be initialized in the constructor. the only exception are static members, which init should be done outside of the class definition.


        TOXCCT >>> GEII power

        [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

        1 Reply Last reply
        0
        • 9 9ine

          How to initialize a variable array in a class? it should be static defined? class{ ... char array[] = "asdhajsdjasgdj"; ... }

          9ine

          H Offline
          H Offline
          Hamid Taebi
          wrote on last edited by
          #4

          See Constructor/Destructor[^]

          _**


          **_

          WhiteSky


          1 Reply Last reply
          0
          • 9 9ine

            How to initialize a variable array in a class? it should be static defined? class{ ... char array[] = "asdhajsdjasgdj"; ... }

            9ine

            Z Offline
            Z Offline
            Zac Howland
            wrote on last edited by
            #5

            In general, you would initialize member variables as part of the Constructor (either in the implicit parameter list, or in the constructor). For the example you give, you may consider writing it the following way:

            // In the header file
            
            class MyClass
            {
            public:
            	MyClass() {}
            	~MyClass() {}
            
            	static const char* ms_MyString;
            };
            
            // In the implementation file
            
            const char* MyClass::ms_MyString = "0123456789";
            

            This gives you a single copy of the string regardless of the number of objects of this type you have. It will remain constant (that is, you can't change it), and it will remain in the same location in memory for the entire lifetime of your application. If you need to have an array that you can change, you would initialize it in the constructor:

            // In the header file
            
            class MyClass
            {
            public:
            	MyClass() : m_MyString(0) {memset(m_MyOtherString, 0, 100);}
            	~MyClass() {}
            
            	char* m_MyString; // pointer-style array
            	char m_MyOtherString[100]; // allocated array
            };
            

            If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

            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