I am trying to create a class which has a private member that is an array. I do not know the size of the array and will not until the value is passed into the constructor. What is the best way to go about defining the class constructor as well as the definition in the .h file to allow for this variable size of the array?
If you want a "real" C-style array, you have to add a pointer private member to your class, and allocate dynamically the memory for it in the constructor (with new). Obviously you must not forget to free it in the destructor.
class YourClass
{
private:
int * array;
size_t size;
// Private copy constructor operator to block copying of the object, see later
// C++03:
YourClass(const YourClass &); // no definition
// C++11:
YourClass(const YourClass&) = delete;
public:
YourClass(size_t Size) : array(new int[Size]), size(Size)
{
// do extra init stuff here
};
~YourClass()
{
delete [] array;
}
};
To make this work easier, you may consider to use a smart pointer (for example, a boost::scoped_array in C++03, or plain std::unique_ptr in C++11), that you may initialize using the initializer list before the constructor or simply in the constructor.
class YourClass
{
private:
boost::scoped_array<int> array; // or in C++11 std::unique_ptr<int[]> array;
size_t size;
public:
YourClass(size_t Size) : array(new int[Size]), size(Size)
{
// do extra init stuff here
}
// No need for a destructor, the scoped_array does the magic
};
Both these solutions produce noncopyable objects (you didn't specify if they had to be copyable and their copy semantic); if the class don't have to be copied (which happens most of times), these both are ok, and the compiler will generate an error if you try to copy/assign one class to another, in the first case because the default copy constructor has been overloaded with a private one (or plain deleted in C++11), in the second case because boost::scoped_array and std::unique_ptr are noncopyable.
If, instead, you want to have copyable objects, then you must decide if you want to create a copy that shares the array (so, just a pointer copy) or if you want to create a new, separate array for the other object.
In the first case, you must be very careful before freeing the allocated memory, since other objects may be using it; a reference-counter is the most common solution. You can be helped in this by boost::shared_array (or std::shared_ptr in C++11), which does all the tracking work automatically for you.
If instead you want to do a "deep copy", you'll have to allocate the new memory and copy all the objects of the source array to the target array. This is not completely trivial to do correctly, and is usually accomplished through the "copy and swap idiom".
Still, the simplest solution is to use a std::vector as a private member: it would handle all the allocation/deallocation stuff by itself, constructing/destroying itself correctly when the object of your class is constructed/destructed. Moreover, it implements the deep-copy semantic out of the box. If you need to make your callers access the vector read-only, then, you could write a getter that returns a const_iterator or a const reference to the vector object.
Using a std::vector is the best option.
If you ever need to pass it to a function that expects a pointer to an array (like the GSL often does), you can still pass &vec[0]...
Related
I've an operator and function which goal is to copy the values of the vector pointer to a vector. I've read that as long it's a vector pointer, deep copying methods would only copy its pointer, not its value. I'm wondering how to copy it's values over instead. (The vector is a member of CObject class)
Operator function:
void CObject::operator=(CObject& rhs)
{
this->ClearObject(); //Object will be cleared first
// How to perform the deep copy below?
copy(rhs.m_Vector.begin(), rhs.m_Vector.end(), back_inserter(this->m_Vector));
}
Declared as -> void operator=(CObject& rhs);
Example application of operator function (Performed in another class):
CObject* m_pObjectOne;
CObject m_ObjectTwo;
m_ObjectTwo = m_pObjectOne;
(Therefore, when m_pObjectOne is deleted in its class destructor, m_ObjectTwo loses its value)
Vector & others:
struct OBJECT_ITEM
{
char m_chType;
UINT m_nDataByte;
BYTE* m_pData;
CString m_strRecipeTag;
}
std::vector<OBJECT_ITEM> m_Vector;
I've also tried other deep copy methods like push_back() & assign() but it gives me the same result.
Any help would be appreciated!
Strive for the "rule of zero".
How to Deep Copy Values of Vector Pointer (Not just the pointer)?
Basically - don't. You should strive to follow the rule of zero: Unless otherwise necessary, set things up so that the default constructors, assignment operators and destructors do what they should.
In your case: Suppose you let CObject use its default (copy) assignment operator. That means, that rhs.m_Vector will be assigned to lhs.m_Vector. And vector assignment means that individual members of the vector are assigned to their corresponding members.
The way you wrote your vector, that won't do what you want: You wrote you want to avoid the case of "when m_pObjectOne is deleted in its class destructor, m_ObjectTwo loses its value)". Well, since you're willing to hold copies of the objects, consider something like the following:
struct OBJECT_ITEM
{
char m_chType;
UINT m_nDataByte;
MyContainer m_upData;
CString m_strRecipeTag;
}
And choose or write MyContainer to suit your needs. It could just be an std::vector<BYTE>; or if you don't want the size to change after construction, use a dynarray (not in the standard library these days; here's an alternative); etc. And your chosen container will actually get properly copied when you copy an OBJECT_ITEM. Finally, don't hold pointers to OBJECT_ITEM's - just hold actual OBJECT_ITEM's (which it seems you were already doing?)
And there, now you can no longer have dangling pointers - with no custom constructors, assignment operators or anything else like that.
Edit: I originally posed this question out of context so I've reworked it. I've left as much as possible unchanged so most of your responses will still apply.
I'm having trouble understanding how to implement a constructor which accepts a pointer to an array of pointers.
I have the following class which contains a member, bodies, of type Body** (i.e. it is a pointer to an array of pointers to body objects).
class Galaxy
{
private:
int n; // Number of bodies in galaxy.
Body** bodies; // Ptr to arr of ptrs to Body objects.
public:
Galaxy();
Galaxy(int, Body**);
// Some other member functions.
};
Here is the implementation of the constructors:
// Default constructor. Initializes bodies to null pointer.
Galaxy::Galaxy() : bodies(NULL) {}
// Alternate constructor. Here I try to perform a deep copy of bodiesIn.
Galaxy::Galaxy(int nIn, Body** bodiesIn)
{
n = nIn;
// Allocate memory for an array of n pointers to Body objects.
bodies = new Body*[n];
// Perform deep copy.
for (int i=0; i<n; i++)
{
bodies[i] = new Body;
*bodies[i] = *bodiesIn[i];
}
}
Is this method sound, or is there a preferred way to construct such an object.
P.S. I realize it would be easier to code this with std::vector's, however the size of the array doesn't change, and minimizing memory usage is important.
There's lots wrong with your function:
Creating an object and then immediately assigning to it is inefficient, use the copy ctor instead.
If an exception is thrown by any new-expression but the first one or by one of the assignments, you are leaking objects.
Better take a std::size_t for the size, it's designed for it.
Better swap the arguments, that's more idiomatic.
You don't return the copy at the moment
Why not templatize it?
BTW: std::unique_ptr does not add any overhead, but provides plenty of comfort and safety.
Assume I have a following class, basically an ordinary array wrapper:
template<class T>
SimpleArray {
T * array;
SimpleArray(): T(NULL) {}
SimpleArray(T * array_) {
// what goes here?
}
// TODO: copy constructor and copy assignment
~SimpleArray() {
delete[] array;
}
};
How do I copy elements from one array to another? If it was a simple type, I could just use something like memcpy, and it would work just fine. But what if T is a complex type that requires deep copying? If, say, T has a member array, than shallow-copying it would create several T objects pointing to the same resource, which is a disaster.
Basically, I need some way to create a deep copy of an array. Any suggestions?
I know I could use something from the standard library, but there's a reason I don't. SimpleArray is designed to give an easy access to the internal storage of my objects in form of T*. As far as I am concerned, no STL container supports such functionality.
It's inadvisable to roll your own dynamic arrays. If all you need is access to the underlying data via a T*, then you can use vec.data() (c++11 only), or &vec.front().
Inside a method can one create an uninitialised object from the class?
Here's some context: imagine a class where the constructors all allocate memory:
class NumberArray
{
size_t m_Size;
int *m_Numbers;
public:
NumberArray() { m_Size = 1; m_Numbers = new int[1]; m_Numbers[0] = 0; }
// . . . other methods for manipulating or constructing . . .
~NumberArray() { delete[] m_Numbers; }
// What if I had a method that concatenates two arrays?
NumberArray ConcatenateWith(const NumberArray &) const;
};
Inside such a method one would desire to create an uninitialised object of class NumberArray, and then 'construct' a new object based on this and the object in the parameter? AKA:
NumberArray NumberArray::ConcatenateWith(const NumberArray &other) const
{
// Mystery manner of creating an uninitialised NumberArray 'returnObject'.
returnObject.m_Size = m_Size + other.m_Size;
returnObject.m_Numbers = new int[returnObject.m_Size];
std::copy(m_Numbers, m_Numbers + m_Size, returnObject.m_Numbers);
std::copy(other.m_Numbers, other.m_Numbers + other.m_Size, returnObject.m_Numbers + m_Size);
return returnObject;
}
What's the best way of doing this? Basically, I don't want the default constructor to create a size 1 array that I will just delete and then allocate a new array for again anyway.
It's not entirely clear what you are trying to do, but if all you want is to create a new instance of the class and not have a constructor other than the default constructor called then do just that.
All you have to do is create a private constructor, that has a different signature from the default constructor and which does not allocate memory (or differs in whatever way you need it to differ from the default constructor); then simply have your class invoke that constructor internally, when necessary.
What you're asking for is placement new. This looks something like this:
#include <cstdlib>
#include <new>
void* mem = std::malloc(sizeof(T)); // memory for a T (properly aligned per malloc)
T* x = new (mem) T; // construct a T in that memory location
x->~T(); // destruct that T
std::free(mem); // and free the memory
Doing this correctly (in an exception-safe manner with properly managed and aligned memory) is not a trivial task. You need to be careful about the lifetime of your objects.
For your question, you are describing exactly what std::vector does. It allocates raw uninitialized memory and constructs inserted elements directly into that memory. And lots of its code is dedicated to just getting the lifetime and memory management correct and exception safe!
You should strongly prefer to use std::vector instead of writing it yourself.
There is no well-defined way, as far as I'm aware, to create an object without invoking it's constructor. This is regardless of whether you have access to its public interface or not, though you could implement a private or protected constructor if you want to restrict who can invoke it. There is otehrwise no restrictions on creating new instances of a class from its own internal methods, in fact it is quite common to define a private constructor and a static public method that create instances of said object if you want to restrict under which conditions said object can be created.
If you want to, you can allocated sufficient memory for an object and reinterpret_cast a pointer to that memory to a pointer of the type you want. This usually works for POD's, but since many implementations (if not all) of polymorphic inheritance in c++ adds a pointer to a vtable to polymorphic instances this approach will usually, if not always, fail for those.
In short, create a private constructor and have a static method invoke it and then do any other work that you need is my recommendation.
I think this may be similar to what you want, an 'anonymous' class of sorts:
struct test {
virtual void doSomething() {
puts("test");
}
};
struct a {
test *t() {
struct b : test {
void doSomething() {
puts("b");
};
};
return new b;
};
};
int main()
{
a a;
a.t()->doSomething(); // outputs 'b'
}
However, due to slicing and how new works on C++, you must return a pointer and the 'anonymous' type must have a name, even if it's restricted only to the function.
If you could edit the OP and clarify exactly what you wish to accomplish by this, maybe we could help you more.
while writing a offset array class(your idxs go from lets say 100 to 1000, so you create class that takes that into account without wasting first 100 slots in the array) I ran into a problem.
How to initialize a class that has an C array of elements(problem is that T doesnt have def constructor). Basically I want the array to be totally uninitiated. Example:
class MyClass
{
MyClass(int i)
{
}
};
template <typename T, size_t n, size_t offset>
struct offsetedIdxArray
{
T data[n];// error line : error C2512: 'MyClass' : no appropriate default constructor available
offsetedIdxArray()
{
}
T& operator [](size_t pos)
{
return data[(pos-offset)];
}
};
usage:
offsetedIdxArray<MyClass, 1024,offset> oia;
Making def constructor is not the option because class I use is in fact library class.
*EDIT: * not related to problem described here, but it turned out that my precious library class doesnt have copy ctor, just move ctor, so I had to use vector of unique_ptr.
To get a statically-sized uninitialized portion of storage, you can use an "untyped" buffer of aligned storage, like std::aligned_storage<sizeof(T[n]), alignof(T)>::type in C++11 (in C++03 you need to use a char[sizeof(T[n])+something] and do manual corrections for alignment, or use a char[sizeof(T[n])] and compiler extensions to specify alignment).
That means using placement new for constructors, and explicit destructor calls for destruction. It is up to you to track what parts of that storage have objects (and thus needs destruction) and what parts don't have objects (and can't have destructors called on). It is also up to you to cater for when the client requests an element that isn't initialized at all (there's no object at the place it's supposed to be).
An alternative is to use an array of boost::optionals, and then you don't have to care about destruction and can simply assign new elements to their respective index.