| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | virtual functions |
Hello,
I'm having a problem with virtual functions in a large program. I'm
basically assigning a pointer to a base class the address of a derived
class that overrides a virtual function in the base class, and then
calling the overridden function through the pointer. The problem is that
unless I declare the base classes' function nonvirtual, the program
crashes with an access violation at 00000000. Of course, when it's
nonvirtual, the wrong function executes.
I've been able to get the following (very small) program to run with
no problems, proving that this is allowed in a C++ program, but the
larger one (50+ modules, 900K+ EXE size), crashes. The following
program illustrates my problem:
class A {
public:
int data;
// virtual function
virtual void assign(int x) { }
};
class B : public A {
public:
void assign(int x) { data = x; }
};
void main() {
B b; // object of type "B"
A *pA = (A *)&b; // pointer to a "B" object...
// this is where i get the access violation.
pA->assign(5);
}
This works fine, and is exactly the same kind of stunt I'm pulling in
my large program, except that the large program crashes. I was wondering
if there are any compiler switches that control this sort of thing, or
whether it's the fact that I'm using the Borland compiler, which is
getting all sorts of bad press for being a piece of crap. If anyone can
suggest why this might not work, I'd be real interested in hearing it.
Thanks,
Kelly
___
X KWQ/2 1.2g NR X This tagline stolen by KWQ Mail/2.
--- Maximus/2 2.02
* Origin: OS/2 Shareware BBS, Fairfax, VA: 703-385-4325 (1:109/347)SEEN-BY: 12/2442 620/243 632/348 640/820 690/660 711/409 410 413 430 807 808 SEEN-BY: 711/809 934 942 949 712/353 515 713/888 800/1 @PATH: 109/347 2 7 3615/50 229/2 12/2442 711/409 808 809 934 |
|
| SOURCE: echomail via fidonet.ozzmosis.com | |
Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.