Array accessibility, in class environment.
During my book progress, i have little less examples for array/vectors with constructor.
In case if I have Header, Cpp and file with main function, i suggested to initialize array with variables in main file.
Is there a way to initialize it in cpp file and access from cpp file as per below?
Please also advise what are the other options array/vectors accessibility with class objects. Big thanks in advance!
By putting your class' declaration in a header file (.h) and the implementation of that class in a separate source file (.cpp), the class can then be "used" in any source code file (.cpp) of your application, including your "main" source code file – simply by #include'ing the respective header file.
This, of course, means that anypublic methods and fields of the class can be accessed. Though you should be aware that, unless the field is declared static, then each instance of the class has its own separate field!
Note: A static field is "shared" by all instances of a class.
Also note that static fields should be used with extreme care. It's usually okay for things like constants.
So, maybe you want:
Date.h
1 2 3 4 5 6 7
class Date
{
public:
staticconst std::array<std::string, 12> months;
/* ... */
};
// date.h
#include <string>
#include <array>
class Date
{
public:
// static: shared by all objects of type Date
// const: we don't want these to be modified
staticconst std::array < std::string, 12 > month_names;
explicit Date( int month );
// ...
private:
int month;
// ...
};
date.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13
// date.cpp
#include "date.h"
const std::array < std::string, 12 > Date::month_names
{ {"January", "February", "March", "April", "May", "June", "July", "August", "September", "November", "December"} } ;
Date::Date( int month ) : month(month)
{
// validate that month is not out of range
// throw or correct on error
}
// ...
Works! Thanks, guys!!!
I will use this as template, but I would love to understand this more deep.
Static and Const together make no sense for me, I will try to figure this out, but now I'm trying to find alternatives:
Is there any another (non-magic library functions or complicated things as inline) option to make my
described case work?
const and static are kind of orthogonal! You can have them solitary as well as combined.
const means that a variable or field does not change. It's initialized once and then its value is fixed.
In fact, the value of a const variable/field could already be predetermined at compile-time.
When it comes to member variables, then static means that the member variable is shared between all the instances (objects) of that class - rather than giving each instance its own separate member variable.
Note: A static variable still is perfectly modifiable – unless declared const.
static has a somewhat different meaning when applied to "global" variables declared outside of a class.
Is there any another (non-magic library functions or complicated things as inline) option to make my described case work?
Not quite sure what you mean?!
All that was discussed in this thread were very fundamental C++ features. No "magic" involved.
int main()
{
Date instance; // <-- requires an instance (object)
std::cout << instance[7] << std::endl;
}
Using a static "getter" method works just as well and should actually be preferred here:
(IMO, there is no reason why you should need an instance to access the static data)
1 2 3 4 5 6 7
class Date
{
public:
staticconst std::string& get_month(const size_t n);
private:
staticconst std::array <std::string, 12> months;
};
^^^ something like that.
the idea is that your object would support array notation.
mything[index] = something; //generic example of the idea
which is the same as
mything.somecontainer[index] = something //exact same as above assuming public member or
mything.setcontainer(index,something) //private member with setter
--- its just another way to grant access or do a 'getter/setter' concept with less typing and clutter.
honestly the number of times this is useful is smaller than you would think. But when your object is wrapping a container to add just a little bit of something special around it... note that generally its a violation of OOP pie-in-the-sky concepts, namely data hiding and protection. Those concepts exist for a reason, but there are times you don't need all that.