Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stack overflow when adding parameter to constructor 1

Status
Not open for further replies.

abcd12344445555

Programmer
May 10, 2009
24
BR
Do you have any clues how the addition of a parameter to a constructor can cause a Stack overflow?

The following constructor works just fine:


__fastcall TformInteracaoL::TformInteracaoL(TComponent* Owner)
: TForm(Owner)


but the addition of a simple int parameter causes a Stack Overflow:


__fastcall TformInteracaoL::TformInteracaoL(TComponent* Owner,int sType)
: TForm(Owner)

Regards,
Paul.

 
I can think of a several of reasons why but first let's see of this will fix the problem. Whenever I need to add a parameter to the constructor, I first leave the default constructor alone and add my own below the default.
Code:
__fastcall TAddBundleForm::TAddBundleForm(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
__fastcall TAddBundleForm::TAddBundleForm(AnsiString ABParcelStr, AnsiString ABProdStr, AnsiString ABPartStr, int ABTallyInt, int ABSheetsInt, bool ABPrntBool, AnsiString ABStickerDev, AnsiString ABCompNoStr, double* ABDiffSM, int* ABDiffSF, TComponent* Owner)
    : TForm(Owner)
{
    // a bunch of code
}

Notice that I also add the parameters before the Owner parameter.

Your might look something like:
Code:
__fastcall TformInteracaoL::TformInteracaoL(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
__fastcall TformInteracaoL::TformInteracaoL(int sType, TComponent* Owner)
    : TForm(Owner)
{
    // a bunch of code
}

Finally, I go into the header and add the new constructor into the public section of the class.
Code:
public:		// User declarations
    __fastcall TAddBundleForm(TComponent* Owner); //default
    __fastcall TAddBundleForm(AnsiString ABParcelStr, AnsiString ABProdStr, AnsiString ABPartStr, int ABTallyInt, int ABSheetsInt, bool ABPrntBool, AnsiString ABStickerDev, AnsiString ABCompNoStr, double* ABDiffSM, int* ABDiffSF, TComponent* Owner); // mine

Again, your might look something like this:
Code:
public:		// User declarations
    __fastcall TformInteracaoL(TComponent* Owner); // default
    __fastcall TformInteracaoL(int sType, TComponent* Owner); // yours

If you want, you can put some error coding to inform the user that the constructor is being called without the proper parameters.


James P. Cottingham
[sup]I'm number 1,229!
I'm number 1,229![/sup]
 
Yes, that solved the problem, but I noticed that everytime I add new parameters to the constructor - in this particular form - if I add them after the TComponent* Owner I get a stack overflow, but everything works smoothly if I add BEFORE (happens the same with overloaded constructors).

That's too weird, do you know the reason?

Thanks,
regards,
Paul.
 
I'm not certain of the exact reason, only that all "official" documents say the Owner parameter has to be last. I have my suppositions, though.



James P. Cottingham
[sup]I'm number 1,229!
I'm number 1,229![/sup]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top