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 II

Inheritance II

Scheduled Pinned Locked Moved C / C++ / MFC
c++oophelptutorialquestion
4 Posts 4 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.
  • 0 Offline
    0 Offline
    0v3rloader
    wrote on last edited by
    #1

    Hello, I have been getting these annoying error and warning messages from the compiler that I really don't understand. Consider the following code example:

    class objA
    {
    public:
    objA(){};
    ~objA(){};

    public:
    virtual bool Setup(int){};
    bool Setup(float){};
    };

    class objB : public objA
    {
    public:
    objB(){};
    ~objB(){};

    public:
    virtual bool Setup(int){};
    };

    class C
    {
    public:
    C(){};
    ~C(){};

    public:
    bool doTest()
    {
    objB b;

    	b.Setup(100);	
    	b.Setup(0.0001); // warning C4244: 'argument' : 
    			 // conversion from 'const double' to 'int', possible loss of data
    };
    

    };

    So here we get a warning message from the compiler as it doesn't know what function to use -- it is as if it doesn't recognise the base-class' method. I know that somehow this has to do with the virtual keyword in use, but I really don't know why this is happening. Now consider yet another code example:

    class _objA
    {
    public:
    _objA(){};
    ~_objA(){};

    public:
    bool Setup(int){};
    bool Setup(float){};
    };

    class _objB : public _objA
    {
    public:
    _objB(){};
    ~_objB(){};
    };

    class _C
    {
    public:
    _C(){};
    ~_C(){};

    public:
    bool doTest()
    {
    _objB _b;

    	\_b.Setup(100);	
    	\_b.Setup(0.0001);	// no warnings whatsoever! the compiler correctly casts!
    };
    

    };

    Here we have a class being derived from another one but without any virtual methods -- no errors/warnings! Can someone please explain me what are the compiler/C++/ANSI rules I am missing here? Thank you very much, David

    A M 2 Replies Last reply
    0
    • 0 0v3rloader

      Hello, I have been getting these annoying error and warning messages from the compiler that I really don't understand. Consider the following code example:

      class objA
      {
      public:
      objA(){};
      ~objA(){};

      public:
      virtual bool Setup(int){};
      bool Setup(float){};
      };

      class objB : public objA
      {
      public:
      objB(){};
      ~objB(){};

      public:
      virtual bool Setup(int){};
      };

      class C
      {
      public:
      C(){};
      ~C(){};

      public:
      bool doTest()
      {
      objB b;

      	b.Setup(100);	
      	b.Setup(0.0001); // warning C4244: 'argument' : 
      			 // conversion from 'const double' to 'int', possible loss of data
      };
      

      };

      So here we get a warning message from the compiler as it doesn't know what function to use -- it is as if it doesn't recognise the base-class' method. I know that somehow this has to do with the virtual keyword in use, but I really don't know why this is happening. Now consider yet another code example:

      class _objA
      {
      public:
      _objA(){};
      ~_objA(){};

      public:
      bool Setup(int){};
      bool Setup(float){};
      };

      class _objB : public _objA
      {
      public:
      _objB(){};
      ~_objB(){};
      };

      class _C
      {
      public:
      _C(){};
      ~_C(){};

      public:
      bool doTest()
      {
      _objB _b;

      	\_b.Setup(100);	
      	\_b.Setup(0.0001);	// no warnings whatsoever! the compiler correctly casts!
      };
      

      };

      Here we have a class being derived from another one but without any virtual methods -- no errors/warnings! Can someone please explain me what are the compiler/C++/ANSI rules I am missing here? Thank you very much, David

      A Offline
      A Offline
      act_x
      wrote on last edited by
      #2

      u assume that the call to Setup(float) will be called . Well the compiler is seeing a virtual Setup(int) as the only function . I am not aware of considerations where if u have one function as virtual other overloaded methods are not considered in overload resolution . This might be what is happenning . Need to dig into stroustrup and find .

      1 Reply Last reply
      0
      • 0 0v3rloader

        Hello, I have been getting these annoying error and warning messages from the compiler that I really don't understand. Consider the following code example:

        class objA
        {
        public:
        objA(){};
        ~objA(){};

        public:
        virtual bool Setup(int){};
        bool Setup(float){};
        };

        class objB : public objA
        {
        public:
        objB(){};
        ~objB(){};

        public:
        virtual bool Setup(int){};
        };

        class C
        {
        public:
        C(){};
        ~C(){};

        public:
        bool doTest()
        {
        objB b;

        	b.Setup(100);	
        	b.Setup(0.0001); // warning C4244: 'argument' : 
        			 // conversion from 'const double' to 'int', possible loss of data
        };
        

        };

        So here we get a warning message from the compiler as it doesn't know what function to use -- it is as if it doesn't recognise the base-class' method. I know that somehow this has to do with the virtual keyword in use, but I really don't know why this is happening. Now consider yet another code example:

        class _objA
        {
        public:
        _objA(){};
        ~_objA(){};

        public:
        bool Setup(int){};
        bool Setup(float){};
        };

        class _objB : public _objA
        {
        public:
        _objB(){};
        ~_objB(){};
        };

        class _C
        {
        public:
        _C(){};
        ~_C(){};

        public:
        bool doTest()
        {
        _objB _b;

        	\_b.Setup(100);	
        	\_b.Setup(0.0001);	// no warnings whatsoever! the compiler correctly casts!
        };
        

        };

        Here we have a class being derived from another one but without any virtual methods -- no errors/warnings! Can someone please explain me what are the compiler/C++/ANSI rules I am missing here? Thank you very much, David

        M Offline
        M Offline
        Michael Dunn
        wrote on last edited by
        #3

        No, virtual has nothing to do with it since you're not calling any methods through a pointer. In your first example, B::Setup() hides all methods in A named Setup(). At the point of call, B::Setup(int) is the only method named "Setup" that is visible. The reason you get no warning in the second example is because you removed B::Setup(), so both A::Setup() overloads are visible. --Mike-- LINKS~! Ericahist | 1ClickPicGrabber | CP SearchBar v2.0.2 | C++ Forum FAQ | You Are Dumb

        J 1 Reply Last reply
        0
        • M Michael Dunn

          No, virtual has nothing to do with it since you're not calling any methods through a pointer. In your first example, B::Setup() hides all methods in A named Setup(). At the point of call, B::Setup(int) is the only method named "Setup" that is visible. The reason you get no warning in the second example is because you removed B::Setup(), so both A::Setup() overloads are visible. --Mike-- LINKS~! Ericahist | 1ClickPicGrabber | CP SearchBar v2.0.2 | C++ Forum FAQ | You Are Dumb

          J Offline
          J Offline
          John R Shaw
          wrote on last edited by
          #4

          I think you are right! Your convoluted answer explaned what I dislike aboute C++. Do not get me wrong! C++ is great, it simplifies so many things. But you need to know at leased 10 times as much (than C) in order to acomplish those goals. INTP

          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