Is it a good practice to instantiate an object with auto? [closed] - c++

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
solution 1 :
auto a = A()
Solution 2 :
A a;
I was wondering which one of the two solutions is the best manner to instantiate an object ?
I know that the solution 1 calls the default constructor and then the copy constructor, but I really don't know if there is any advantages of writing that.
Edit: I wrote a small class to test by my own and it appears that in accordance with the comments, these two "solutions" have exactly the same behavior.
class A
{
public:
A(){
std::cout <<"default_constructor\n";
}
A(const A &g){
std::cout <<"copy_constructor\n";
}
};
solution 1 :
default_constructor
solution 2 :
default_constructor

In my opinion you should always use solution 2 for this case, as you have to define the type somewhere. I prefer using auto just in cases where the type is already set and I can avoid duplicate the type e.g. getting an element from an container
std::vector<double> myVector{0.0, 1.0};
auto firstElement = myVector.front();
or
auto myInt = static_cast<int>(2.0);

Related

How can I ensure range1 is smaller than range2? [closed]

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 1 year ago.
Improve this question
I am trying to ensure that int range1 must be smaller than int range2 before using this structure. I am new to programming.
struct prime
{
int range1;
int range2;
};
You can write a constructor. Constructors are the place where you should make sure the object is constructed in a valid state:
struct prime
{
prime(int a,int b) : range1(std::min(a,b)), range2(std::max(a,b)) {}
private:
int range1;
int range2;
};
I assumed you just want to make sure that range1 < range2 but assume nothing about the passed parameters. If you want to make sure that a < b you could throw an exception when it is not the case.

Is there a reason why std::priority_queue as MinHeap requires greater than comparer; MaxHeap, less than comparer? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
When I use std::priority_queue as a Min Heap for example, I'd need to do something like this:
struct MyType{
...
int data;
};
auto greaterThanCmp = [](const MyType& a, const MyType& b){ return a > b; };
std::priority_queue<MyType, std::vector<MyType>, greaterThanCmp> minHeap;
My question is: is there an advantage to providing the greaterThanCmp for a Min Heap or would a less than comparer works just as well, had the committee/implementer gone that route.
Or in other words, why did the standard choose one over the other?
I have tried implementing my MinHeap class with a lessThanCmp just for the heck of it. I tested my code, it works fine.
Thank you very much for your response in advance!
Once you have a greater object, you can easily make a less object out of it:
less ::operator() (a, b) { return greater (b, a) }. So the less requirement is not limiting.

I have used a vector to create a list of class objects. How can I call the constructor with parameters when I initialize the vector? [closed]

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
I have a class Random. The constructor has a parameter "int range" if I say
class Random
{
private:
int r;
public:
Random(int range)
{
r = range
}
}
I want to create a vector of objects of class Random. How do i pass the range parameter in that case
vector<Random> v;
Where does the parameter go? Bit confused.
There's no great way to do what you want.
Since Random is cheap to copy, you can do this:
vector<Random> v{Random(1), Random(2), Random(3)};
but if it wasn't cheap to copy (or not copyable at all), then you would do this instead:
vector<Random> v;
v.reserve(3);
v.emplace_back(1);
v.emplace_back(2);
v.emplace_back(3);

Copy structure to a different structure [closed]

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
I was wondering if there was an elegant way to copy a structure to a different structure, where the second structure is essentially the same as the original one, except without the last field(s).
For example,
struct A {
int a;
int b;
int c;
};
struct B {
int a;
};
struct A v1;
struct B v2;
Would,
memcpy(&v2, &v1, sizeof(v2));
Achieve the functionality I wish?
Where v2 has the "a" value that was originally found in v1?
Thank you
If instead of copying all bytes in A, you only copy the number of bytes that B expects, you will achieve your desired result:
memcpy(&v2, &v1, sizeof(v2)); // remember that the first argument is the destination
However, this is not good coding style. With this minimal code example, it is hard to tell, but you would probably want A to inherit from B so that you can convert the two without having to physically copy memory.
Otherwise, this would be easier and cleaner:
b2.a = v1.a;

Using a template to create arrays of varying length inside of class in C++11 [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Say I have a class:
template <int S1>
class Cont {
private:
std::array<int, S1> nums;
public:
Cont(std::array<int, S1> ns);
}
When trying to link my program, I am getting an error saying that, for example, Cont(std::array ns) is undefined. So am I doing something wrong? Or will this never work due to how arrays work?
EDIT: Sorry, I should have mentioned that the constructor is defined. I was only giving the header to illustrate what I meant.
Cont(std::array<int, S1> ns)
Is only a declaration, you need to define it as well.
like:
Cont(std::array<int, S1> ns){
// code
}