Tuesday 20 June 2017

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than the constructor would not already implicitly do.


In the second case, without them, the compiler will expect an implementation elsewhere - such as a .cpp file.

Why is Gwen Stacy (Emma Stone) in the Amazing Spider-man? - Movies & TV



This is a re-telling of the origin story of Spider-man. There wasn't any girlfriend in the comic origin. I understand that a love interest is needed in a movie but why was Gwen Stacy chosen instead of the better known Mary Jane Watson? Gwen Stacy has a tragic death in the comic, which means they could potential kill her in the re-boot. Why bring her into a retelling?


Answer




From Marvel.com:





Gwen Stacy was Peter Parker's first true love.



Next to the death of his Uncle Ben, no death has weighed as heavily upon Spider-Man's shoulders as her passing.



Death of Gwen Stacy





From producer Matt Tolmach:





The relationship between Peter and Gwen is very significant – the previous movies haven’t explored this until now.



Gwen is a very self-assured character; she’s his rival intellectually. And her father happens to be Captain Stacy and let’s be honest, it’s hard enough to meet your girlfriend’s parents for the first time, but when he happens to be the head of the police force that’s chasing you, it makes things that much more complicated.



But there’s an emotional honesty and partnership that’s unique to their relationship. Gwen is really the only person who truly knows Peter – and because of that, there’s a closeness that develops between the two of them that neither of them have with anyone else in their lives.





From director Marc Webb:





One of the reasons why I wanted to use Gwen — first and foremost, she’s his first love in the comics. Let’s just set the record straight, it’s not Mary Jane.



But I like the idea of following somebody who is as smart, if not smarter, than Peter Parker. And Emma Stone is the perfect woman to play somebody who is much more proactive, much more intelligent and feisty.



I just like that dynamic in relationships in movies where they’re kind of lovers as rivals, you know? There’s this back and forth that I love, in the laboratory, and there’s just this great bond that you feel between them. She’s not just a prize, she’s not just a damsel in distress. She’s a confidante, and that was a really important thing.





I'd say using Gwen Stacy instead of Mary Jane also helps distinguish The Amazing Spider-Man from Raimi's Spider-Man.


debugging - Why do I get: double free or corruption (top) after running this c++ script?

I've got the following piece of code:



Class Chain


with



char* c; 



as its only public atrib



istream& operator>>(istream& i, Chain& s) {
delete [] s.c;
const int L = 256;
char *t = new char[L];
i.getline(t,L);
s.c = t;

return i;
}

ostream& operator<<(ostream& o, Chain s) {
o << s.c;
return o;
}

#include
#include "Chain.h"

using namespace std;

