Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The problem I am having is I have a SDK with a few classes that I would like to use which contain pure virtual functions which I want to use. How do I go about doing this? Can't seem to find anything on utilizing the functions from the base class. If I knew what code I was using from the SDK, I'd just override the virtual functions, but I can't do that.

I made a test program to try and show you what I really want to do

class A {
  public:
  virtual int f() = 0 ;
};
class B : public A {
  public:
  B() ;
  int f() ;
};
//would like to use this
int A::f() {
     int x = 1 ;
     x = 1 ;
     return x ;
}
B::B() {}
int B::f() {
    int x = 1 ;
    x = 2 ;
    return x ;
}
int main() {
A* aPtr ;
B* bPtr = new B();
int retVal = 0 ;
retVal = bPtr->f() ; //uses B::f() which is not what I want to use
retVal = aPtr->f() ; //access violation (since f() = 0 right?)
return 0 ;
}
Posted

MIDL
retVal = bPtr->f() ; //uses B::f() which is not what I want to use
retVal = aPtr->f() ; //access violation (since f() = 0 right?)


Access violation is generated because aPtr is not pointing any valid object.

The main point is that you cant create an instance of a class that contain a pure virtual function.

Thanks
 
Share this answer
 
Any class that contains a pure virtual function is an abstract class and cannot be instantiated. It is only used as a base class that can be built upon. Any class derived from an abstract will have to implement the pure virtuals or it becomes an abstract class also. In your class A the function f() contains no implementation so any derived class must implement that function using whatever code is appropriate. If you want two versions of f(), one in A and one in B then don't make it pure virtual in A.
 
Share this answer
 
The problem is that I don't know how A::f() is even implemented (in my test program I do, but in the SDK I don't.).

Since A::f() is pure virtual, is there no way to use it? I'm assuming that I cannot modify class A in any way.



By definition, pure virtual functions don't exist. Theres no code there. Its blank. And such a class can never be instantiated. None of the existing compilers allow that. You got to roll out your own if you need to. Such classes as 'A' are supposed to be just a layout for anything that wants to act as 'A', remember?

Imagine this. Theres a wall on a high raise room with a door called exit. Theres nothing on the other side. If you attempt to use it any way then you'll be a splat. You are supposed to provide a means of exit; be it a fire ladder, a staircase, a lift, a rocket - anything that suits your purpose.

So, don't worry about not knowing if and how that function is implemented 'cause its not. NOT. Theres nothing, null, cipher, sonne, duck, love...
 
Share this answer
 
Comments
Aescleal 12-Aug-10 16:09pm    
If pure virtual functions don't exist "by definition" then how come the code the questioner posted compiles? And if he'd used:

bPtr->A::f();

it would have run and done what he'd wanted?
[no name] 13-Aug-10 2:47am    
I said "by definition" because the fact that they can have body is always put after a 'however'.

So it seems like the OP's SDK's pure virtual function has a body but I don't know if that call would have done what (s)he wanted ...'cause....even the (s)he doesn't know if it will :P but would have run ;)
IF you know the pure virtual function has a definition then the only way you're going to call it is to do so explicitly. In the example you gave you can use an object of class B:

int main()
{
    B b;
    b.A::f();
}

Of course if the library hasn't implemented the pure virtual function then you'll get some sort of warning like "Pure virtual function called" and your code will crash in a heap.

If there aren't any derived classes of A that you can explicitly call through you'll have to derive your own class and override the virtual function:

class C : public A
{
    public:
        virtual void f()
        {
            A::f();
        }
};

int main()
{
    C c;
    c.f();
}


Again, if A::f() hasn't an implementation then your code is on the way to crash city.

Normally when you declare a function as pure virtual you're not going to implement it as pure virtual functions specify an interface. Having said that there are two main reasons why you'd implement a pure virtual function:

- destructors need a body, even if pure virtual

- to provide default behaviour for a virtual function but make the deriver explicitly say they want the default behaviour.

Cheers,

Ash
 
Share this answer
 
v2
thanks folks.

Just for future reference, if I made a class that had pure virtual functions in it, I would do this to force the user implement a function with the same name/type as the pure virtual functions that I created? Maybe this is used for other non virtual functions in my class and such? There's probably a whole host of reasons to do this I guess.
 
Share this answer
 
Comments
Emilio Garavaglia 12-Aug-10 17:16pm    
Don't put question as "answer". If you want to discuss use comments or use the proper forum.
retVal = aPtr->f() ; //access violation (since f() = 0 right?)

No. "An access violation" in your example occurs since aPtr is not pointing to a valid object. Calling aPtr->f() in a context in which only a polymorphic type A is known is the basic virtue of polymorphism. Your SDK is dealing with object (better "interfaces") which "behave as A" no matter of which late type (in your case B) they are.
 
Share this answer
 
To access A's f() you need to do this: aPtr->A::f()

Since this is virtual function if you just call aPtr->f() it'll always resolve to the most derived class version of f().

http://simplestcodings.blogspot.com

Edited by Ash to clarify an ambiguous statement of what's called when.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900