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. Vector of class with contractor

Vector of class with contractor

Scheduled Pinned Locked Moved C / C++ / MFC
graphicsquestion
4 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.
  • O Offline
    O Offline
    Ohad Cabiri
    wrote on last edited by
    #1

    Hello, I want to start a vector of calsses with the same contruct Lets say this is my class : class aaa { public: aaa(int i){ m_iA = i; } private: int m_iA; }; and I want now to do 5 classes with the same contruct: aaa* f = aaa(2)[5]; I have to do a loop for this (pointer of pointers etc.) ? Or it is possible to do this somehow in a short way ? Thanks,

    C M 2 Replies Last reply
    0
    • O Ohad Cabiri

      Hello, I want to start a vector of calsses with the same contruct Lets say this is my class : class aaa { public: aaa(int i){ m_iA = i; } private: int m_iA; }; and I want now to do 5 classes with the same contruct: aaa* f = aaa(2)[5]; I have to do a loop for this (pointer of pointers etc.) ? Or it is possible to do this somehow in a short way ? Thanks,

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      Ohad Cabiri wrote:

      I have to do a loop for this (pointer of pointers etc.) ?

      In fact if you don't have a default constructor, the only way to do that is to have an array of pointers to your class:

      aaa** pArray;
      ...
      ...
      pArray = new aaa*[5];

      And then loop through your array and create all the classes:

      for (int i=0;i<5;i++)
      pArray[i] = new aaa(Value);

      Of course, you will need to delete all these instances AND the array.


      Cédric Moonen Software developer
      Charting control [v1.1]

      O 1 Reply Last reply
      0
      • C Cedric Moonen

        Ohad Cabiri wrote:

        I have to do a loop for this (pointer of pointers etc.) ?

        In fact if you don't have a default constructor, the only way to do that is to have an array of pointers to your class:

        aaa** pArray;
        ...
        ...
        pArray = new aaa*[5];

        And then loop through your array and create all the classes:

        for (int i=0;i<5;i++)
        pArray[i] = new aaa(Value);

        Of course, you will need to delete all these instances AND the array.


        Cédric Moonen Software developer
        Charting control [v1.1]

        O Offline
        O Offline
        Ohad Cabiri
        wrote on last edited by
        #3

        So there is no shortcut here. Thanks

        1 Reply Last reply
        0
        • O Ohad Cabiri

          Hello, I want to start a vector of calsses with the same contruct Lets say this is my class : class aaa { public: aaa(int i){ m_iA = i; } private: int m_iA; }; and I want now to do 5 classes with the same contruct: aaa* f = aaa(2)[5]; I have to do a loop for this (pointer of pointers etc.) ? Or it is possible to do this somehow in a short way ? Thanks,

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

          If a statically-allocated array is acceptable, you can do:

          aaa stuff[] = { aaa(2), aaa(2), aaa(2), aaa(2), aaa(2) };

          --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ"); Ford, what's this fish doing in my ear?

          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