Board index » cppbuilder » static Variables and Class Member Functions Across Projects

static Variables and Class Member Functions Across Projects

All,

Sorry if this is seen as a double post (I posted this in cppbuilder.language
as well), but I'm not sure if this is a language issue or an IDE/Project
issue.  Anyway, if anyone has any thoughts, please pass them on.  I'm dying
here.

Thanks!
Josh

Quote
> I am trying to create a global class so that two different Borland C++
> Builder classes can access/update the static variables declared within.  I
> only want one instance of the variable SelectedOptionPrice (see below) to
> occur yet the second project always accesses the class like it is the
first
> time the variable has been updated (read: the variable isn't being treated
> as a global).  I've traced through this and there are disparate addresses
> being created for the SelectedOptionPrice variable, so I know that I am
> doing something incorrectly.  Strangely, this code works on an SGI
compiler,
> so I assume that I am fighting the way Borland manages the memory between
> projects.

> If anyone has any ideas (no matter how far-fetched), please, please,
please
> pass them on.  I am facing a deadline on Monday, and this is really
> hampering me.

> Thanks!
> Josh

> <<OptionsSplitter.h>> This is where I have declared the class.  I have
done
> it in two different ways, and both fail in the same way:

> /*class GlobalOptions
>  {
>  private:
>   static float SelectedOptionPrice;

>   //PRIVATE CONSTRUCTOR
>   GlobalOptions();  //This shouldn't do anything since instantiation
> can't happen in a static;

>  public:
>   static void setOptionPrice(float NewSelectedOptionPrice)
>     { SelectedOptionPrice += NewSelectedOptionPrice; }

>                 static float getOptionPrice()
>     { return SelectedOptionPrice; }
>  };
> */

> class GlobalOptions
>         {
>         private:
>                 static GlobalOptions foo;
>                 static float SelectedOptionPrice;

>                 //PRIVATE CONSTRUCTORS
>                 GlobalOptions();
>                 GlobalOptions(const SHBGlobalOptions&);  //Prevent
> copy-construction

>         public:
>                 static GlobalOptions* instance()
>                          {return &foo;}

>                 static void setOptionPrice(float NewSelectedOptionPrice)
>     {SelectedOptionPrice += NewSelectedOptionPrice; }

>                 static float getOptionPrice()
>     {return SelectedOptionPrice; }
>         };

> float GlobalOptions::SelectedOptionPrice = 99;

> <<One thing to note is that I get all kinds of unresolved linking errors
if
> I put the last line in one of my implementation files and not the other>>
> ++++++++++++++++++++++++++++++++++++++++++++++
> <<OptionsSplitter.cpp -- this is in the first project>>
> .....
>         GlobalOptions::setOptionPrice(CustomerRec.FullValue);

>         float tmpSOP;
>         tmpSOP = GlobalOptions::getOptionPrice();

> <<Note: tmpSOP is getting updated correctly >>
> +++++++++++++++++++++++++++++++++++++++++++++++

