gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > const and class

#27262 - Glehenna - Thu Oct 07, 2004 8:21 pm

Consider this:

Code:

class MyClass
{
public:
  int x;
  ...
};

........

const MyClass myclass;


Now, my question is... will 'x' be placed in ROM? Even though 'x' itself is not declared as a 'const'

#27264 - sajiimori - Thu Oct 07, 2004 8:50 pm

Yes. Declaring 'x' as const may not even create a memory object at all, since C++ const primitives (like ints) are sometimes treated more like #defines.

#27265 - Glehenna - Thu Oct 07, 2004 9:04 pm

Thank you sajiimori :)

#27266 - jma - Thu Oct 07, 2004 9:20 pm

Yes, it can go into ROM, provided the following conditions are met when delcaring the class:

    1. The mutable keyword isn't used in the class
    2. It must have no user-defined constructors or destructor (this code must be called at runtime)
    3. There can't be any base classes with user-defined constructors or destructors

Hope this helps,

Jeff
_________________
massung@gmail.com
http://www.retrobyte.org

#27273 - sajiimori - Thu Oct 07, 2004 10:08 pm

Oh yeah, I'm not used to thinking about those things. I've only used C++ on PC.