Dynamically allocating private member variables?

I need to create a class in which all member variables are dynamically allocated. How do I go about dynamically allocating member variables string name, double price, and double rating?

"I need to... dynamically allocate... name, price, rating."

I doubt it.

If you can show me a truly valid reason to add that kind of fragile complexity to your code...
try use smart pointer .
since they can auto delete the allocated memeory to avoid memory leakage.
You declare dynamic member variables like any othe dynamic variables.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class SomeClass
{
public:
  SomeClass ()
  {
    name = new std::string;
    price = new double(0);
    rating = new double(0);
  }
  ~SomeClass ()
  {
    delete name;
    delete price;
    delete rating;
  }
private:
  std::string *name;
  double *price, *rating;
};


Is this a kind of exercise or assignment? Normally there is no need for them in this case.
Topic archived. No new replies allowed.