> <<DesignOptions.cpp -- This file calls OptionsSplitter.cpp at the function
> that contains the previous snip>>
> ....
> void TDesignOptions::LoadSelectedOptions()
> {
>   SaveOptionInsertion = false;   // don't save insert when loading
selected
>   SaveOptionDeletion  = false;   // don't save delete when loading
selected
>   if (OptionsSplitterWindow)

OptionsSplitterWindow->SendMessage(WM_LOADCUSTOMERSELECTEDOPTIONS,0,0L);

- Show quoted text -

Quote
>   //JTA
>  tempSelectedOptionPrice = GlobalOptions::getOptionPrice();
> ...

> <<Note: when I trace through this, a new version of SelectedOptionPrice is
> created despite the static declaration in the header file, and
> tempSelectedOptionPrice is set to 99. >>

 

Re:static Variables and Class Member Functions Across Projects


Make sure that your static variables are declared in a cpp file and
not in the header file.  It may be that for each instance of inclusion
of the header file your getting another copy of the static variables.

On Fri, 6 Jul 2001 16:18:59 -0400, "Josh Asbury"

Quote
<joshasb...@REMOVETOREPLYfuse.net> wrote:
>All,

>Sorry if this is seen as a double post (I posted this in cppbuilder.language
>as well), but I'm not sure if this is a language issue or an IDE/Project
>issue.  Anyway, if anyone has any thoughts, please pass them on.  I'm dying
>here.

>Thanks!
>Josh

>> I am trying to create a global class so that two different Borland C++
>> Builder classes can access/update the static variables declared within.  I
>> only want one instance of the variable SelectedOptionPrice (see below) to
>> occur yet the second project always accesses the class like it is the
>first
>> time the variable has been updated (read: the variable isn't being treated
>> as a global).  I've traced through this and there are disparate addresses
>> being created for the SelectedOptionPrice variable, so I know that I am
>> doing something incorrectly.  Strangely, this code works on an SGI
>compiler,
>> so I assume that I am fighting the way Borland manages the memory between
>> projects.

>> If anyone has any ideas (no matter how far-fetched), please, please,
>please
>> pass them on.  I am facing a deadline on Monday, and this is really
>> hampering me.

>> Thanks!
>> Josh

>> <<OptionsSplitter.h>> This is where I have declared the class.  I have
>done
>> it in two different ways, and both fail in the same way:

>> /*class GlobalOptions
>>  {
>>  private:
>>   static float SelectedOptionPrice;

>>   //PRIVATE CONSTRUCTOR
>>   GlobalOptions();  //This shouldn't do anything since instantiation
>> can't happen in a static;

>>  public:
>>   static void setOptionPrice(float NewSelectedOptionPrice)
>>     { SelectedOptionPrice += NewSelectedOptionPrice; }

>>                 static float getOptionPrice()
>>     { return SelectedOptionPrice; }
>>  };
>> */

>> class GlobalOptions
>>         {
>>         private:
>>                 static GlobalOptions foo;
>>                 static float SelectedOptionPrice;

>>                 //PRIVATE CONSTRUCTORS
>>                 GlobalOptions();
>>                 GlobalOptions(const SHBGlobalOptions&);  //Prevent
>> copy-construction

>>         public:
>>                 static GlobalOptions* instance()
>>                          {return &foo;}

>>                 static void setOptionPrice(float NewSelectedOptionPrice)
>>     {SelectedOptionPrice += NewSelectedOptionPrice; }

>>                 static float getOptionPrice()
>>     {return SelectedOptionPrice; }
>>         };

>> float GlobalOptions::SelectedOptionPrice = 99;

>> <<One thing to note is that I get all kinds of unresolved linking errors
>if
>> I put the last line in one of my implementation files and not the other>>
>> ++++++++++++++++++++++++++++++++++++++++++++++
>> <<OptionsSplitter.cpp -- this is in the first project>>
>> .....
>>         GlobalOptions::setOptionPrice(CustomerRec.FullValue);

>>         float tmpSOP;
>>         tmpSOP = GlobalOptions::getOptionPrice();

>> <<Note: tmpSOP is getting updated correctly >>
>> +++++++++++++++++++++++++++++++++++++++++++++++

>> <<DesignOptions.cpp -- This file calls OptionsSplitter.cpp at the function
>> that contains the previous snip>>
>> ....
>> void TDesignOptions::LoadSelectedOptions()
>> {
>>   SaveOptionInsertion = false;   // don't save insert when loading
>selected
>>   SaveOptionDeletion  = false;   // don't save delete when loading
>selected
>>   if (OptionsSplitterWindow)

>OptionsSplitterWindow->SendMessage(WM_LOADCUSTOMERSELECTEDOPTIONS,0,0L);
>>   //JTA
>>  tempSelectedOptionPrice = GlobalOptions::getOptionPrice();
>> ...

>> <<Note: when I trace through this, a new version of SelectedOptionPrice is
>> created despite the static declaration in the header file, and
>> tempSelectedOptionPrice is set to 99. >>

Other Threads