Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. segmentation fault issue in derived class assigment [modified]

segmentation fault issue in derived class assigment [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hawk23reddy
    wrote on last edited by
    #1

    hi this code causes segmentation fault can u pleases y.

    class ab
    {
    public:
    virtual void abb()=0;
    };
    class bc : public ab
    {
    public:
    void abb()
    {
    cout<<"here";
    }
    };
    void get(ab* c)
    {
    bc *cc = new bc();
    c = cc;//i know this is culprit
    }
    int main()
    {
    ab *b = NULL;
    get(b);
    b->abb();
    }

    but how can i solve this so that i need to fill derived class object (reference or pointer) to base class in get function only.. [my request]:confused: if u feel iam very poor in basics for give me plz by only this i can learn more and attain like u [/my request] :((

    modified on Tuesday, May 19, 2009 2:03 AM

    T T 2 Replies Last reply
    0
    • H hawk23reddy

      hi this code causes segmentation fault can u pleases y.

      class ab
      {
      public:
      virtual void abb()=0;
      };
      class bc : public ab
      {
      public:
      void abb()
      {
      cout<<"here";
      }
      };
      void get(ab* c)
      {
      bc *cc = new bc();
      c = cc;//i know this is culprit
      }
      int main()
      {
      ab *b = NULL;
      get(b);
      b->abb();
      }

      but how can i solve this so that i need to fill derived class object (reference or pointer) to base class in get function only.. [my request]:confused: if u feel iam very poor in basics for give me plz by only this i can learn more and attain like u [/my request] :((

      modified on Tuesday, May 19, 2009 2:03 AM

      T Offline
      T Offline
      Taran9
      wrote on last edited by
      #2

      First of all, you have forgotten to derive bc from ab here.

      hawk23reddy wrote:

      class bc

      Secondly, change the signature of get from

      hawk23reddy wrote:

      void get(ab* c)

      to

      hawk23reddy wrote:

      void get(ab** c)

      and while calling get, pass address of b as argument.

      1 Reply Last reply
      0
      • H hawk23reddy

        hi this code causes segmentation fault can u pleases y.

        class ab
        {
        public:
        virtual void abb()=0;
        };
        class bc : public ab
        {
        public:
        void abb()
        {
        cout<<"here";
        }
        };
        void get(ab* c)
        {
        bc *cc = new bc();
        c = cc;//i know this is culprit
        }
        int main()
        {
        ab *b = NULL;
        get(b);
        b->abb();
        }

        but how can i solve this so that i need to fill derived class object (reference or pointer) to base class in get function only.. [my request]:confused: if u feel iam very poor in basics for give me plz by only this i can learn more and attain like u [/my request] :((

        modified on Tuesday, May 19, 2009 2:03 AM

        T Offline
        T Offline
        tolw
        wrote on last edited by
        #3

        Try this:

        ...
        void get(ab* **&**c)
        ...

        Research passing arguments to functions and especially passing arguments by reference.

        H 1 Reply Last reply
        0
        • T tolw

          Try this:

          ...
          void get(ab* **&**c)
          ...

          Research passing arguments to functions and especially passing arguments by reference.

          H Offline
          H Offline
          hawk23reddy
          wrote on last edited by
          #4

          thank u. i got it..

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups