Initializing a reference within a structure

This is a discussion on Initializing a reference within a structure within the C++ Programming forums, part of the General Programming Boards category; How does one initialize a reference within a structure? Example: Code: struct _s { char& c; }...

  1. #1
    Registered User Russell's Avatar
    Join Date
    May 2004
    Posts
    71

    Initializing a reference within a structure

    How does one initialize a reference within a structure?

    Example:
    Code:
    struct _s {
      char& c;
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,672
    The reference must be initialized when you instantiate the struct. I believe in that case you've shown, you'd have to create a constructor for the struct that initialized the reference to something. There may be a problem with multiple instances of the struct as they might have all there c data members initialized to the same value (You'd have to check that out yourself).
    I used to be an adventurer like you... then I took an arrow to the knee.

  3. #3
    Registered User Russell's Avatar
    Join Date
    May 2004
    Posts
    71
    So I might as well use a class. Thanks.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,672
    struct or class, it doesn't really matter that much, structs can have constructors and member functions just like classes do.
    I used to be an adventurer like you... then I took an arrow to the knee.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Makefile
    By pinkprincess in forum C Programming
    Replies: 3
    Last Post: 06-24-2007, 09:02 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Gnarly Linking Error HELP!!!!
    By brooksbp in forum C++ Programming
    Replies: 8
    Last Post: 05-04-2007, 01:00 AM
  4. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  5. OpenGL vs. DirectX ???
    By Grumpy_Old_Man in forum Game Programming
    Replies: 55
    Last Post: 08-18-2003, 11:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21