Lane.h
class Lane{
//other declarations..
public:
Lane(){}
static Lane left_line;
static Lane right_line;
};
Lane.cpp
Lane Lane::left_line;
main.cpp
int main(){
Lane::left_line(); //doesn't work
What am I doing wrong or am I doing everything wrong. I am actually confused about how the static objects work exactly.
static
members get declared inside a class and initialized once outside the class. There is no need to call the constructor once again. I've added a method to your Lane
class to make it clearer.
class Lane{
//other declarations..
public:
Lane(){}
static Lane left_line; //<declaration
static Lane right_line;
void use() {};
};
Lane.cpp
Lane Lane::left_line; //< initialisation, calls the default constructor of Lane
main.cpp
int main() {
// Lane::left_line(); //< would try to call a static function called left_line which does not exist
Lane::left_line.use(); //< just use it, it was already initialised
}
You can make the initialisation even more obvious by doing this:
Lane Lane::left_line = Lane();
in Lane.cpp.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.