Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 months ago.
Improve this question
I am using a library that provides functions related to randomness (https://github.com/effolkronium/random#is-equal) and I applied one of them that will randomly choose between the 2 objects.
Class object1;
Class object2;
std::unique_ptr<Class> p1 = std::make_unique<Class>(Random::get({object1, object2}));
std::unique_ptr<Class> p2 = std::make_unique<Class>(Random::get({object1, object2}));
Because I am using unique_ptr is it certain that these two pointers will always point to a different object?
no matter what object is chosen (1 or 2) make_unique will call the copy constructor p1 will not point to object1 or object2 it will create a new object of type 'Class' and will point to that.
-> p1 and p2 will always point to different objects (of type 'Class')
after your lines, you have 4 different objects
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 days ago.
Improve this question
I often use references to avoid unnecessary copying that comes with creating a new variable and assigning existing data to it.
Recently I was thinking about the example of a reference being assigned with an expression and whether there are really any advantages to using a reference for that. To give an example, let's say I have two vector object instances and I subtract one from the other. In the example below I am defining a new object instance to which the expression (subtraction) will be assigned to.
VectorClass vector1{// Instantiate with data};
VectorClass vector1{// Instantiate with data};
VectorClass vector3 = vector2 - vector1;
In my understanding, the expression will be evaluated, stored on the stack and copied to the new vector object (vector3).
What exactly does happen in the case of:
VectorClass vector1{// Instantiate with data};
VectorClass vector1{// Instantiate with data};
VectorClass& vector3 = vector2 - vector1;
My guess is that the expression will be evaluated, stored on the stack and the reference &vector3 references this memory, until they both go out of scope and get destroyed.
My question is: Am I correct, and if so, is the only advantage to the second example the fact that the copying when assigning value to vector3 doesn't happen?
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have a template class Array that has a buffer type vector.
template <typename T>
class Array
{
protected:
std::vector<T> buffer;
......
}
I want to implement the destructor ~Array(); for this buffer. Any ideas on how to do it?
The default destructor is already releasing the memory of vector, you don't need to define one here.
If you want to free memory of the vector explicitly, then try swap technique whenever you want:
std::vector<T>().swap(buffer);
Any ideas on how to do it?
It depends completely on what you've designed the class and its destructor to do.
The best option, when ever it does what is needed, is to not have a user declared destructor at all, but the implicitly generated one instead. In other words, follow the rule of 0 when possible.
I want to delete the allocated memory of the buffer
All member variables are destroyed automatically. The destructor of the vector deallocates its internal buffer. There is no need for a custom destructor here.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
given class (for example in name of X) , I want to allocate array like that:
X** array=new X*[20];
Let's look about the following function:
void Func(){
X** array=new X*[2];
X[0]=& X(5);
X[1]=& X(3);
}
Is it OK to do it like that or that I must to do it with new?
The thing is, when you do something like this :
X foo(5);
X[0] = &foo;
(wich i suppose is what you want to say)
X[0] will take the adress of foo, wich is a local variable who will only exist in the scope of your function. And if i do not tell bullshit, does not compile. (-fpermissive)
So after the last instruction of your function, the usage of this pointer will be Undefined
You can't be sure after any allocation that the content of your data is equal to nullptr (if not initialise yet). Initialisation syntax have way different behavior depending language, compiler, stars position etc (just kidding for the last one) so make sure to read the documentation about it.
However you can be sure to init something with null Value by using a zero-initialisation. Link here
If you don't want to dynamicly allocate your array of pointer make sure that those pointer still valid during the lifetime of your object
Another solution could be to dynamicly allocate them.
X[0] = new X(5);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Lets assume I have the following class:
class A
{
public:
A() {}
};
I need to declare a global instance of class A (required by spec.), what works better?
A a; // is the constructor even called here?
or
A* pa; // and allocate later a new instance
Thanks in advance
"A a; - is the constructor even called here?" - yes, the constructor is called.
"* pa; - and allocate later a new instance" - if you actually assign something to this pointer variable before using it, then it can work. But do you?
In general I'd say that the need for global variables indicate a flaw in the design. But if you really have to have them, then make sure they are constructed before you use them.
If A a is used, there would not be a need to release memory allocated to a. When the main() function exits, the destructor for A will be automatically called.
If A *pa is used, ideally pa should be explicitly deleted at some point in the code with delete pa and care should be taken to not use pa after it has been deleted.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I'm trying to wrap my head around what seems to be strange to me in TC++PL. It is about allocation of a class derived from an abstract class.
Since we don't know anything about the representation of an abstract type, we must allocate objects on the free store and access them through references and pointers.
Why free store? As you already know, a class derived from an abstract class can be allocated like any other local variables. I have no idea what exactly TC++PL tries to convey in this sentence.
In my opinion it is an incorrect statement. For example if you have an abstract base class B and derived (non-abstract) class D then you can write
D d;
B &b = d;
that is object of type D is created in stack provided that this code is in some function.
They mean that if you want to use some abstract class without knowing what concrete type that is, you must use dynamic allocation for it, i.e that you cannot write abstract_class A = concrete_class B;