Thread: Initializing a reference within a structure

  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,817
    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).
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  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,817
    struct or class, it doesn't really matter that much, structs can have constructors and member functions just like classes do.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

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