initializing struct object declared in class private in c++
I am declaring my struct object inside my class private and i am initializing them using constructors but my style checker says that the member function of my struct type are not initialized. Can any one help me in this regards i will be very thankful to you.
Below is my code please suggest some solution to this problem
class Datastructure{
//forward decleration
struct Ship;
public:
//Constructor DS class
Datastructure();
//Destructor DS class
~Datastructure();
private:
struct Ship{
std::string s_class;
std::string name;
unsigned int length;
Ship();
Ship(const std::string& shipClass, const std::string& shipName,
unsigned int len);
};
Ship minShip;
Ship maxShip;
std::vector<Ship> shipVector;
};
#endif
it is giving me the following warnings
CIMP, line 17: Uninitialized member variables in class 'Datastructure'.
FSCH, line 17: No access specifiers at the beginning of class
'Datastructure'.
IVAP, line 62: Field 'minShip' in class 'Datastructure' is not initialized.
IVAP, line 63: Field 'max开发者_开发百科Ship' in class 'Datastructure' is not initialized.
IVAP, line 64: Field 'shipVector' in class 'Datastructure' is not
initialized.
By the C++ standard, minShip, maxShip, and shipVector have been initialized using their default constructors.
However, you indicated that you are using a style checker. Your style-guide may require explicit calls to the default constructor. One reason to do this is to make sure that you call the correct constructor and not rely on automatic behavior -- this is not what the standard require or even typical C++ programmers do, but if that's your style, and your style checker flags it, you might just need to comply.
There are all kinds of style rules in place in different organizations that aren't necessary for the compiler, but perhaps make it easier for developers to understand each other's code.
The member variables, minShip
and maxShip
, needs to initialized in the DataStructure constructor. For example,
DataStructure() : minShip(), maxShip(), shipVector() {}
Although it is not incorrect otherwise, it is better to provide implementations of the Ship
constructor(s) so that length
gets initialized to a known (as opposed to a random) value.
Ship() : length() {}
The above syntax is same as
Ship() : length( 0 ) {}
because
int i = int();
initializes i
to 0
.
精彩评论