Thread: Returning the address of a member structure

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    271

    Returning the address of a member structure

    Is it possible to retrieve the address of a member structure?

    I tried the following code but it doesn't work:

    Code:
    class SomeClass
    {
    private:
    struct x
    {int a
    } y;
    public:
    int struct_address(){return &y;}
    }
    
    ...
    
    cout << classinstance.struct_address() << endl;
    How would you access the address of a member variable or structure or whatever?

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    First of all, fix your indentation.

    Second, why do you even want to do this? I can't think of a good reason.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    Nevermind, got it to work.

    Code:
    	...
    	int* struct_address(){return (int*) &y;}
    FYI, I need to know how bitfield variables are configured in the memory so that I can perform a bitfield operation on them later.

  4. #4
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by cunnus88
    Nevermind, got it to work.

    Code:
    	...
    	int* struct_address(){return (int*) &y;}
    FYI, I need to know how bitfield variables are configured in the memory so that I can perform a bitfield operation on them later.
    that's an ugly hack. if you need to return the address of your struct, don't cast away your type info.
    Code:
    class SomeClass
    {
    private:
       struct x
       {
           int a
        } y;
    public:
       // return type matches type being returned
       x* struct_address(){return &y;}
    };
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    That kind of interface is very bad.
    You should never allow direct access to the internal data of your class.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-19-2008, 12:06 AM
  2. I thought pointers were pointers...
    By keira in forum C Programming
    Replies: 19
    Last Post: 08-15-2007, 11:48 PM
  3. Address will always return true? (class vectors + member vars)
    By littleweseth in forum C++ Programming
    Replies: 12
    Last Post: 11-27-2003, 01:49 AM
  4. Oy, question on linked lists
    By tammo21 in forum C Programming
    Replies: 8
    Last Post: 08-18-2002, 05:14 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM