Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Syntax help: pointer to an array as a class member 1

Status
Not open for further replies.

chpicker

Programmer
Apr 10, 2001
1,316
I want to create a class with a member that is an array whose size is determined at runtime. Does this work to create and destroy the array? I always have trouble keeping track of pointers to arrays, arrays of pointers, and the syntax involved in each.
Code:
class MyClass {
public:
  MyClass(int num);
  ~MyClass();

private:
  int* MyArray;
};

MyClass::MyClass(int num) {
  MyArray=new int[num];
}

MyClass::~MyClass() {
  delete[] MyArray;
}
 
There's really no reason to do it that way, unless you are forced to by class requirements. You should use a vector instead:
Code:
class MyClass {
public:
  MyClass(int num);

private:
  std::vector<int> MyArray;
};

MyClass::MyClass(int num) : MyArray(num) {
}
If you are taking a course and your instructor requires a dynamic array (or if you are just stubborn ;-)) then you need more code. You have to add a copy constructor and a copy assignment operator to your MyArray class, otherwise you will likely end up with a crash or other bad behavior if your class gets copied.
 
or if you don't want your class to be copiable, you can disable copying by making those functions private & undefined:
Code:
class Blah
{
 ...
private:
   Blah( const Blah& ); // Disabled.
   Blah& operator=( const Blah& ); // Disabled.
};
 
Thanks for the replies.

The main reason I don't use vectors (or any other STL constructs) is that I don't really understand them yet. I come from a QuickBASIC background; a lot of the old habits are hard to break, and the familiar is easy to use.
 
I would think that vectors are closer to what you know in QuickBASIC than C style or dynamic arrays, but I'm not that familiar with it myself.

In C++, I have yet to hear any real reason to use a dynamic array with new[] and delete[] in code like above. The vector class and other standard containers should be taught as the solution before dynamic arrays, but unforunately many old and/or not good teaching tools still ignore them.

Once you get over the initial learning curve (a few hours maybe) the vector is used much like an array, only it is less error prone and has more features that you can learn if you choose. I would seriously consider taking the time to learn them before continuing on any project where you are using dynamic arrays.
 
I'd say the two most basic tools that any C++ developer should know are std::string and std::vector. Once you start getting used to those classes, you'll start looking through the other STL classes to see what other things you can find to save you time and headaches.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top