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. Inheritance issue [modified]

Inheritance issue [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
helpoopquestion
10 Posts 5 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.
  • B Offline
    B Offline
    big_denny_200
    wrote on last edited by
    #1

    hello :) I've found my silly mistake please help me find error in my code :

    // *************
    class Base
    {
    public :

    	virtual ~Base() {};
    
    	// Retrieve value of X
    	virtual void getX() const
    	{
    		cout<// should be &pError here
    {
    	pError->getX(); **// and .getX() here** 
    }
    
    return 0;
    

    }

    sorry for stupid question I am pretty tired, need a good sleep :) -- modified at 9:42 Monday 29th May, 2006

    T S S 3 Replies Last reply
    0
    • B big_denny_200

      hello :) I've found my silly mistake please help me find error in my code :

      // *************
      class Base
      {
      public :

      	virtual ~Base() {};
      
      	// Retrieve value of X
      	virtual void getX() const
      	{
      		cout<// should be &pError here
      {
      	pError->getX(); **// and .getX() here** 
      }
      
      return 0;
      

      }

      sorry for stupid question I am pretty tired, need a good sleep :) -- modified at 9:42 Monday 29th May, 2006

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

      big_denny_200 wrote:

      virtual void getX() const { cout<;

      don't close your functions definitions with semicolons (';'). at last, just curious... don't you understand what the compiler says when complaining ?! ;P i have no compiler to test here, so, sorry.


      TOXCCT >>> GEII power

      [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ] -- modified at 9:45 Monday 29th May, 2006

      B 1 Reply Last reply
      0
      • T toxcct

        big_denny_200 wrote:

        virtual void getX() const { cout<;

        don't close your functions definitions with semicolons (';'). at last, just curious... don't you understand what the compiler says when complaining ?! ;P i have no compiler to test here, so, sorry.


        TOXCCT >>> GEII power

        [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ] -- modified at 9:45 Monday 29th May, 2006

        B Offline
        B Offline
        big_denny_200
        wrote on last edited by
        #3

        no :( why not use semicolons ? thanks -- modified at 9:34 Monday 29th May, 2006

        T 1 Reply Last reply
        0
        • B big_denny_200

          no :( why not use semicolons ? thanks -- modified at 9:34 Monday 29th May, 2006

          T Offline
          T Offline
          toxcct
          wrote on last edited by
          #4

          big_denny_200 wrote:

          why not use semicolons ?

          and you didn't answer my questions more. * What are the compiler errors ?


          TOXCCT >>> GEII power

          [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ] -- modified at 9:45 Monday 29th May, 2006

          C S 2 Replies Last reply
          0
          • B big_denny_200

            hello :) I've found my silly mistake please help me find error in my code :

            // *************
            class Base
            {
            public :

            	virtual ~Base() {};
            
            	// Retrieve value of X
            	virtual void getX() const
            	{
            		cout<// should be &pError here
            {
            	pError->getX(); **// and .getX() here** 
            }
            
            return 0;
            

            }

            sorry for stupid question I am pretty tired, need a good sleep :) -- modified at 9:42 Monday 29th May, 2006

            S Offline
            S Offline
            Sarath C
            wrote on last edited by
            #5

            it is not the problem with a ";" problem is with ur try catch block;

            big_denny_200 wrote:

            catch (Base *pError)

            u r catching a pointer to the throwing object but inside the try block u r doing

            big_denny_200 wrote:

            throw Derived1(); // when I add this line I get assert

            so inside the throw block do as follows Derived1 d; // when I add this line I get assert throw &d; or throw &Derived1(); // when I add this line I get assert u can also resolve if u change the catch block to pass by value instead pass by reference SaRath
            "D on't blindly follow rules you read somewhere without verifying that it makes sense for your situation!"

            S 1 Reply Last reply
            0
            • T toxcct

              big_denny_200 wrote:

              why not use semicolons ?

              and you didn't answer my questions more. * What are the compiler errors ?


              TOXCCT >>> GEII power

              [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ] -- modified at 9:45 Monday 29th May, 2006

              C Offline
              C Offline
              Cedric Moonen
              wrote on last edited by
              #6

              toxcct wrote:

              because the C++ doesn't want them here

              In fact, this won't change anything. You can let them there. They will simply be ignored.


              Cédric Moonen Software developer
              Charting control

              S 1 Reply Last reply
              0
              • T toxcct

                big_denny_200 wrote:

                why not use semicolons ?

                and you didn't answer my questions more. * What are the compiler errors ?


                TOXCCT >>> GEII power

                [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ] -- modified at 9:45 Monday 29th May, 2006

                S Offline
                S Offline
                Sarath C
                wrote on last edited by
                #7

                a function ending with ";" is simply compiling with visual studio .net and visual studio 6 C++ compiler. SaRath
                "D on't blindly follow rules you read somewhere without verifying that it makes sense for your situation!"

                1 Reply Last reply
                0
                • C Cedric Moonen

                  toxcct wrote:

                  because the C++ doesn't want them here

                  In fact, this won't change anything. You can let them there. They will simply be ignored.


                  Cédric Moonen Software developer
                  Charting control

                  S Offline
                  S Offline
                  Sarath C
                  wrote on last edited by
                  #8

                  Cedric have u seen my post? I think that is the reason for his assertion? SaRath
                  "D on't blindly follow rules you read somewhere without verifying that it makes sense for your situation!"

                  1 Reply Last reply
                  0
                  • S Sarath C

                    it is not the problem with a ";" problem is with ur try catch block;

                    big_denny_200 wrote:

                    catch (Base *pError)

                    u r catching a pointer to the throwing object but inside the try block u r doing

                    big_denny_200 wrote:

                    throw Derived1(); // when I add this line I get assert

                    so inside the throw block do as follows Derived1 d; // when I add this line I get assert throw &d; or throw &Derived1(); // when I add this line I get assert u can also resolve if u change the catch block to pass by value instead pass by reference SaRath
                    "D on't blindly follow rules you read somewhere without verifying that it makes sense for your situation!"

                    S Offline
                    S Offline
                    Stephen Hewitt
                    wrote on last edited by
                    #9

                    SaRath C wrote:

                    Derived1 d; // when I add this line I get assert throw &d;

                    Never do this as it’s seriously flawed. The object d is on the stack but the stack is unwound when an exception is caught. The upshot is that when you catch an exception with such code the exception handler gets a pointer to an object which no longer exists. It's the same situation as returning a reference or pointer to a local variable from a function. Steve

                    1 Reply Last reply
                    0
                    • B big_denny_200

                      hello :) I've found my silly mistake please help me find error in my code :

                      // *************
                      class Base
                      {
                      public :

                      	virtual ~Base() {};
                      
                      	// Retrieve value of X
                      	virtual void getX() const
                      	{
                      		cout<// should be &pError here
                      {
                      	pError->getX(); **// and .getX() here** 
                      }
                      
                      return 0;
                      

                      }

                      sorry for stupid question I am pretty tired, need a good sleep :) -- modified at 9:42 Monday 29th May, 2006

                      S Offline
                      S Offline
                      Stephen Hewitt
                      wrote on last edited by
                      #10

                      You're throwing a Derived1 but catching a Base*. If you want to catch a Base* you need code like this: try {     throw new Derived1(); } catch (Base *pError) {     pError->getX();     delete pError; } A better was is not to throw a pointer. Use code like this: try {     throw Derived1(); } catch (const Base &e) {     e.getX(); } Steve

                      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