The explanation of the matter is that the first opinion, which is the opinion of the Rashba in a responsum, holds that just as the surplus belongs to his heirs—so too all the money, since the money was collected with him in mind, and he acquired it from the time of the collection. And the second opinion is the opinion of the Rosh in a responsum, that specifically when the mitzvah is performed—it is appropriate that the surplus belongs to him and his heirs. But as long as they have not yet begun this mitzvah—it is obvious that they did not volunteer with this in mind. And according to the letter of the law, it would be necessary to return the donations to everyone who gave, except that this is an impossibility, and would incur much expense. Therefore, they use them for the needs of the public. And they decided in accordance with the opinion of the Rosh. Our teacher, the Rema, wrote regarding one who vowed to an orphan girl and she died, that the heirs did not acquire it. That is to say: and the money returns to the volunteer, for it is not similar to that which was collected from the public, as is understood. And our rabbis were not concerned to explain this, that in the first law they use it for the needs of the public, and in the second law it returns to the owners, for this is understood on its own. However, when she is alive—it is given to her immediately. But in Choshen Mishpat there are those who disagree, for our teacher, the Beit Yosef, in siman 253, section 16, ruled that it is given to her before she marries, and her heirs inherit from her. And our teacher, the Rema, ruled otherwise, and we explained this there in section 25, see there that the primary view is like the words of our teacher, the Rema, that it is given to her immediately. And despite all this, if she died—it returns to his heirs and not to her heirs. (And the law of the Rema is also according to the Rashba and is not similar to the public. And be precise and you will find it easy.)
ביאור הדברים דדעה ראשונה, שהיא דעת הרשב"א בתשובה, סוברת דכמו שהמותר שייך ליורשיו – כמו כן כל המעות, כיון דאדעתא דידיה גבו המעות, וזכה משעת הגבייה. ודעה שנייה היא דעת הרא"ש בתשובה, דדווקא כשהמצוה נעשית – שפיר שייך המותר לו וליורשיו. אבל כל שעדיין לא התחילו במצוה זו – פשיטא דאדעתא דהכי לא התנדבו. ומעיקר הדין היה צריך להחזיר הנדבות לכל מי שנתן, אלא שזהו מן הנמנעות, ויפול הוצאות הרבה. ולכן עושין בהם צרכי רבים. והכריעו כדעת הרא"ש. וכתב רבינו הרמ"א באחד שנדר ליתומה ומתה, דלא זכו היורשים. כלומר: וחוזרים המעות להמתנדב, דלא דמי לנגבה מרבים כמובן. ולא חשו רבותינו לבאר זה, דבדין הראשון עושין צרכי רבים, ובדין השני חוזר לבעלים, דממילא מובן זה. אמנם כשהיא חיה – נותנין לה מיד. אך בחושן משפט יש חולקין, דרבינו הבית יוסף בסימן רנ"ג סעיף ט"ז (שולחן ערוך חושן משפט רנג, טז) פסק דנותנין לה קודם שתנשא, ויורשיה יורשין אותה. ורבינו הרמ"א פסק לא כן, ובארנו זה שם בסעיף כ"ה, עיין שם שהעיקר כדברי רבינו הרמ"א שנותנין לה מיד. ועם כל זה אם מתה – חוזרת ליורשיו ולא ליורשיה. (ודינו של הרמ"א הוא גם להרשב"א ול"ד לרבים. ודייק ותמצא קל.)