int main(){

Chain id;

cin >> id;

cout << id;
cout << id;



After running the code under the Eclipse IDE on Xubuntu (last version) I get the following error:




Error in [...] double free or corruption (top): 0x00000000008fd290 ***




What could be wrong?

Monday 19 June 2017

c# - How to add double quotes to a string that is inside a variable?

If you have to do this often and you would like this to be cleaner in code you might like to have an extension method for this.



This is really obvious code, but still I think it can be useful to grab and make you save time.



  /// 
/// Put a string between double quotes.

///

/// Value to be put between double quotes ex: foo
/// double quoted string ex: "foo"
public static string AddDoubleQuotes(this string value)
{
return "\"" + value + "\"";
}


Then you may call foo.AddDoubleQuotes() or "foo".AddDoubleQuotes(), on every string you like.




Hope this help.

Open and define two excel files in VBA

Answer


As a part of a bigger macro, and need to open and define two workbooks and sheets. (I am aware of that I define my worksheets as Variant, I need this for futhure operations). I get a mistake when I try to set value to SheetRI. Does anyone see what it can be? Thanks in advance!



Sub compareQRTsAll()

Dim ActiveWb As Workbook
Dim ActiveSh As Worksheet
Dim SheetFasit As Variant
Dim SheetRI As Variant
Dim FolderFasit As String
Dim FileFasit As String

Dim FolderRI As String
Dim FileRI As String
Dim WbFasit As Workbook
Dim WbRI As Workbook
Dim WbFasitPath As String
Dim strRangeToCheck As String
Dim nShFasit As Integer
Dim nShRI As Integer
Dim iRow As Long
Dim iCol As Long

Dim i As Integer
Dim j As Integer
i = 2
j = 6

Set ActiveWb = ActiveWorkbook
Set ActiveSh = ActiveWb.Worksheets(1)
strRangeToCheck = "A1:AAA1000"
ActiveSh.Range("A2:D10000").Clear


FolderFasit = ActiveSh.Range("J6")
FolderRI = ActiveSh.Range("J7")

Do While ActiveSh.Cells(j, 8) <> ""

FileFasit = Dir(FolderFasit & "\*" & ActiveSh.Cells(j, 8) & "*.xls*")
Set WbFasit = Workbooks.Open(Filename:=FolderFasit & "\" & FileFasit)
SheetFasit = WbFasit.Worksheets(1).Range(strRangeToCheck)
nShFasit = WbFasit.Sheets.Count


FileRI = Dir(FolderRI & "\*" & ActiveSh.Cells(j, 8) & "*.xls*")
Set WbRI = Workbooks.Open(Filename:=FolderRI & "\" & FileRI)
SheetRI = WbRI.Worksheets(1).Range(strRangeToCheck) '<-------------THIS DOESN'T WORK
nShRI = WbRI.Sheets.Count


If nShFasit <> nShRI Then
MsgBox "QRT " & ActiveSh.Cells(j, 8) & " has different number of sheets in fasit and in RI. Further check will not be performed"

ElseIf nShFasit = nShRI And nShFasit = 1 Then


For iRow = LBound(SheetFasit, 1) To UBound(SheetFasit, 1)
For iCol = LBound(SheetFasit, 2) To UBound(SheetFasit, 2)
If SheetFasit(iRow, iCol) = SheetRI(iRow, iCol) Then

' Do nothing.
Else
ActiveSh.Cells(i, 1) = "Check row " & iRow & ", column " & iCol & " in " & ActiveSh.Cells(j, 8)
ActiveSh.Cells(i, 2) = SheetFasit(iRow, iCol)
ActiveSh.Cells(i, 3) = SheetRI(iRow, iCol)

i = i + 1
End If
Next iCol
Next iRow

End If


'close workbooks



Dim wb As Workbook
For Each wb In Workbooks
If Not wb Is ActiveWb Then
wb.Close SaveChanges:=False
End If
Next wb



j = j + 1
Loop

End Sub

What are the Android SDK build-tools, platform-tools and tools? And which version should be used?

I'll leave the discussion of the difference between Build Tools, Platform Tools, and Tools to others. From a practical standpoint, you only need to know the answer to your second question:


Which version should be used?


Answer: Use the most recent version.


For those using Android Studio with Gradle, the buildToolsVersion has to be set in the build.gradle (Module: app) file.


android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
...
}

Where do I get the most recent version number of Build Tools?


Open the Android SDK Manager.



  • In Android Studio go to Tools > Android > SDK Manager > Appearance & Behavior > System Settings > Android SDK

  • Choose the SDK Tools tab.

  • Select Android SDK Build Tools from the list

  • Check Show Package Details.


The last item will show the most recent version.


enter image description here


Make sure it is installed and then write that number as the buildToolsVersion in build.gradle (Module: app).

javascript - What does "this" mean in jQuery?




In jquery, what does this means and when it is used?


Answer



this in JavaScript is very special and powerful. It can mean just about anything. I cover some of it here and here, but it's really worth finding a good tutorial on JavaScript and spending some time with it.



Let's look at jQuery's use of it first, then talk about it more generally in JavaScript (a bit).



In jQuery, specifically




In code written with jQuery, this usually refers to the DOM element that's the subject of the function being called (for instance, in an event callback).



Example jQuery event callback (what this is is covered in the .bind docs):



$("div").click(function() {
// Here, `this` will be the DOM element for the div that was clicked,
// so you could (for instance) set its foreground color:
this.style.color = "red";

// You'll frequently see $(this) used to wrap a jQuery object around the

// element, because jQuery makes lots of things a lot simpler. You might
// hide the element, for example:
$(this).hide();
});


Similarly, various jQuery functions that act on all of the elements matched by the current jQuery selector can optionally accept a function, and when that function gets called, this is again the DOM element in question — for instance, the html function allows this:



// Find all divs inside the `foo` element, and set
// their content to their CSS class name(s)

// (Okay, so it's a hokey example)
$("#foo div").html(function() {
return this.className;
});


Another place jQuery uses this is in the callback on jQuery.each:



var a = ["one", "two", "three"];
jQuery.each(a, function() {

alert(this);
});


...which will alert "one", then "two", then "three". As you can see, this is a totally different usage of this.



(Confusingly, jQuery has two functions called each, the one above which is on the jQuery/$ function itself and always called that way [jQuery.each(...) or $.each(...)], and a different one on jQuery instances [objects] rather than the jQuery/$ function iself. Here are the docs for the other one, I don't discuss the other one in this answer because it uses this the same way html and event callback do, and I wanted to show a different use of this by jQuery.)



Generically in JavaScript




this refers to an object. Update: As of ES5's strict mode, that's no longer true, this can have any value. The value of this within any given function call is determined by how the function is called (not where the function is defined, as in languages like C# or Java). The most common way to set up this when calling a function is by calling the function via a property on the object:



var obj = {};
obj.foo = function() {
alert(this.firstName);
};
obj.firstName = "Fred";
obj.foo(); // alerts "Fred"



There, because we called foo via a property on obj, this was set to obj for the duration of the call. But don't get the impression that foo is in any way married to obj, this works just fine:



var obj = {};
obj.foo = function() {
alert(this.firstName);
};
obj.firstName = "Fred";
obj.foo(); // alerts "Fred"

var differentObj = {};

differentObj.firstName = "Barney";
differentObj.bar = obj.foo; // Not *calling* it, just getting a reference to it
differentObj.bar(); // alerts "Barney"


In fact, foo isn't intrinsically tied to any object at all:



var f = obj.foo; // Not *calling* it, just getting a reference to it
f(); // Probably alerts "undefined"



There, since we didn't call f via an object property, this wasn't explicitly set. When this isn't explicitly set, it defaults to the global object (which is window in browsers). window probably doesn't have a property firstName, and so we got "undefined" in our alert.



There are other ways to call functions and set what this is: By using the function's .call and .apply functions:



function foo(arg1, arg2) {
alert(this.firstName);
alert(arg1);
alert(arg2);
}


var obj = {firstName: "Wilma"};
foo.call(obj, 42, 27); // alerts "Wilma", "42", and "27"


call sets this to the first argument you give it, and then passes along any other arguments you give it to the function it's calling.



apply does exactly the same thing, but you give it the arguments for the function as an array instead of individually:



var obj = {firstName: "Wilma"};

var a = [42, 27];
foo.apply(obj, a); // alerts "Wilma", "42", and "27"
// ^-- Note this is one argument, an array of arguments for `foo`


Again, though, there's a lot more to explore about this in JavaScript. The concept is powerful, a bit deceptive if you're used to how some other languages do it (and not if you're used to some others), and worth knowing.



Here are some examples of this not referring to an object in ES5's strict mode:



(function() {

"use strict"; // Strict mode

test("direct");
test.call(5, "with 5");
test.call(true, "with true");
test.call("hi", "with 'hi'");

function test(msg) {
console.log("[Strict] " + msg + "; typeof this = " + typeof this);
}

})();


Output:



[Strict] direct; typeof this = undefined
[Strict] with 5; typeof this = number
[Strict] with true; typeof this = boolean
[Strict] with 'hi'; typeof this = string



Whereas in loose mode, all of those would have said typeof this = object; live copy.


c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...