Syntax error in map instantiation.. probably something silly :)

This is a discussion on Syntax error in map instantiation.. probably something silly :) within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by Shamino If I use polymorphism here, it could be the end of my program if I try ...

  1. #16
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,161
    Quote Originally Posted by Shamino View Post
    If I use polymorphism here, it could be the end of my program if I try to use a sound file as a text file, know what I mean? If I use templates and return the actual specific data type, it prevents that problem, so stupid programmers don't try to add something to my system that will break it, not that they could know, and that is exactly the problem, they wouldn't know.
    But templates don't work at runtime. You can't use a template unless the compiler can determine AT COMPILE TIME what the type T is. You want something more dynamic than that. You need to use runtime polymorphism.

    I'm not sure why you were told that polymorphism isn't type-safe. It's one of the basic driving features of the C++ language. You would never "try to use a sound file as a text file" if you design your library correctly.

    Ogre3d does this and it is one of the main flaws of their engine. People try to program for ogre and end up breaking things because they accidentally grab a sound file and try to output it as a 3d model on the screen.
    It sounds like that program is just badly coded, then.

    It's really not a big big problem, but I'd rather do it this way, the type safe way.
    It can't be done this way because the compiler doesn't know what "T" is. You are asking "T" to stand for some type that can only be determined at runtime. A template just can't do that.

  2. #17
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Man that guy was really convincing that I could do it this way, and it would be better... Maybe he really knew less than I did....

    But I agree, my argument was the same.

    Why would I try to use a sound file as a text file ever?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  3. #18
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,161
    Quote Originally Posted by Shamino View Post
    Why would I try to use a sound file as a text file ever?
    Sounds like the code in question has a lot of pointers to the base class being passed around. Really, the only purpose of the base class is to let you stuff all these different types into the same map.

    The basic architectural issue is, how do you get stuff out of the map in a type-safe way? If you design it so that the caller asks for something by name, and a base class pointer is handed back, you have the problem of determining what the actual type of that object is. But by accessing the database a different way you can get around this problem.

    Instead of having the DB return a pointer, you call the lookup and pass it a pointer to an "acceptor" object which accepts the looked-up pointer via a function call. This leaves the decision about what type of object it is UP TO THE OBJECT ITSELF, because it will invoke the proper accept function on the acceptor object:

    Code:
    // abstract Acceptor base class -- classes interested in looking things up from a database
    // will derive from this.
    class Acceptor
    {
    public:
        virtual ~Acceptor() { }
        virtual bool acceptSound(DBSoundObject *ptr) = 0;
        virtual bool acceptText(DBTextObject *txt) = 0;
    };
    
    // Database object base class -- all objects stored in the database derive from this and
    // override the put() method to call the appropriate acceptor function.
    class DBObject
    {
        virtual ~DBObject { }
        virtual bool put(Acceptor *acc) { return false; }
    };
    
    class DBSoundObject : public DBObject
    {
    public:
        bool put(Acceptor *acc)
        {
            return acc->acceptSound(this);
        }
    };
    
    class DBTextObject : public DBObject
    {
    public:
        bool put(Acceptor *acc)
        {
            return acc->acceptText(this);
        }
    };
    Now, the things you store in the maps are shared pointers to DBObject objects. To do a lookup, you get the DBObject from the map, then you call its put() method and pass a pointer to the Acceptor object, i.e., the object which was trying to do the lookup.

    Code:
    DBObject *obj = some_map[some_string];
    if(obj)
    {
        // Get the result and pass it to this, which accepts it with one of the various acceptor
        // functions
        obj->put(this);
    }
    Or, if you know for sure that obj will never come back null, the more compact:

    Code:
    some_map[some_string]->put(this);
    It seems sort of roundabout, but you should be able to see that there is no way an object could ever be treated as the wrong type, because the object itself knows what its type is, and the object decides which accept() method to call on the Acceptor.

    So you derive from Acceptor and override the various accept() functions to accept the lookup-up objects and do whatever needs to be done with them.
    Last edited by brewbuck; 06-21-2007 at 11:45 AM.

  4. #19
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    That is some interesting C++ design, never seen it before, but it looks great, I'll consider this more when my AC is turned off, it's kinda freezing cold while im typing on the computer :\
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  5. #20
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,161
    Quote Originally Posted by Shamino View Post
    That is some interesting C++ design, never seen it before, but it looks great, I'll consider this more when my AC is turned off, it's kinda freezing cold while im typing on the computer :\
    I goofed when using some names in my example code... I fixed it and highlighted the changes in blue.

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

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  3. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 06:33 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Sreen Resolution Statistics?
    By Davros in forum A Brief History of Cprogramming.com
    Replies: 38
    Last Post: 04-26-2004, 02:33 PM

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