Why can’t I store a value and a reference to that value in the same struct?

I have a value and I want to store that value and a reference to something inside that value in my own type: struct Thing { count: u32, } struct Combined<‘a>(Thing, &’a u32); fn make_combined<‘a>() -> Combined<‘a> { let thing = Thing { count: 42 }; Combined(thing, &thing.count) } Sometimes, I have a value and … Read more

When should I use the new keyword in C++?

I’ve been using C++ for a short while, and I’ve been wondering about the new keyword. Simply, should I be using it, or not? With the new keyword… MyClass* myClass = new MyClass(); myClass->MyField = “Hello world!”; Without the new keyword… MyClass myClass; myClass.MyField = “Hello world!”; From an implementation perspective, they don’t seem that … Read more

Why does the Rust compiler not optimize code assuming that two mutable references cannot alias?

As far as I know, reference/pointer aliasing can hinder the compiler’s ability to generate optimized code, since they must ensure the generated binary behaves correctly in the case where the two references/pointers indeed alias. For instance, in the following C code, void adds(int *a, int *b) { *a += *b; *a += *b; } when … Read more

Is the practice of returning a C++ reference variable evil?

This is a little subjective I think; I’m not sure if the opinion will be unanimous (I’ve seen a lot of code snippets where references are returned). According to a comment toward this question I just asked, regarding initializing references, returning a reference can be evil because, [as I understand] it makes it easier to … Read more

JavaScript by reference vs. by value [duplicate]

This question already has answers here: Is JavaScript a pass-by-reference or pass-by-value language? (33 answers) Closed 7 years ago. I’m looking for some good comprehensive reading material on when JavaScript passes something by value and when by reference and when modifying a passed item affects the value outside a function and when not. I’m also … Read more

Why can’t I make a vector of references?

When I do this: std::vector<int> hello; Everything works great. However, when I make it a vector of references instead: std::vector<int &> hello; I get horrible errors like error C2528: ‘pointer’ : pointer to reference is illegal I want to put a bunch of references to structs into a vector, so that I don’t have to … Read more

Does JavaScript pass by reference? [duplicate]

This question already has answers here: Is JavaScript a pass-by-reference or pass-by-value language? (33 answers) Closed 2 years ago. Does JavaScript pass by references or pass by values? Here is an example from JavaScript: The Good Parts. I am very confused about the my parameter for the rectangle function. It is actually undefined, and redefined … Read more

The type or namespace name could not be found [duplicate]

This question already has answers here: Getting “type or namespace name could not be found” but everything seems ok? (44 answers) Closed 7 years ago. I have a C# solution with several projects in Visual Studio 2010. One is a test project (I’ll call it “PrjTest“), the other is a Windows Forms Application project (I’ll … Read more

MongoDB relationships: embed or reference?

I’m new to MongoDB–coming from a relational database background. I want to design a question structure with some comments, but I don’t know which relationship to use for comments: embed or reference? A question with some comments, like stackoverflow, would have a structure like this: Question title=”aaa” content = bbb’ comments = ??? At first, … Read more