About Store Forum Documentation Contact



Post Reply 
pointers, memc and inheritance
Author Message
Pherael Offline
Member

Post: #1
pointers, memc and inheritance
Sorry for this basic question, not really related with Esenthel Engine, but maybe somebody from users could help me. Also sorry for my english.

Code:
Memc<CharBase *> Charbases;

[code]
struct CharBase
{
virtual Byte getType(){return CHR_BASE;}
}
struct Tower : CharBase
{
virtual Byte getType(){return CHR_TOWER;}
}

Code:
void CreateTowers()
{
        FREP(22){
        Tower t;
    Charbases.New() = &t;
        }
}

Code:
void compressData(File &f)
{
    f.putInt(Charbases.elms());
    REPA(Charbases)
    {
        f.putByte(Charbases[i]->[b]getType()[/b]); <-- this line crash program
        Charbases[i]->compress(f);
    }
}

Charbase[i]->anyfunc() would crash program, and I don't really know why. This probably some basic stuff I just forgot (I used to programing some time ago, but I had a long break)

(This pop up in debug mode: Unhandled exception at 0x00d59a81 in Server.exe: 0xC0000005: Access violation reading location 0xffffffff)

So, I try something like this
Code:
Memc<CharBase > Charbases;

Code:
struct CharBase
{
virtual Byte getType(){return CHR_BASE;}
}
struct Tower : CharBase
{
virtual Byte getType(){return CHR_TOWER;}
}

Code:
void CreateTowers()
{
        FREP(22){
        Tower t;
    Charbases.New() = t;
        }
}

Code:
void compressData(File &f)
{
    f.putInt(Charbases.elms());
    REPA(Charbases)
    if(Tower *twr=CAST(Tower, &Charbases[i]))
            {
                f.putByte(twr->getType());
                twr->compress(f);    
            }
}

but this time CAST always returning NULL (all elements in Charbase are CharBase type).

I'm sure there is easy way to fix one of above, but i can't figure it by myself. Please, don't laugh at me...
09-24-2012 10:12 PM
Find all posts by this user Quote this message in a reply
miro@3dea Offline
Member

Post: #2
RE: pointers, memc and inheritance
hi, try removing ampersand when casting Charbases[i], your i-th element is pointer to Tower, not it's address.. so try:
Code:
Tower * twr = CAST(Tower, Charbases[i]);
if (twr) {...}

p.s. when you are inexperienced, try to avoid assignment in the if statement, for your own good wink
09-25-2012 07:56 AM
Find all posts by this user Quote this message in a reply
Post Reply