Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

PACKAGE and STATIC class member bug???

63 views
Skip to first unread message

Clayton M. Arends

unread,
Jul 25, 2000, 3:00:00 AM7/25/00
to
All,

I've had a problem (that I recently found a solution for) regarding packages
and static members of a class in BCB5. I've read a few posts in the past
regarding this problem and felt others may benefit from what I have found.

The problem can be described as follows. A class has been declared in a
package with the PACKAGE macro modifier. I don't know if deriving the class
from TObject is necessary for the bug(?) to appear but I always derived from
it (or descendants) in my test cases. Declare a public static member in the
class (in my case of type String):

class PACKAGE TMyTestClass : public TObject
{
public:
static const String BugTest;
};

The const modifier is not necessary for the bug(?) to appear. Now, in the
CPP file make use of this variable somewhere in code before actually
defining the variable. This function can be a stand-alone function or a
class method, it doesn't matter:

void __fastcall QuickUse()
{
ShowMessage(TMyTestClass::BugTest);
}

const String TMyTestClass::BugTest = "I left my heart in SF";

Now, compile the package and you will receive the following error message at
the definition line:

[C++ Error] E2233 Cannot initialize a class member here

The help mentions nothing about static class members for this error code.
If you move the definition above the first use of the member then the
package compiles just fine:

const String TMyTestClass::BugTest = "I left my heart in SF";
void __fastcall QuickUse() ...

This is a very simple fix but has plagued me for a couple days. At first, I
gave up and converted the static member to a global variable and continued
on my merry way. However, I revisited this problem this morning (with an
epiphany of sorts) and discovered how to fix it.

Using variables in this way outside of a package is never a problem. It
only occurs in a package where the class has been declared with the PACKAGE
macro. I hope this will help some people in the future.

- Clayton

0 new messages