How to pass value to the object constructor's declare using dynamic memory allocation
-
26-06-2021 - |
Вопрос
The code is as follow :
The Code :
#include <iostream>
using namespace std;
class pub
{
string name;
public:
pub(string name):name(name){} //Constructor
void getName(string name){this->name = name;}
string returnName(void){return name;}
};
int main(void)
{
pub * p = new pub[5]; //Error-prone statement.
//Ignore for not having "delete" statement
return 0;
}
The Question :
1.) In this case , is there any method for me to pass value to each dynamic memory I've allocated , or is it I have to set a default value to the constructor's argument in order to circumvent this problem ?
Thank you !
Решение
If your compiler supports C++11 you could use std::vector
and with an initializer list:
std::vector<pub> v { pub("1"), pub("2") };
See online demo https://ideone.com/Qp4uo .
Or std::array
:
std::array<pub, 2> v = { { pub("1"), pub("2") } };
See online demo https://ideone.com/lBXUS .
Either of these also removes the burden of delete[]
ing the dynamically allocated array.
Другие советы
Apart from the somewhat unconvential naming convention that you used (I changed getName()
to setName()
and returnName()
to getName()
, and used trailing _
to denote private data members), using a `std::vector will do the memory management automatically for you:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class pub
{
public:
pub(): name_("default") {}
pub(string const& name): name_(name){} //Constructor
pub(const char* name): name_(name) {}
void setName(string const& name){ name_ = name;}
string getName(void) const {return name_;}
private:
string name_;
};
int main(void)
{
// initialize with 2 elements, then add 3 more default elements
std::vector<pub> pub_vec { "bla", "bar" };
pub_vec.resize(5);
std::for_each(pub_vec.begin(), pub_vec.end(), [](pub const& elem){
std::cout << elem.getName() << "\n";
});
return 0;
} // 5 destructors automatically called
Note: adding an overloaded constructor that takes const char*
allows you to use string literals to initialize your data.
Output on Ideone
Use std::vector<pub>
. It doesn't require the default constructor.
E.g.
std::vector<pub> vec(5, pub("xyz"));
creates a vector with 5 equal elements.