Saturday, March 20, 2010

Virtual Muddles My Mind

Alright, so small dilemma... I have to declare an instance of another class (BField) in my class (BForm)'s constructor, this would all be well and fine if it weren't for virtual. In BField's constructor (it has no empty constructor mind you), there is something that references (void* data = (void*) 0) to be passed in. This is reference to a virtual member of BField's class (virtual void* data()). So, when I try to add something in to that position from BForm with my BField member, I get error C2259: 'BField' : cannot instantiate abstract class. Or rather, I assume this is what causes it.
So instead of this:
BField* _fld[MAX_NO_FIELDS];

_fld[_curidx] = new BField(row, col, "problem here-Virtual Element", framed);

I have to figure out how to override the virtual element of the BField constructor. I'll come back to this when I figure it out. Go Team!

2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Hi Carolyn, BField class really muddles people, including me. I struggled so long but I finally know what we are supposed to do. The parameter, (void* data = (void*) 0), received by BField's constructor represents an address of a certain string to be edit in the future, rather than a virtual member of BField's class. We can simply put 0(zero) there, or declare a string and then put the address of the string there. The reason why you got error C2259 is BField contains pure virtual funtion declarations, and there are three of them:
    virtual int edit() = 0;
    virtual bool editable() = 0;
    virtual BField& set(const void* data) = 0;
    If you want to create an object of BField, you can comment these pure virtual function declarations in the header file.

    However, I have an problem with this function declaration in BField header file: virtual bool editable() = 0;
    It doesn't correspond with those declarations in the child classes which are BEdit and BLable. You can notice that there is an additional key world 'constant' there. I spent nearly 2 hours to figure out this problem. I've sent him an email about this but he hasn't reply yet.

    ReplyDelete