Thread: static reference member

  1. #1
    C++11 User Tux0r's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    135

    static reference member

    Currently I have a static pointer member to a std::vector<std::string>> in my abstract class.

    Now I wonder is there a way to have a reference to a T instead of a pointer? The trouble I'm having is that I have to initialize it, and I don't want to because it would make no sense to statically initialize the member since the target is not global (as no variables should be).

    I'd like to stay away from this pointer if there is a way but alas, maybe this class is doomed to fail in real life by the hands of C code.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You can't reassign references. So the answer is no.

    But you can make the static pointer private and provide a static member function that returns a reference for the actual access.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    C++11 User Tux0r's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    135
    Ok. Perhaps there is a better way to design this though. This is what I have and I don't know of a better way without using globals.

    (next two blocks are in namespace Item)
    Code:
    	class Item {
    		private:
    			const unsigned short sprite;
    
    		public:
    			static const std::vector<std::string> *strings;
    
    			virtual void aquire()=0;
    
    			Item(const unsigned short&);
    			virtual ~Item();
    	};
    
    	Item::Item(const unsigned short& a): sprite{a} {}
    	Item::~Item() {}
    
    	const std::vector<std::string> *Item::strings{}; //the static construct
    This is how Item will be used, as the strings std::vector.

    Code:
    	class Smallkey: public Item {
    		public:
    			void aquire();
    
    			Smallkey();
    	};
    
    	Smallkey::Smallkey(): Item{0} {}
    
    	void Smallkey::aquire() {
    		std::cout<<(*strings)[0]<<std::endl;
    	}
    main, where assignment occurs.

    Code:
    //this is main
    	const std::vector<std::string> strings{"Foo","Foo","Foo","Foo","Foo","Foo","Foo"};
    
    	Item::Item::strings=&strings; //the relevant construct
    edit: the reason I don't have the std::vector with the data in the actual class is because more classes will use this vector, and it will be loaded dynamically at run-time.
    Last edited by Tux0r; 07-17-2009 at 04:15 AM.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't see why you need static in the first place. Make an instance of the class and use it.
    Furthermore, one instance is one item, so it makes sense to have several instances if you want several items.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    C++11 User Tux0r's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    135
    Quote Originally Posted by Elysia View Post
    I don't see why you need static in the first place. Make an instance of the class and use it.
    Furthermore, one instance is one item, so it makes sense to have several instances if you want several items.
    Item is an abstract class. What exactly do you mean?

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    For example, if you wanted a player to have several items, then this might be in the player class:
    Code:
    std::vector< std::tr1::shared_ptr<Item> > m_Items;
    And to add a new item, you would do:
    Code:
    m_Items.push_back( std::tr1::shared_ptr(new Smallkey) );
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    C++11 User Tux0r's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    135
    My problem is not relevant to how polymorphism works, I know that already. I want to know if there is a more beautiful way of, in my classes, accessing the strings loaded at run-time!

    off-topic: isn't this just so elegant?

    Code:
    std::vector<Object::Object*> objects{
    	new Object::Chest{new Item::Rupee{20}},
    	new Object::Chest{new Item::Smallkey},
    	new Object::Chest{new Item::Feather}
    };
    Last edited by Tux0r; 07-17-2009 at 04:42 AM.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    But as I try to explain, I fail to see why you need to. Why not take another, better approach?
    Your approach is troublesome and definitely not a good polymorph-based approach for games.
    And no, it's not elegant. You've replaced everything with { and }, including ( and ).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    C++11 User Tux0r's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    135
    I'd like to see what that other, better approach is. Thereof this thread.

    Hey you, it's C++0x uniform coolness!

  10. #10
    The larch
    Join Date
    May 2006
    Posts
    3,573
    My default choice would be to pass the string (or vector of strings if more than one is needed) to the acquire method. No good?

    Hey you, it's C++0x uniform coolness!
    Must upgrade compiler
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  11. #11
    C++11 User Tux0r's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    135
    Quote Originally Posted by anon View Post
    My default choice would be to pass the string (or vector of strings if more than one is needed) to the acquire method. No good?
    It's a lot of passing, my thought was it would be faster to just have the pointer at the abstract base. If not I would have to pass strings/vector with strings to every method that might use strings! I don't think it's abstract enough to implement. But what do I know.

    Quote Originally Posted by anon View Post
    Must upgrade compiler
    :-D If you are running Windows you can get weekly builds of gcc here Fortran, C, C++ for Windows this weeks' build will arrive today if it doesn't fail to compile! And if you didn't already know, compile your source with the argument -std=gnu++0x to make c++0x code work.

  12. #12
    The larch
    Join Date
    May 2006
    Posts
    3,573
    My next idea would be to give a reference/pointer to the vector to the objects in their constructor.

    Actually, if you want the static vector, why does it have to be a pointer/reference? Why not simply add the strings to the static vector in main directly if you so wish?
    Last edited by anon; 07-17-2009 at 05:44 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  13. #13
    C++11 User Tux0r's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    135
    Ok but wouldn't that end up in a lot of duplicate pointers? Maybe it's worth it from a design PoV though...

  14. #14
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    The magical re-assignable reference!

    Code:
    #include <iostream>
    
    template < typename T >
    class Reference
    {
    public:
        Reference( T &obj )
            : mPtr( &obj )
        {
        }
    
        Reference &operator=( const T &obj )
        {
    	*mPtr = obj;
        }
    
        operator T&()
        {
            return *mPtr;
        }
    
        void Reassign( T &obj )
        {
            mPtr = &obj;
        }
    
    private:
        T *mPtr;
    };
    
    void Foo( int &x )
    {
        x = 30;
    }
    
    int main()
    {
        int a, b, c;
        Reference< int > x ( a );
    
        x = 15; // You can assign
        std::cout << a << std::endl;
        x.Reassign( b ); // You can reassign to a different object
        x = 20;
        x == a; // You can compare
        Foo( x ); // You can pass it to a function that takes a reference
        // etc...
        std::cout << b << std::endl;
    }
    Now, this thing is evil -- don't use it!
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  15. #15
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    The magical re-assignable reference!
    Your operator=() has issues.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. Reference to singleton as member
    By JaWiB in forum C++ Programming
    Replies: 6
    Last Post: 01-29-2006, 01:27 PM
  4. Using private class members in static functions
    By sethjackson in forum C++ Programming
    Replies: 2
    Last Post: 09-23-2005, 09:54 AM
  5. illegal references in static member functions
    By bennyandthejets in forum Windows Programming
    Replies: 10
    Last Post: 12-31-2002, 10:11 AM