reference

Can C++ compiler assume a const bool & value will not change?

大憨熊 提交于 2020-01-03 16:38:11
问题 Can the C++ compiler assume a 'const bool &' value will not change? For example, imagine that I have a class: class test { public: test(const bool &state) : _test(state) { } void doSomething() { if (_test) { doMore(); } } void doMore(); private: const bool &_test; }; And I use it as follows: void example() { bool myState = true; test myTest(myState); while (someTest()) { myTest.doSomething(); myState = anotherTest(); } } Is it allowed by the standard for the compiler to assume _test's value

Can C++ compiler assume a const bool & value will not change?

☆樱花仙子☆ 提交于 2020-01-03 16:38:08
问题 Can the C++ compiler assume a 'const bool &' value will not change? For example, imagine that I have a class: class test { public: test(const bool &state) : _test(state) { } void doSomething() { if (_test) { doMore(); } } void doMore(); private: const bool &_test; }; And I use it as follows: void example() { bool myState = true; test myTest(myState); while (someTest()) { myTest.doSomething(); myState = anotherTest(); } } Is it allowed by the standard for the compiler to assume _test's value

JPA Bidirectional relationship - infinite loop / circular reference

与世无争的帅哥 提交于 2020-01-03 15:56:24
问题 I have a bidirectional relationship @Entity @Table(name = "facility") public class Facility implements Serializable { @Id @GeneratedValue private Long id; @OneToMany(mappedBy = "facility") private Set<Amenity> amenities; } @Entity @Table(name = "amenity") public class Amenity implements Serializable { @Id @GeneratedValue private Long id; @ManyToOne private Facility facility; } This all works fine and I can see the table is created correctly. I can add data fine through a rest endpoint and but

How to get the size of an object via reference?

不想你离开。 提交于 2020-01-03 15:33:10
问题 Suppose I have a class class Foo { : : } I have another function void getf( Foo &f) { : : std::cout<<sizeof f<<std::endl; } After I process the data and assign a lot of data to f (vector included in Foo members), I need the size of f object However, as what I did above, I always get 16, which is the size of a reference here. Did I do anything wrong? How to do that? Thanks! 回答1: sizeof returns the size of a type. Often time, as in the case of a Vector , the size of the type does not

Are const references to members safe

六月ゝ 毕业季﹏ 提交于 2020-01-03 13:32:11
问题 If I use a const reference to another member, is it possible that this reference gets invalidated? class Class { public: const int &x{y}; private: int y; }; For example when I use instances of this class in a vector which increases its capacity after a push_back . According to the standard all iterators and references are invalidated if a vector has to increase its capacity. Is the reference still valid after that? 回答1: This is currently not safe, as when you copy an instance of Class , x

Unsureness about passing EndPoint to Socket.ReceiveFrom()

淺唱寂寞╮ 提交于 2020-01-03 11:32:45
问题 If I do something like this: byte[] buffer = new byte[1024]; Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); IPEndPoint remote = new IPEndPoint(IPAddress.Parse("12.34.56.78"), 1337); sock.ReceiveFrom(buffer, ref remote); Will the ReceiveFrom method only receive packets from the endpoint that is being passed? The documentation states the following: With connectionless protocols, ReceiveFrom will read the first enqueued datagram received into the local

Does std::move work with lvalue references? How does std::move work on standard containers?

我是研究僧i 提交于 2020-01-03 11:28:11
问题 #include <vector> struct A { int a[100]; }; void foo (const A& a) { std::vector<A> vA; vA.push_back(std::move(a)); // how does move really happen? } int main () { A a; foo(a); } The above code compiles fine. Now everywhere it's written that move avoids copying. Following are my queries: Does the move really work when one deals with a lvalue [non]- const reference? Even with "rvalue reference", how is the copy avoided when the object is inserted into a standard container like above? e.g. void

Will this lead to a memory leak in C++?

十年热恋 提交于 2020-01-03 10:32:41
问题 I have a C++ memory management doubt, that's (obviously) related to references and pointers. Suppose I have a class Class with a method my_method : OtherClass& Class::my_method( ... ) { OtherClass* other_object = new OtherClass( ... ); return *other_object; } Meanwhile in a nearby piece of code: { Class m( ... ); OtherClass n; n = m.my_method( ... ); } So, I know that there's a general rule about pointers (~ "anything new-ed, must be delete-d") to avoid memory leaks. But basicly I'm taking a

Will this lead to a memory leak in C++?

亡梦爱人 提交于 2020-01-03 10:32:30
问题 I have a C++ memory management doubt, that's (obviously) related to references and pointers. Suppose I have a class Class with a method my_method : OtherClass& Class::my_method( ... ) { OtherClass* other_object = new OtherClass( ... ); return *other_object; } Meanwhile in a nearby piece of code: { Class m( ... ); OtherClass n; n = m.my_method( ... ); } So, I know that there's a general rule about pointers (~ "anything new-ed, must be delete-d") to avoid memory leaks. But basicly I'm taking a

Why can C++ const references be collasped into non-const references

蓝咒 提交于 2020-01-03 08:39:47
问题 Consider the following C++ program: #include <iostream> template<typename T> class A { public: explicit A(T& x) : x_(x){} const T& get() { return x_; } private: T x_; }; int main() { int x = 42; A<int&>(x).get() = 43; // compiles fine, even though get() looks like it returns a const ref std::cout << x << '\n'; } The program compiles OK and outputs 43. This suggests that the seemingly const reference returned by get() is in fact a non-const reference, because it allows to modifies the value it