I build a class for containing vectors with no default constructor. Specifically:
template<typename T>
struct MyVector
{
public:
int GetN(void)
{
return n;
}
MyVector(int n1)
{
n=n1;
ListElt = new T[n1];
}
~MyVector()
{
delete [] ListElt;
}
// some other irrelevant to the question code
private:
int n;
T *ListElt;
};
Now I want to build a class derived from it that contains one integer and a vector.
The code that works is the following:
struct EquivInfo {
public:
EquivInfo(int inpOrbit, MyVector<int> &inpMat)
{
iOrbit=inpOrbit;
eVect=new MyVector<int>(inpMat.getN());
// some code for copying the vector in place.
}
~EquivInfo()
{
delete eVect;
}
private:
int iOrbit;
MyVector<int> *eVect;
};
Is there a way to avoid the use of the pointer for the vector?
The problem is that if I remove the pointer then there is a call to a constructor of the kind MyVector(). I do not understand why. There should be a way to have a constructor for EquivInfo that calls a precise constructor for MyVector
I could add a constructor MyVector(), i.e. a default constructor that set the vector to something trivial. But precisely, I want to avoid such kind of constructors so that all vectors are well defined and the code is clean. The use of the pointer allows me to have a reasonable situation but I wonder if there is a clean way to avoid it.
Use member initializer list:
class EquivInfo {
public:
EquivInfo(int inpOrbit, MyVector<int> &inpMat)
: eVect(inpMat.getN())
, iOrbit(inpOrbit) {
// some code for copying the vector in place.
}
// ....
MyVector<int> eVect;
}
Related
I have a std:array something like this:
class MyClass {
private:
std::array<MyComplexType, 10> myArray;
}
In the constructor, I need to do the following:
MyClass::MyClass() : myArray({
MyComplexType(func(const_arg1, const_arg2, const_arg3).method(const_arg4)),
... repeated 8 more times...
MyComplexType(func(const_arg1, const_arg2, const_arg3).method(const_arg4))
})
Now, I want the 10 to be a compile-time constant that I can modify without having to copy&paste more (or less) of those initializers. They are all the same, I just need a "repeat n times". Everything up to and including C++17 is fair game.
I am 99.9% certain this should be doable with some template magic, but so far I came up with nothing that worked. Any ideas?
(And if it is impossible with templates, maybe with macros? Though I would hate having to go that direction...)
If you want to use a std::array you are going to need to build a helper function, namely a delegating constructor. Your default constructor will then call the delegate to actually initialize the member. That can look like
class MyClass {
public:
// default c'tor, create sequence of 10 integers
MyClass() : MyClass(std::make_index_sequence<10>{})
private:
// delegate, take a sequence and expand the initializer of myArray sizeof...(Is) times
template <std::size_t... Is>
MyClass(std::index_sequence<Is...>) :
myArray{ (Is, MyComplexType(func(const_arg1, const_arg2, const_arg3).method(const_arg4)))... } {}
std::array<MyComplexType, 10> myArray;
}
You can instead change myArray to be a vector instead and that would let you simplify the code to
class MyClass {
public:
MyClass() :
myData(MyComplexType(func(const_arg1, const_arg2, const_arg3).method(const_arg4)), 10) {}
private:
std::vector<MyComplexType> myData;
}
You could wrap the type into another struct/class which would provide an appropriate default constructor:
int f(int n)
{
return n;
}
struct S
{
S(int n) : n(n) { }
int n;
};
class MyClass
{
struct Wrapper
{
S s;
Wrapper() : s(f(7)) {}
};
std::array<Wrapper, 10> myArray;
};
You're now delegating the initialisation to the wrapper.
You might yet invest some work makeing the need of explicit indirection (myArray[i].s) obsolete, e.g. wrapping the array itself into another struct/class, too, providing the same interface as a std::array (as far as you need at least…) but with its iterators and index operators dereferencing to the complex type instead of to the wrapper (i.e. your iterator wraps around an array's iterator with S& operator*() { return i->s; } S* operator->() { return &i->s; } and further operators like increment just delegating to the wrapped iterator).
I need to create a simple template container, which can store any object of any type and could be used everywhere. So, I did something like this:
template <typename Type>
class Container {
public:
Container() : arraySize(10) { valueWrappers = new Type[arraySize];}
Container(const Container& other) { /* --- */}
~Container() { /* --- */}
Container& operator=(const Container& other) { /* --- */}
/* some functions */
private:
int arraySize;
Type* valueWrappers;
};
Now I have the problem - when I'm trying to create my container using as template a class without default constructor, the compilation error appears:
class MyClass {
public:
MyClass(int value) :v(value) { }
private:
int v;
};
int main() {
Container<MyClass> cont;
return 0;
}
C2512 'MyClass': no appropriate default constructor available
The problem is that I need to initialize the array of "Type" values with something, but I don't no what I need to use. I can't use NULL because, in this case, Container will work only with pointers. So, can somebody give an advice, how am I able to do it? Or, maybe, there is another way to solve this task?
Based on your requirements, I think you're going to have to use placement new. Since you haven't provided all the relevant code, I'm going to do what I can.
First, you're going to have to allocate raw memory instead of using new directly.
Container() : arraySize(10) { valueWrappers = reinterpret_cast<Type*>(::operator new(sizeof(Type) * arraySize)); }
Now when you put something in your Container, you'll have to construct it in place, using something like the following:
new (valueWrappers + index) Type(arguments to type);
In your destructor, you'll need to explicitly call the destructors on any object that you used placement new for.
valueWrappers[index]->~Type();
Lastly, release the memory using ::operator delete.
::operator delete(valueWrappers);
Please bear in mind that this is a very quick and dirty answer, and this code can be hard to debug and maintain. You're going to have to keep track of what indexes in valueWrapper have been initialized and which haven't during cleanup. If possible, I highly recommend using something akin to std::vector, which handles all this complexity for you.
One option is to not allocate the array in the default constructor, but initialise valueWrappers to null instead. Another option is to not have a default constructor in your template. Third option is to keep your class as-is and simply document that the template is default constructible only if the type argument is default constructible.
You can use std::optional to defer initialization, which is guaranteed to handle object lifetime correctly. Letting a default constructed container have 10 elements is also a questionable choice — a (count) constructor may be preferable.
template <typename Type>
class Container {
using elem_t = std::optional<Type>;
std::size_t count{};
std::unique_ptr<elem_t[]> elems{};
public:
Container() = default;
Container(std::size_t cnt)
: count{cnt}
, elems{std::make_unique<elem_t[]>(cnt)}
{
}
// for example
template <typename... Args>
void construct_at(std::size_t pos, Args&&... args)
{
assert(pos < count);
assert(!elems[pos]);
elems[pos].emplace(std::forward<Args>(args)...);
}
// ...
};
Note that I used std::unique_ptr to simplify memory management; a pointer will also be OK, though apparently more error-prone. Now you can traverse the container and construct the elements:
class MyClass {
public:
MyClass(int value) :v(value) { }
private:
int v;
};
int main()
{
Container<MyClass> cont(10);
for (std::size_t i = 0; i < 10; ++i) {
cont.construct_at(i, /* argument */);
}
}
I am having trouble with writing the Rule of three when it comes to vector of pointers to class objects. Searched and examples don't seem to apply. I have these three classes:
class Data
{
private:
map<string, double> m_DataVariables;
public:
Data();
Data(const Data &data);
};
class Sample
{
private:
Data *m_pData;
public:
virtual ~Sample()
{
delete m_pData;
}
Sample();
Sample(const Sample &sample);
};
class BuildTree
{
private:
vector<Sample*> BuildSamples;
public:
BuildTree(vector<Sample*> &Samples);
// This does not compile
BuildTree(const BuildTree& other) : BuildSamples(new(other.BuildSamples))
{
}
~TreeBuilding()
{
for (int i = 0; i < BuildSamples.size(); ++i)
delete BuildSamples[i];
}
void BuildTrees(void);
};
1- Not sure if I am correctly deleting BuildSamples.
2- In the constructor want to do a deep copy of the passed parameter into member variable BuildSamples.
BuildTree::BuildTree(vector<Sample*> &samples)
{
BuildSamples = samples; // This just copies the references
}
How do I write the copy constructor to make a deep copy? What am I missing here?
3- Please note: don't have access to smart pointers, share_ptr or unique_ptr, etc. C++98 is what I have.
Please show the steps required to do this. Really appreciate your time and consideration.
I noted you marked your question as c++98; even if C++98 doesn't support smart pointers in std::, you can certainly use the smart pointers defined in Boost, e.g. boost::shared_ptr.
For example, instead of using raw owning pointers like in vector<Sample*>, you can make your code simpler with
vector<boost::shared_ptr<Sample>>.
In this way, the copy and destruction operations will be automatically implemented under the hood.
You probably want something like:
BuildTree(const BuildTree& other)
{
BuildSamples.reserve(other.BuildSamples.size());
for (std::size_t i = 0; i != other.BuildSamples.size(); ++i) {
BuildSamples.push_back(new Sample(*other.BuildSamples[i]));
}
}
BuildTree(const vector<Sample*> &samples)
{
BuildSamples.reserve(samples.size());
for (std::size_t i = 0; i != samples.size(); ++i) {
BuildSamples.push_back(new Sample(*samples[i]));
}
}
You need to initialise the BuildSamples member as a vector of pointers, and ensure every pointer points at a clone of the objects passed. One way is
BuildTree(const BuildTree& other) : BuildSamples(other.BuildSamples)
{
std::vector<Sample *>::iterator i = BuildSamples.begin(), end = BuildSamples.end();
while (i != end)
{
*i = new Sample(**i);
++i;
}
}
The usage of BuildSamples(other.BuildSamples) initialises BuildSamples with the correct number of elements, but those elements are the same pointers as in other.BuildSamples. This ensures the vector is of the correct size, without worrying about setting the size explicitly. That is a shallow copy. The body of the constructor then sets every element of BuildSamples so it points at a clone of itself - thus completes the deep copy.
The constructor BuildTree(const std::vector<Sample *> &) can be implemented in a similar manner.
Note: given that your class is implementing a non-trivial copy constructor (to do the deep copy) and a destructor to cleanup, you also need to implement an assignment operator BuildTree &operator=(const BuildTree &). For an explanation why, look up the "rule of three".
While dealing with pointers and to avoid crashing of a running program due to destructor being called, Deep copy must be used! deep copy allows creating a new pointer pointing to a newly allocated space. But when passing objects as R-value in vector it is better to use the move constructor.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class example{
private:
int *pointer;
public:
//constructor
example(int d){
pointer = new int;
*pointer = d;
cout<<"Constructor Called"<<endl;
}
// deep copy
example(const example &source){
pointer = new int;
*pointer= *source.pointer;
cout<<"deep copy made"<<endl;
}
// Move Constructor
example(example &&source) noexcept :pointer{source.pointer}{
source.pointer = nullptr;
cout << "object moved"<<endl;
}
// Destructor
~example() {
delete pointer;
cout << "Destroyed"<<endl;
}
};
int main()
{
vector <example> vec;
vec.push_back(example{300});
vec.push_back(example{300});
vec.push_back(example{300});
vec.push_back(example{300});
return 0;
}
I want to recombine a (complex) member attribute of two agents and put it in a new agent. It's a vector of numbers, every second value is taken from agent1, the rest from agent2. The problem is, I want to be able to exchange the implementation of my numberList, maybe another numberListInt2 using integers or like in my example using floats:
#include <vector>
using namespace std;
class NumberList {
};
class NumberListInt : public NumberList {
vector<int> number_list {1,2,3};
};
class NumberListFloat : public NumberList {
vector<float> number_list {1.2f,2.5f,30.0f};
};
class Agent {
NumberList* numbers;
public:
Agent();
Agent(NumberList* numbers) {
numbers = numberList*
}
~Agent() {
delete numbers;
}
NumberList* recombine(Agent& other) {
NumberList* new_number_list;
if(true) // a boolean config value
new_number_list = new NumberListInt();
else
new_number_list = new NumberListFloat();
for(unsigned int i=0;i<3;i++) {
if(i%2)
new_number_list[i] = other.get_number_list()[i];
else
new_number_list[i] = numbers[i];
}
return new_number_list;
}
NumberList* get_number_list() {
return numbers;
}
};
int main ()
{
Agent agent;
Agent agent2;
Agent agent3(agent.recombine(agent2));
return 0;
}
My questions:
How to implement the operator [] of NumberList?
Is there a better way than using a pointer for the polymorphism?
Do I free the memory correctly?
Thanks in advice!
The problem is that operator[] shall return a reference to an item of the NumberList. But you don't know the type of the numbers, when you're in the parent class. So you won't be able to define this operator in a polymorphic manner (unless you define somehow a polymorphic item).
To benefit from polymorphism you have to use references or pointers. In your case the pointers are a good alternative. However you have to clarify their use in the constructors. I assume that the copy constructor should copy the object and not reuse the list of the original agent.
No, because you have not defined a virtual destructor for NumberList. And when you recombine() you return a newly allocated list. So the caller has to delete the returned object. That's very dangerous: if he forget, memory will leak. You'd better consider opting for shared_ptr to avoid leaking.
It's not clear if you need dynamic change of the NumberList type at runtime. If you don't, a safer approach would be to use templates
With templates it would look like:
template <class T>
class NumberList {
vector<T> number_list;
T& operator[] (size_t i) { return numberlist[i]; } // may be add boundary check ?
};
template <class T>
class Agent {
NumberList<T> numbers; // No more pointer, but directly the object
....
};
Like if I have this structure:
struct S
{
S(const S &arg) : (arg.bIsDouble ? v1{arg.v1} : v{arg.v}) {}
bool bIsDouble{false};
union {
vector<int> v;
double v1;
};
} ;
How can I make the copy constructor to either initialize 'v' or 'v1' based on some condition?
I would just outsource your work to Boost.Variant:
struct S {
S(const S&) = default;
boost::variant<double, std::vector<int> > v;
};
If you don't want to use Boost, you can read about how to write a discriminated union here, and then implement your own.
Since you only need two types, that's not too complicated, although it's still very error prone and involves a lot of code:
struct DoubleOrVector {
bool is_double;
static constexpr std::size_t alignment_value = std::max(alignof(double), alignof(std::vector));
alignas(alignment_value) char storage[std::max(sizeof(double), sizeof(std::vector))];
DoubleOrVector(double v)
: is_double(true)
{
new (storage) double(v);
}
// similar for vector
DoubleOrVector(const DoubleOrVector& dov)
: is_double(dov.is_double)
{
if (is_double) {
new (storage) double(dov.asDouble());
}
else {
new (storage) std::vector<int>(dov.asVector());
}
}
double& asDouble() {
assert(is_double);
return *reinterpret_cast<double*>(storage);
}
std::vector<int>& asVector() {
assert(!is_double);
return *reinterpret_cast<std::vector<int>*>(storage);
}
// plus other functions here
// remember to explicitly call ~vector() when we need to
};
And then we're still defaulting our copy ctor:
struct S {
S(const S&) = default;
DoubleOrVector v;
};
Constructor initialization list won't help here.
You have to use placement new in the class constructor, and then destroy (by manually calling the destructor) the correct member in the destructor. Also, since you define the destructor, you should define or delete the rest of The Big Five.
Minimal code:
struct S
{
bool bIsDouble{false};
union {
vector<int> v;
double v1;
};
S(bool d) : bIsDouble(d)
{
if(!bIsDouble)
new(&v) vector<int>();
// no need to create or destroy a double, since it is trivial
}
~S()
{
if(!bIsDouble)
v.~vector<int>();
}
// the other Big Five members are implicitly deleted
// so no copying, unless you write it yourself.
};
Note that switching between types is harder: if you had used the vector, and now want to use double, you need to destroy the vector first. I recommend to hide the data behind the accessor functions to enforce the invariant.
...or just use boost::variant. It's way simpler and less bug-prone.