C++ Avoiding using copy constructor which is called by priorityqueue - c++

I have this current setup:
#include <iostream>
#include <queue>
#include <vector>
class A
{
int val;
public:
A()
{
std::cout << "Inside A. Constructor with val =" << val << ".\n";
}
A(const A& msg)
{
std::cout << "Inside const A. Never want to come here. val =" << msg.val << ".\n";
}
bool operator()(const A* m1, const A* m2)
{
std::cout << "Inside () function.\n";
std::cout << "m1: " << m1->GetVal() << " m2: " << m2->GetVal()<<"\n";
return (m1->GetVal() < m2->GetVal());
}
void setVal(int input) { std::cout << "inside setVal.\n"; val = input; }
int GetVal()const { return val; }
};
void specialPriorityQueue()
{
//init
A* val = new A();
val->setVal(5);
std::priority_queue<A*, std::vector<A*>, A> pq;
pq.push(val);
A* val2 = new A();
val2->setVal(3);
pq.push(val2);
delete val;
delete val2;
}
int main()
{
specialPriorityQueue();
return 0;
}
outputs:
Inside A. Constructor with val =-85000000...
inside setVal.
Inside A. Constructor with val =-85000000...
Inside const A. Never want to come here. val =-85000000....
Inside A. Constructor with val =-85000000...
inside setVal.
Inside const A. Never want to come here. val =-85000000....
Inside () function.
m1: 5 m2: 3
My question is: Is there any way to avoid using the copy constructor by the priority queue. This causes undefined behaviour. I can't remove the function either as it has too much dependencies elsewhere.

That copy constructor is called when it creates the comparator object, which is also your element type, A.
Use another type for comparator, e.g.:
struct Compare
{
bool operator()(const A* m1, const A* m2) const {
return m1->GetVal() < m2->GetVal();
}
};
And then:
std::priority_queue<A*, std::vector<A*>, Compare> pq;
And, even better, use smart pointers to avoid memory leaks:
struct Compare
{
bool operator()(std::unique_ptr<A> const& m1, std::unique_ptr<A> const& m2) const {
return m1->GetVal() < m2->GetVal();
}
};
void specialPriorityQueue() {
std::unique_ptr<A> val(new A());
val->setVal(5);
std::priority_queue<std::unique_ptr<A>, std::vector<std::unique_ptr<A>>, Compare> pq;
pq.push(move(val));
std::unique_ptr<A> val2(new A());
val2->setVal(3);
pq.push(move(val2));
}

Related

C++11 passing variadic number of objects by reference to function

I'm trying to pass a variable number of objects by reference in C++11.
The C-style of variadic argumnets did not seem very elegant, so I tried using the std::initialiszer_list.
But all the examples i found used pass by copy, I wrote a simple example of what I want to achieve:
//Example class for illustration purposes
class MyClass
{
public:
MyClass()
: m_id(0), m_idChecks(0)
{}
~MyClass(){}
int getId()
{
m_idChecks++;
return m_id;
}
int getIdChecks() const
{
return m_idChecks;
}
private:
int m_id;
int m_idChecks;
};
// I want to do something like this (pass the objects by reference):
// void test_initializerList(std::initializer_list<MyClass&> listClasses)
// But only pass by copy works:
void test_initializerList(std::initializer_list<MyClass> listClasses)
{
// Doing operations on copies of the object:
int sum = 0;
for(auto c : listClasses)
{
sum += c.getId();
}
std::cout << "\n id: " << sum;
}
void test()
{
MyClass c1;
MyClass c2;
MyClass c3;
test_initializerList({c1,c2,c3});
std::cout << "\n Wanted: " << 1 << " id checks, got: " << c1.getIdChecks() << "\n";
}
So how do you pass a variable number of arguments in modern C++? Examples are appreciated!
Unfortunately, initializer_list can only copy it's arguments, and can't hold a reference to it (directly at least). However, there is way around it (example trimmed):
#include <initializer_list>
#include <iostream>
#include <functional>
//Example class for illustration purposes
class MyClass
{
public:
MyClass() : m_idChecks(0) {}
~MyClass(){}
int getId() const
{
return m_idChecks++;
}
int getIdChecks() const
{
return m_idChecks;
}
private:
mutable int m_idChecks;
};
using r = std::reference_wrapper<MyClass>;
void test_initializerList(std::initializer_list<r> listClasses)
{
// Doing operations on copies of the object:
int sum = 0;
for(const auto& c : listClasses)
{
sum += c.get().getId();
}
}
int main()
{
MyClass c1;
MyClass c2;
MyClass c3;
test_initializerList({c1,c2,c3});
std::cout << "\n Wanted: " << 1 << " id checks, got: " << c1.getIdChecks() << "\n";
}

