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. Managed C++/CLI
  4. variables initialization in the constructor

variables initialization in the constructor

Scheduled Pinned Locked Moved Managed C++/CLI
question
4 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.
  • C Offline
    C Offline
    chen_k
    wrote on last edited by
    #1

    let's say i have a class like this: class a { private: char name[10]; . . . } how do i give 'name' a value in the constructor? chen.

    C H N 3 Replies Last reply
    0
    • C chen_k

      let's say i have a class like this: class a { private: char name[10]; . . . } how do i give 'name' a value in the constructor? chen.

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      name = {"10 chars long"}; OR name = new char[10]; ::strcpy(&name[0], "value"); // going from memory, args could be the wrong way around If you use the second method, don't forget to release memory in the destructor. If you use the first, don't change the value of the string, which means it should be const. Overall, you'd do better to use a string class to hold the value, if practical. Christian Graus - Microsoft MVP - C++

      1 Reply Last reply
      0
      • C chen_k

        let's say i have a class like this: class a { private: char name[10]; . . . } how do i give 'name' a value in the constructor? chen.

        H Offline
        H Offline
        hamster1
        wrote on last edited by
        #3

        If you get the name from the outside, go with this: class a( const char* a_name) { strcpy(name,a_name); // note: unsafe ;-) } However, if you know the name at compile time already, simply hard-code it - gives more speed and security. class a { const char* get_name() const { return "blabla"; } } ---------------------- ~hamster1

        1 Reply Last reply
        0
        • C chen_k

          let's say i have a class like this: class a { private: char name[10]; . . . } how do i give 'name' a value in the constructor? chen.

          N Offline
          N Offline
          Nemanja Trifunovic
          wrote on last edited by
          #4

          Instead of using a char array, consider a string class:

          class a
          {
          private:
          std::string name;
          .
          .
          .
          };

          And then a constructor may look like:

          a::a(const char* yourName) : name(yourName)
          {}


          My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it. -- modified at 8:32 Tuesday 30th August, 2005

          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