Thread: const elements of struct / class not accessible?

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    69

    [Solved] const elements of struct / class not accessible?

    I currently have a class that I'm working with, and that class has structs inside it. I want these structs to contain lists of GUIDs for different situations (a struct with paytype IDs, another with notetype IDs, etc.) so that I can get the GUIDs out of my scripts and have them in one easily modifiable section.

    However, I don't want to have to bother setting the elements of these structs separately, so I thought I'd use const on the internal variables, which allowed me to set the values inside the struct definition. When I started using const variables, I was unable to see the internal variables when operating on an instance of the class (with its internal member struct).

    As soon as I remove const, the variable becomes visible.

    The other problem is that these GUIDs are being used in case statements in my other scripts, and thus have to be of a const type.

    Is there any way around this? Or will I just have to go back to using hard coded GUIDs in each and every file?


    Here is an example of the struct that I'm working with (note: I changed these to nested classes, but it didn't help):

    Code:
    public class ShipType
    {
    	public  string shipper		= "guid goes here";
    	public  string consignee	= "guid goes here";
    	public  string thirdParty	= "guid goes here";
    }
    public ShipType	shipType	= new ShipType();
    And I want the use of these values to be:

    Code:
    g_vars.payType.prepaid
    where g_vars is an instance of the parent class of ShipType.

    I'll post back with more info if I wasn't clear enough in any of my explanation
    Last edited by talz13; 03-24-2006 at 01:32 PM. Reason: Solved

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    69

    I think I found out...

    So const is some kind of subset of static, which means you have to access the variable via the class itself instead of through the instance of the class.

    In my case, this would be
    Code:
    Banyan.Globals.PayType.prepaid
    instead of
    Code:
    g_vars.PayType.prepaid

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    Yes, const members of a class are automatically static, which belong to the class, not any particular instance of the class. And the syntax usage is: ClassType.member.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  3. Weird errors.
    By Desolation in forum C++ Programming
    Replies: 20
    Last Post: 05-09-2007, 01:10 PM
  4. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  5. Bi-Directional Linked Lists
    By Thantos in forum C Programming
    Replies: 6
    Last Post: 12-11-2003, 10:24 AM