I have a class CM which has a constructor as follow:
CM::CM(const std::vector<Something>& vector1, const SomethingElse& something):myVector(vector1),mySomething(something)
{
}
and from another class (class Ad) I want to create an object
Ad.h
CM* cmObj;
Ad.cpp
Ad::Ad()
{
cmObj = (CM*) malloc(1);
}
and I also want to instantiate my cmObj to NULL. How can I do that?
Pre C++11, use cmdObj = NULL
to set the pointer to a null pointer value.
Don't use malloc
and cast, as the constructor to CM
will not be called and things like virtual function tables will not be set up correctly.
Use cmObj = new CM(/*ToDo - constructor arguments here*/);
instead. Don't forget to call delete cmObj;
somewhere (the destructor for Ad
is a good place).
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.