Thread: How do I inherit static data members ?

  1. #1
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657

    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 ?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Override a virtual function to return the desired string for each derived class.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    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 10:46 PM.

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    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"];
    }

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

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

  6. #6
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    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).

  7. #7
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    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
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    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.

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

  10. #10
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    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.

  11. #11
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    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
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    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.

  13. #13
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    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 10:39 AM.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    838
    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.

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, 11: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