problems with inheritance in g++ compiler

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nomad5000
    New Member
    • Sep 2006
    • 22

    problems with inheritance in g++ compiler

    I'm trying to compile the following code but it won't work.

    the compiler always tells me:

    michael@michael :~/forum$ g++ *.cpp
    ClassB.cpp: In member function `virtual void ClassB::abstrac tMethod(int) const
    ':
    ClassB.cpp:6: error: passing `const ClassB' as `this' argument of `void
    ClassA::setAttr ibute(int)' discards qualifiers
    michael@michael :~/forum$

    I've tried to compile the same code with Borland C++ BuilderX and it works.

    this is the code I'm trying to compile:
    //file ClassA.h
    Code:
    #ifndef CLASSA_H
    #define CLASSA_H
    class ClassA{
    	private:
    		int attribute;
    	public:
    		ClassA(int attribute);
    		void setAttribute(int attribute);
    		virtual void abstractMethod(int attribute)const=0;
    };
    #endif
    //file ClassB.h
    Code:
    #ifndef CLASSB_H
    #define CLASSB_H
    #include "ClassA.h"
    class ClassB : public ClassA{
    	private:
    	public:
    		ClassB(int attribute);
    		void abstractMethod(int attribute) const;
    };
    #endif
    //file ClassA.cpp
    Code:
    #include "ClassA.h"
    
    ClassA::ClassA(int attribute){
    	this->attribute = attribute;
    }
    //file ClassB.cpp
    Code:
    #include "ClassB.h"
    
    ClassB::ClassB(int attribute):
    ClassA(attribute){}
    void ClassB::abstractMethod(int attribute) const{
    	setAttribute(attribute);
    }
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    It is because g++ is a more rigerous compiler than borland and has detected a fault that borland hasn't.

    The problem is that

    void abstractMethod( int attribute) const;

    is declared const this means that it is saying that it will not be changing any class member variables. This is important because it means you can call the function on a const object.

    However this function calls

    void setAttribute(in t attribute);

    which is not declared const and in fact does change the class member variable attribute.

    In calling setAttribute from abstractMethod you are loosing the const qualifer on the call.

    Fix all these errors byt changing

    void abstractMethod( int attribute) const;

    to

    void abstractMethod( int attribute);

    in all cases.

    Comment

    • nomad5000
      New Member
      • Sep 2006
      • 22

      #3
      I get the idea now, a virtual method in c++ is not the same as an abstract method in java. But isn't there a way to declare a class abstract, that is that there can be no instances of this class?

      Comment

      • tyreld
        New Member
        • Sep 2006
        • 144

        #4
        Originally posted by nomad5000
        I get the idea now, a virtual method in c++ is not the same as an abstract method in java. But isn't there a way to declare a class abstract, that is that there can be no instances of this class?
        The existance of a pure virtual member function (ie. a virtual function followed by the pure specifier "= 0") implies that the class as a whole is abstract. In your example code ClassA is abstract, and the compiler will give an error if you try and instantiate an instance of ClassA.

        Comment

        • nomad5000
          New Member
          • Sep 2006
          • 22

          #5
          thank you both very much. You where of great help.

          Comment

          Working...