"Array& params": How to understand "&" after "Array" - type modifer?

-1

I'm trying to understand how "invalidateblock"-function works, literally line by line.

Since I'm at basic level in C++, I couldn't explain to myself what's the "&" after "Array" there to achieve though I know of references and pointers and though I've found out that it's a so called "type modifier". Still I'm wondering "Array" is a type just like "int", "char" etc.

Where can I find more information on where Array is defined/explained?What are member functions?

Thanks in advance!

Aliakbar Ahmadi

Posted 2015-05-18T18:41:27.517

Reputation: 1 335

1

I'm voting to close this question as off-topic because this question is not related to Bitcoin specifically and would be better suited for http://stackoverflow.com

cdecker 2015-05-18T19:11:17.077

Ok, but I asked it here in order to hear developer's answers or comments who have written the code and are represented in Bitcoi.SE.Aliakbar Ahmadi 2015-05-18T20:41:14.717

Can you provide a link to the particular software your question is about?David Schwartz 2015-05-18T23:05:02.460

@DavidSchwartz: I've been looking at rpcblockchain.cpp of bitcoin-0.10.0Aliakbar Ahmadi 2015-05-19T10:43:39.457

Answers

1

It's part of the JSON code. An Array is a JSON entity that contains an ordered list of zero or more JSON entities. As its name, "params", suggests, this one contains the parameters to the command. So when you see:

std::string strHash = params[0].get_str();

That gets the first parameter into a string called strHash. This makes sense since the first (and only) parameter to invalidateblock is a hash, as this help text indicates:

        "invalidateblock \"hash\"\n"
        "\nPermanently marks a block as invalid, as if it violated a consensus rule.\n"
        "\nArguments:\n"
        "1. hash   (string, required) the hash of the block to mark as invalid\n"
        "\nResult:\n"
        "\nExamples:\n"

David Schwartz

Posted 2015-05-18T18:41:27.517

Reputation: 46 931

Thanks! But what's the purpose of "&" after "Array"? I think type modifiers, such as "&", are used in programming to convert a data type into another and obviously Array is being converted into a reference type while it's defined in the "invalidateblock"-function so can I assume that this conversion makes references out of Array's params? And if yes, what variables are they references to? What do they have to do with get_str()?Aliakbar Ahmadi 2015-05-19T10:55:02.747

@AliakbarAhmadi It sounds like you need to learn C++ and then come back and look at this code.David Schwartz 2015-05-19T16:41:55.390

I know I'm not using the best technical terms but would you give me a hint to understand: why I need to put a "&" after "Array" though - to my limited knowledge - you could leave it away and simply write ‍‍‍Value invalidateblock(const Array params, bool fHelp) {...‎‎‏}? - I found this but it's not really helping to understand this problem. But I think get_str() is just putting the hash value as a string to params[0]!?

Aliakbar Ahmadi 2015-05-19T17:10:12.603

1@AliakbarAhmadi Why do you think you need to do that? In this case, the parameter could be passed by value or by reference, but passing by reference is cheaper. Competent C++ coders reflexively pass "complex" classes by reference when that is possible because it avoids copying the value.David Schwartz 2015-05-19T17:16:22.300

David Schwartz, thanks! Finally you gave the answer and I can sleep well :D +1Aliakbar Ahmadi 2015-05-19T18:26:03.030