White Bear
White Bear

Reputation: 107

Allocating memory for parameters given in constructors' class

Let's suppose, we've got a class in which we have to make constructor that will allocate memory for its parameters, i.e. according to their values.

I guess I've might messed up a little bit but at least I gave it a go.

#include <iostream> 

using namespace std; 

class Person
{ 
   private: 
      int height 
      int age; 

   public: 
      Person(int height, int age){}
}; 

Person::Person(int *height, int *age)
{ 
   int *height1 = new int; 
   int *age = new int; 
} 

Is it any good or utterly hopeless attempt ?

Upvotes: 0

Views: 61

Answers (1)

ecotax
ecotax

Reputation: 1953

As pointed out in the comments, the signatures don't match. Also, if you just want to store the passed values, there's no need to use new.

You could do

Person::Person(int h, int a)
{ 
   this->height = h; 
   this->age = a; 
} 

or, a bit shorter:

Person::Person(int h, int a) : height(h), age(a) {}

Upvotes: 2

Related Questions