Is it possible to pass a struct's member variable as parameter

I am trying to associate a struct's member variable with a class. So that when I create a new class, I can specify that it is associated with this member variable in a struct. For example:
struct A {
int a;
int b;
};
static A a[2];
a[0].a = 1;
a[0].b = 2;
a[1].a = 3;
a[1].b = 4;
class foo {
public:
foo(int index, ???) {
c = a[index].???; //Is it possible to define the 2nd parameter as a getter of struct A's member? So this line could resolve to either a[index].a or a[index].b?
}
private:
int c;
};
So that:
new foo(0, ???) would set c to 1 given ??? refer to A::a
new foo(0, ???) would set c to 2 given ??? refer to A::b
new foo(1, ???) would set c to 3 given ??? refer to A::a
new foo(1, ???) would set c to 4 given ??? refer to A::b
Yes, it is possible, you need to pass a data member pointer:
#include <iostream>
struct A
{
int a;
int b;
};
static A a[2]
{
1, 2
, 3, 4
};
class foo
{
public: int c;
public:
foo(int const index, int A::* const p_field)
{
c = a[index].*p_field;
}
};
int main()
{
foo const f1(0, &A::a);
::std::cout << f1.c << ::std::endl;
foo const f2(0, &A::b);
::std::cout << f2.c << ::std::endl;
foo const f3(1, &A::a);
::std::cout << f3.c << ::std::endl;
foo const f4(1, &A::b);
::std::cout << f4.c << ::std::endl;
return 0;
}
Check this code at online compiler
You have a couple options. If you just want the integer (like you have in your code you've posted), then just take an integer as a parameter to the constructor and pass it the right number.
class foo {
public:
foo(int val) {
c = val
}
private:
int c;
};
int main() {
foo f(a[0].b);
}
Or you could take a reference to an integer. This way if one changes, the other will as well:
class foo {
public:
foo(int &val) : c(val) { } //need to use an initialization list for this one
private:
int &c;
};
int main() {
foo f(a[0].b);
a[0].b = -1; //f.c will be -1 now as well
}
Using a data member pointer as in VTT's answer is the most direct solution but I often find member pointers and member function pointer syntax a bit cumbersome and I believe it is hard for the compiler to optimize.
For these kind of things I prefer to use a stateless lambda. You can pass a lambda to a function template and then the compiler can easily optimize it away:
#include <iostream>
struct A {
int a;
int b;
};
static A a[2]{{1, 2}, {3, 4}};
class foo {
public:
int c;
public:
template<typename F>
foo(int index, F getter) { c = getter(a[index]); }
};
int main() {
auto agetter = [](const A& a){ return a.a; };
auto bgetter = [](const A& a){ return a.b; };
foo const f1(0, agetter);
std::cout << f1.c << "\n";
foo const f2(0, bgetter);
std::cout << f2.c << "\n";
foo const f3(1, agetter);
std::cout << f3.c << "\n";
foo const f4(1, bgetter);
std::cout << f4.c << "\n";
}

vector push back clarifying

I can't figure out how push_back(const value_type& val) exactly works, in docs it says about val that
val is Value to be copied (or moved) to the new element ...
How it can be copied when it takes val by reference ?
Will that copying ever call the copy constructor of val ?
and what's exactly happening here ?
#include <iostream>
#include <vector>
using namespace std;
struct x
{
x(int v = 0) : v(v) {}
int v;
};
vector<vector<x>> parts;
void fillParts()
{
vector<x> values = { x(1), x(2), x(3) };
parts.push_back(values);
}
int main()
{
fillParts();
parts[0][0].v = -123;
cout << parts[0][0].v; // -123
return 0;
}
this runs with no erros,
is parts[0] is a reference to local vector values or a copy ?
if it is a reference shouldn't it at least give some warnings saying that your accessing and modifying local objects of freed stack ?
How it can be copied when it takes val by reference?
Think of a copy constructor.
It takes parameter by reference, and it performs copying perfectly.
class Bar
{
public:
Bar(const Bar & rhs); // by reference, to copy.
};
Will that copying ever call the copy constructor of val ?
Copy operation uses copy constructor.
You can actually see if it's copied, or moved by providing user-defined constructors.
struct x
{
public:
x(const x & rhs)
{
// Some copy operation.
std::cout << "Copied" << std::endl;
}
x(x && rhs)
{
// Some move operation.
std::cout << "Moved" << std::endl;
}
};
You can try this
class A
{
public:
A() {}
A(const A&) { cout << "copy cons" << endl; }
A& operator= (A &&) { cout << "move" << endl; };
A& operator= (const A &) { cout << "copy" << endl; };
};
vector<A> parts;
void fillParts()
{
A a;
parts.push_back(a);
}
int main()
{
fillParts();
return 0;
}
I got copy cons called in both debug and release builds.

out parameters in c++ pass by reference

I want to achieve something similar in c++. This here is a c# code. I want to avoid raw pointers as much as possible.
class Program
{
public class Foo
{
public int v1;
public int v2;
public Foo(int a, int b)
{
v1 =a; v2 =b;
}
};
public class Bar
{
public static void getFoo(out Foo fooObj)
{
fooObj = new Foo(1,2);
}
};
static void Main()
{
Foo fooObj = null;
Bar.getFoo(out fooObj);
Console.WriteLine("Foo.v1="+fooObj.v1);
Console.WriteLine("Foo.v2="+fooObj.v2);
}
}
Here goes my attempt to convert your C# code into C++. However, once you run it you need to do proper research on how to use use all the features I've used here. unique_ptr will basically manage the "raw" pointer for you (which is what you want, and it will free it once it goes out of scope). I've added an improved version using variadic templates so you can pass any number of arguments of any type to dynamically create your Foo class.
#include <memory>
#include <iostream>
class Foo
{
public:
int v1;
int v2;
Foo(int a, int b)
{
v1 =a; v2 =b;
}
};
class Bar
{
public:
// This is what your function looks like in C++
static void getFoo(std::unique_ptr<Foo>& fooObj)
{
fooObj = std::make_unique<Foo>(1, 2);
}
// This is a better implementation.
template<typename ...Args>
static void getFoo_improved(std::unique_ptr<Foo>& fooObj, Args&&... args)
{
fooObj = std::make_unique<Foo>(std::forward<Args>(args)...);
}
// This is the one used more often in C++ tho.
template<typename ...Args>
static std::unique_ptr<Foo> getFoo_improved_x2(Args&&... args)
{
return std::make_unique<Foo>(std::forward<Args>(args)...);
}
};
int main()
{
std::unique_ptr<Foo> fooObj = nullptr; //nullptr is not needed tho
Bar::getFoo(fooObj);
std::unique_ptr<Foo> fooObj_alt = nullptr; //nullptr is not needed tho
Bar::getFoo_improved(fooObj_alt, 9, 10);
//This is as fast as the other two
auto fooObj_alt_x2 = Bar::getFoo_improved_x2(50, 60);
std::cout << "Foo.v1=" << fooObj->v1 << std::endl;
std::cout << "Foo.v2=" << fooObj->v2 << std::endl;
std::cout << "Foo_alt.v1=" << fooObj_alt->v1 << std::endl;
std::cout << "Foo_alt.v2=" << fooObj_alt->v2 << std::endl;
std::cout << "Foo_alt_x2.v1=" << fooObj_alt_x2->v1 << std::endl;
std::cout << "Foo_alt_x2.v2=" << fooObj_alt_x2->v2 << std::endl;
return 0;
}

Misunderstood the virtual functions work in vector

I have following code on c++:
#include <iostream>;
#include <vector>;
class A
{
public:
A(int n = 0) : m_n(n) { }
public:
virtual int value() const { return m_n; }
virtual ~A() { }
protected:
int m_n;
};
class B
: public A
{
public:
B(int n = 0) : A(n) { }
public:
virtual int value() const { return m_n + 1; }
};
int main()
{
const A a(1);
const B b(3);
const A *x[2] = { &a, &b };
typedef std::vector<A> V;
V y;
y.push_back(a);
y.push_back(b);
V::const_iterator i = y.begin();
std::cout << x[0]->value() << x[1]->value()
<< i->value() << (i + 1)->value() << std::endl;
system("PAUSE");
return 0;
}
The compiler returned result: 1413.
I am little bit confused, because I thought the right result would be 1414 (as the function virtual). How do you explain this program behavior?
You are slicing the object, in order to get polymorphism you need to use either a pointer or a reference. This example keeping as close as possible to your original example and using a pointer will act as you wanted:
const A a(1);
const B b(3);
typedef std::vector<const A*> V;
V y;
y.push_back(&a);
y.push_back(&b);
V::iterator i = y.begin();
std::cout << (*i)->value() << std::endl ;
++i ;
std::cout << (*i)->value() << std::endl ;
To show briefly how the object slicing works here:
const A a(1);
const B b(3);
std::vector<A> y; // so y contains objects of type A
y.push_back(a); // y[0] is copy-constructed from a
y.push_back(b); // y[1] is copy-constructed from b
Note that in both push_back calls, it's always an A being constructed, via the automatically-generated A::A(const A&) copy constructor.
Note also that a B is-a A, which is to say b can be implicitly cast to an A and passed into the same copy-constructor.
So, y[1] is an instance of A with the m_n value copied from b, but its virtual function is still A::value. If you have constructor B::B modify the value when it is initialized, instead of when it is returned, you'll see the result you expect.