Like Tree1Likes

How do I inherit static data members ?

This is a discussion on How do I inherit static data members ? within the C++ Programming forums, part of the General Programming Boards category; I can't get the syntax right. Suppose, the base class has a (static) string called foo. I want the derived ...

  1. #1
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498

    How do I inherit static data members ?

    I can't get the syntax right.
    Suppose, the base class has a (static) string called foo.
    I want the derived classes to maintain their own (constant) values of foo individually and have the same values for same types of objects created.
    How do I do it ?
    Is it better to make them non static and set constant values by the respective constructors ?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,445
    Override a virtual function to return the desired string for each derived class.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    That would work, thanks.

    The only scenario I can think of that failing is when I need, to make objects of the base class directly from stored/shared data.
    What I'm trying to do is to, set up a 'sort of' dynamic typing thing where all types of objects will be derived from a C++ class or direct instances of the base, but the class name, data member names and values will be available as strings. The derived ones will define their C++ functions for operating on the data.
    I want to keep the option of making objects of the base as the data can be made up by a script running somewhere else.

    Following the idea of virtual functions , if I do not make the function purely virtual, but have the base's function return from a private field of the base, is that a good solution ?
    Last edited by manasij7479; 03-08-2012 at 09:46 PM.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  4. #4
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Some proof of concept code; but I think I didn't get the OOP up to the mark.
    Code:
    #include<string>
    #include<vector>
    #include<stdexcept>
    #include<iostream>
    
    class SObject
    {
        const std::string className;
        const std::vector<std::string> mNames;
        std::vector<std::string> mValues;
    public:
        SObject(const std::string& cn, const std::vector<std::string>& mn, const std::vector<std::string>& mv)
            :className(cn), mNames(mn), mValues(mv)
            {};
        virtual std::string& memValue(const std::string& m)
        {
            //Do a better search later
            int i;
            for(i=0;i<mNames.size();++i)
                if(m==mNames[i])return mValues[i];
            throw(std::runtime_error("no_such_member"));
        };
        virtual std::string getClassName(){return className;};    
    };
    
    class derived : public SObject
    {
        static std::string className;
        static std::vector<std::string> mNames;
    public:
        derived(const std::vector<std::string>& mv)
            :SObject(className,mNames,mv){};
        std::string& operator[](const std::string& m){return memValue(m);}
        
    };
    std::string derived::className = "derived";
    std::vector<std::string> derived::mNames = {"mA","mB","mC"};
    
    int main()
    {
        derived foo({"Value_of_mA","b","c"});
        std::cout<<foo["mA"];
    }
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #5
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    map might be easier instead of the 2 vector approach.

    looks like you're trying for a reflection scheme of sorts, yes?

  6. #6
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by m37h0d View Post
    map might be easier instead of the 2 vector approach.
    If I implement binary search for this, it'll (afaik) be the same.
    I'll also probably replace vectors of strings by plain 2d arrays if I need to share the objects between the program and a vm running scripts.

    looks like you're trying for a reflection scheme of sorts, yes?
    You could say so.... but giving a lot less freedom. (like Qt does with QObject, but without mangling the code, and fitting the design I thought of)
    My main goal is to make some sort of 'generic' interface between a program and a script(as of now.. lisp).
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  7. #7
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    implementing your own binary search is certainly not equally easy. if you're doing it as an academic exercise, then more power to you, but it's generally better to use a prefabricated container that does what you're after.

  8. #8
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by m37h0d View Post
    implementing your own binary search is certainly not equally easy. if you're doing it as an academic exercise, then more power to you, but it's generally better to use a prefabricated container that does what you're after.
    Not academic, but trying to enjoy making wheels as a hobby.

    The problems I'll face with maps is that I can't be sure of their orientation in memory without writing a lot of template code.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  9. #9
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    i don't get it. do you want to marshal them as contiguous blocks or something?

  10. #10
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by m37h0d View Post
    i don't get it. do you want to marshal them as contiguous blocks or something?
    Yes, (after some compromises, limiting maximum size of a field.)
    That would take away the necessity of serializing and reconstructing objects every now and then and also a lot of stream parsing.

    As those 'ugly' changes will be internal to the base class, I left those out of the above code.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  11. #11
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    the need to restrict your member types to POD will likely prove too big a compromise to make this really useful.

    difficulties with stream parsing can be avoided if you put the onus on the objects to know the order their members appear in the stream. it's less work, and it simplifies the problem.

    look at boost's implementation. i used it as a design guide for something similar a few years back to great effect.

  12. #12
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by m37h0d View Post
    the need to restrict your member types to POD will likely prove too big a compromise to make this really useful.
    True, but unless I can magically instantiate templates at runtime, that seems to be the most efficient design to me.
    difficulties with stream parsing can be avoided if you put the onus on the objects to know the order their members appear in the stream. it's less work, and it simplifies the problem.
    I was aiming for more simple.
    If I have 64 bits space for each field, I can skip the stream operations totally and have random access I/O with pointer arithmetic(and some cheating by casting).
    Bigger Objects can request numerous such blocks. That would help storing arrays of PODs or serialized representations of objects in other data atructures.
    look at boost's implementation. i used it as a design guide for something similar a few years back to great effect.
    Thanks for that tip. I'll look into it and possibly replicate some of it.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  13. #13
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    you don't need to instantiate templates at runtime. the objects themselves know their layout.

    i'm just jotting this down in here, so this is likely not correct; it's just to illustrate the concept.

    consider:

    Code:
    class Serializable
    {
        virtual void serialize(Serializer& s)=0;
    }
    
    class Serializer
    {
    private:
        std::vector<char>buffer;
    public:
        template<typename T> void pack(const T& instance)
        {
            int oldSize = buffer.size();
            buffer.resize(buffer.length()+sizeof(T));
            char* start = &buffer[oldSize];
            memcpy(start,&instance,sizeof(T));
        }
    
        template<typename T> void pack(const std::vector<T>& v)
        {
             for(unsigned int i=0;i<v.size();++i)
             {
                  pack(v[i]);
             }
        }
    
        void pack(const std::string& s)
        {
        //you get the idea here
        }
    
        void pack(Serializable& instance)
        {
           s.serialize(*this);
        }
    };
    i actually got really clever and worked it out so the serialize function did both serialization and deserialization based on the const-ness of the objects; this was crucial, as it made it impossible to jumble the order of the objects in the memory stream. i'll leave that for you to work out if you decide you like this approach.
    Last edited by m37h0d; 03-09-2012 at 09:39 AM.

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,450
    This serializer is very broken in that it treats any type as just a sequence of bytes. Obviously this doesn't work too well with many containers.
    Actually, boost has a serializer library. Worth a look at.
    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.

  15. #15
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    don't be so quick to judge. you'll note there are overloads for containers. i didn't provide the overloads for every conceivable one, only 2 as examples just to illustrate the idea.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 10-17-2011, 11:22 PM
  2. Replies: 1
    Last Post: 04-29-2011, 08:41 PM
  3. static data members in structure
    By bhagwat_maimt in forum C++ Programming
    Replies: 9
    Last Post: 11-06-2006, 10:47 AM
  4. static data members
    By ichijoji in forum C++ Programming
    Replies: 2
    Last Post: 07-05-2006, 05:18 PM
  5. static and non-static data members
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 06-16-2002, 10:06 AM

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