Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Access violation in simple polymorphism?

polymorphism

  • Please log in to reply
4 replies to this topic

#1 alirezan

alirezan

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 74 posts

Posted 13 February 2012 - 01:14 PM

Hello

I am trying to do a simple thing. I have a base class and a derived class. Both classes have a virtual function in them called "Dispatch". The definition of the function should be in the derived class only so that when i call the function, the method in the derived class is called only.
Here is the code I have:

base.h:

class base
{
public:
base(void);
virtual int dispatch (int eventid, int stateid);
virtual ~base(void);
};


base.cpp:


#include <stdio.h>
#include <iostream>
#include "base.h"

base::base(void){}

base::~base(void){}
void main (void)
{
base* bp;
bp->dispatch (1,1);
getchar();
}


derived.h


#include "base.h"

class derived : public base
{
public:
derived(void);
virtual int dispatch (int eventid, int stateid);
virtual ~derived(void);
};

derived.cpp


#include "derived.h"
#include <stdio.h>
#include <iostream>

derived::derived(void){}
int derived::dispatch (int eventid, int stateid)
{
printf ("Dispatch, %d, %d" ,eventid, stateid);

return 0;
}
derived::~derived(void){}



Right now it won't compile complaining about the definition of "base::dispatch(int,int)".
If I make the definition of dispatch in base class abstract (=0), then it compiles no problem but it won't let me instanciate the class with new or even like this: "base b;"
if i put the =0 and compile it works fine but since "b" is not initialized it will tell me there is access violation which i understand why.

Can someone help me figure out what I need to do please?

Thanks
  • 0

#2 irancplusplus

irancplusplus

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 68 posts
  • Programming Language:C++, C#

Posted 13 February 2012 - 11:48 PM

when you are doing tests why do you write your code in multiple files?!

why don't you use empty definition in base:
virtual int dispatch (int eventid, int stateid){}

with
base* bp = new base;    
bp->dispatch (1,1);
the function in base is called. but with

base* bp = new derived;    
bp->dispatch (1,1);
I think there's no problem even if you make base abstract.
  • 0

I wrote t.me/cplusplus/203


#3 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 14 February 2012 - 07:47 AM

Using multiple files like that is a good programming practice. It has compiler/linker advantages, as well as being how large projects are structured.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#4 irancplusplus

irancplusplus

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 68 posts
  • Programming Language:C++, C#

Posted 14 February 2012 - 12:36 PM

yes.
but if I want to help him I have to create a multiple file project copy the code of every file from forum to appropriate file!!
so I tried to answer without testing the code.
And he is now testing polymorphism, multiple files problems can be solved then! :rolleyes:
  • 0

I wrote t.me/cplusplus/203


#5 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 14 February 2012 - 03:01 PM

yes.
but if I want to help him I have to create a multiple file project copy the code of every file from forum to appropriate file!!
so I tried to answer without testing the code.


Using multiple files shouldn't be an encumbrance to the debugging process. If you're using a decent IDE, importing multiple files should be super easy.

If I make the definition of dispatch in base class abstract (=0), then it compiles no problem but it won't let me instanciate the class with new or even like this: "base b;"
if i put the =0 and compile it works fine but since "b" is not initialized it will tell me there is access violation which i understand why.


What was your code when you tried to define a body for dispatch in the base class? Declaring both as virtual should compile and run just fine.
  • 0

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid






Also tagged with one or more of these keywords: polymorphism

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download