CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jun 2008
    Posts
    2

    Static variables + polymorphism?

    Hi,
    So I'm trying to write a program where there is a base class (called Segment) which implements a few basic functions that all its child classes (PacketHeader, etc.) will use.
    The problem is, PacketHeader and other classes like it have class variables: that is, variables that are the same for each instance of the class, so static. The functions implemented by Segment should use the values for the class calling instead of the ones defined for Segment. Is there any way to do this?

    Here's some sample code:
    Code:
     
    class Segment
    {
    	protected:
    		char voidchar;
    		void *ptrarray[];
    		static char *fieldname[];
    		static char type[];
    	public:
    		Segment();
    		int getType(int index);
    		char* getFieldname(int index);
    		
    		virtual int writeField(int index, void *buffer);
    		virtual int dump(char *delimiter);
    
    		static int totalfields;
    };
    
    class PacketHeader : public Segment
    {
    	protected:
    		void *ptrarray[10];
    		static char *fieldname[10];
    		static char type[10];
    	public:
    		PacketHeader();
    		
    		static int totalfields;
    		char versionID[2];
    		unsigned int size;
    		char date[10];
    };
    (Quick note about the above code: all the scope, virtuals, etc. is very temporary as I'm trying to get this to work.)

    So to clarify, is it possible to make it so when a PacketHeader object calls the writeField or dump functions, those functions use the PacketHeader's version of fieldname and type?

    Thanks a lot!

    Edit: When I take out the re-declarations of the static variables from the derived class, trying to initialize the fieldname, type fields leads to this error:
    filename.cpp:175: error: ISO C++ does not permit 'Segment::fieldname' to be defined as 'PacketHeader::fieldname'
    I'm guessing this is my real problem. Is there another solution except to make those variables not static?
    Thanks again.
    Last edited by scellers; June 26th, 2008 at 12:46 PM.

  2. #2
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Static variables + polymorphism?

    I'm not sure I understand. You have static arrays in the base class, and you want to have the derived classes determine their sizes? If so, just use a vector, and resize it in the constructor of the derived class.

    Viggy

  3. #3
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Static variables + polymorphism?

    I agree with MrViggy based on the way the situation is explained, but the concept of class level data that is polymorphic is a common and viable one.

    The solution is actually VERY simple.

    static private data that is NEVER directly accessed except by virtual accessor methods. Thus you get one copy of the data that is shared by all instances of the class, yet the values can be treated polymorhpically.

    To avoid duplication issues, you want to follow Scott Meyers' advice of make ALL non-leaf classes abstract. Then your base class(s) will have pure accessors, and only the (effective) leaves will implement the data and provide functional accessors.

    The only overhead for this is 2 pointers per class (not per instance) for each accessable piece of data in the _vtable. Unless you are working on a VERY small embedded system, this should NEVER be an issue...
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  4. #4
    Join Date
    Jun 2008
    Posts
    2

    Re: Static variables + polymorphism?

    Sorry for not explaining that very well, but TheCPUWizard explained exactly what I wanted to know. : ) Thanks a lot!
    And yes, the problem was whether I could have class-level data that was polymorphic.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured