Initializing a private array member

This is a discussion on Initializing a private array member within the C++ Programming forums, part of the General Programming Boards category; How do i initialize array groups Code: class GroupManager { private: typedef std::vector<FleetGroup> Categories; typedef Categories::iterator GroupIterator; public: GroupManager(); GroupIterator ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    Initializing a private array member

    How do i initialize array groups

    Code:
    class GroupManager
    {
    private:
    
    	typedef std::vector<FleetGroup> Categories;
    	typedef Categories::iterator GroupIterator;
    
    public:
    
    	GroupManager();
    
    	GroupIterator find();
    	void ReadGroupFile();
    	void AddNewGroup();
    	void EditGroup();
    	void ViewAllGroups();
    	void ReadGroups();
    	void ViewAGroup();
    	void SaveGroupToFile();
    
    private:
    	Categories fleet;
    	std::string groups[MAX_GRP];
    };
    
    // MY ATTEMPT
    GroupManager::GroupManager() :
    groups = { "a", "b", "c", "d", "f", "o", "z" }
    {};
    
    //ERROR!!

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,660
    Do this in the constructor body. You need to use the copy algorithm, or a container that can take in a temporary array.

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Nope.. you will have to do it the way you are trying to avoid doing.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Confusing answers ... can i have examples of your suggestions please

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    citizen was alluding to something like this:
    Code:
    GroupManager::GroupManager()
    {
        std::string temp[] = { "a", "b", "c", "d", "f", "o", "z" };
        std::copy(temp, temp + sizeof(temp) / sizeof(temp[0]), groups);
    }
    Of course, you could also do it without a temporary array, but it would be more tedious:
    Code:
    GroupManager::GroupManager()
    {
        groups[0] = "a";
        groups[1] = "b";
        groups[2] = "c";
        groups[3] = "d";
        groups[4] = "f";
        groups[5] = "o";
        groups[6] = "z";
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Or, you could make it a static variable, in which case you can do exactly what you are doing now, but on a global scope. [which I think it correct anyways, as I doubt that the groups will actually change between different instances of the class - if they do, then you probably should have a more dynamic way to define what the groups are, for example reading the group codes from a file, and of course, you need a completely different way to initialize the groups array if you do that].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    Or, you could make it a static variable, in which case you can do exactly what you are doing now, but on a global scope. [which I think it correct anyways, as I doubt that the groups will actually change between different instances of the class - if they do, then you probably should have a more dynamic way to define what the groups are, for example reading the group codes from a file, and of course, you need a completely different way to initialize the groups array if you do that].

    --
    Mats
    The user should be able to add new groups, which will add to this array... It can only grow up to 26 letters [A-Z], well no car rental will ever have so many groups anyway ...

    Initializing it means there's are already existing groups on the shop (the ones specified) .. But say one opens a new rental shop ... Then this array doesn't need to be initialized.. Well not sure about making it global

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Is your application meant to handle multiple locations (offices) within one instance of the application? Or is it one instance of the application per office?

    If it's one instance per office, it can still be a static list.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    Is your application meant to handle multiple locations (offices) within one instance of the application? Or is it one instance of the application per office?

    If it's one instance per office, it can still be a static list.

    --
    Mats
    one per office

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    one per office
    Ok, so you can still use a static list that is initialized by reading from a file. The file can be "prebuilt" for standard locations, and then added to as the location acquires "out of the ordinary" vehicles. Or, you could have a default set, that is hard-coded, and then added to by reading from a file.

    Note that the only difference with a static member and non-static member is that the static member will be ONE for all instances of the class, and it's sort of like a "global variable", but only available inside the class.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    Ok, so you can still use a static list that is initialized by reading from a file. The file can be "prebuilt" for standard locations, and then added to as the location acquires "out of the ordinary" vehicles. Or, you could have a default set, that is hard-coded, and then added to by reading from a file.

    Note that the only difference with a static member and non-static member is that the static member will be ONE for all instances of the class, and it's sort of like a "global variable", but only available inside the class.

    --
    Mats
    as in
    Code:
    ....
    
    private:
    
    	static std::string groups[MAX_GRP];
    };
    
    GroupManager::GroupManager() :
    
    readDefaultGroupNames()
    
    {};
    
    void GroupManager::readDefaultGroupNames()
    {
    	ifstream datain;
    	datain.open (filename);
    
    	int cnt=0; 
            while( datain.fail() ) { 
    		datain >> groups[cnt++]; 
    	}
    
            datain.close();
    }
    Last edited by csonx_p; 09-17-2008 at 10:52 AM.

  12. #12
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by csonx_p View Post
    as in
    Code:
    ....
    
    private:
    
    	static std::string groups[MAX_GRP];
    };
    
    GroupManager::GroupManager() :
    
    readDefaultGroupNames()
    
    {};
    
    void GroupManager::readDefaultGroupNames()
    {
    	ifstream datain;
    	datain.open (filename);
    
    	int cnt=0; 
            while( datain.fail() ) { 
    		datain >> groups[cnt++]; 
    	}
    
            datain.close();
    }
    This gives an error ... readDefaultGroupNames() should't be a member, but when i remove iand make it a free or static function still complains

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    as in
    Code:
    ....
    
    private:
    
    	static std::string groups[MAX_GRP];
    };
    
    GroupManager::GroupManager() :
    
    readDefaultGroupNames()
    
    {};
    
    void GroupManager::readDefaultGroupNames()
    {
    	ifstream datain;
    	datain.open (filename);
    
    	int cnt=0; 
            while( datain.fail() ) { 
    		datain >> groups[cnt++]; 
    	}
    
            datain.close();
    }
    Is this a straight copy'n'paste? The red bits looks suspicious...

    Edit: And the blue is most likely not what you wanted, right?
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    Is this a straight copy'n'paste? The red bits looks suspicious...

    Edit: And the blue is most likely not what you wanted, right?
    --
    Mats
    The red bit isn't this how you do static initialization in a constructor? Blue bit, yeah forgot the '!'

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cannot access private member declared in class
    By newme in forum C++ Programming
    Replies: 7
    Last Post: 11-16-2008, 02:57 PM
  2. Returning pointer/ref to private member
    By rudyman in forum C++ Programming
    Replies: 13
    Last Post: 06-30-2008, 04:30 PM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21