Problems with the use of const in derived class

Discussion in 'C++' started by InV4in, Jun 11, 2010.

  1. InV4in

    InV4in Guest

    Hello,

    I have an abstract base class looking something like this:

    class AlgebraicValue
    {
    public:
    const char deftype;

    AlgebraicValue( void )
    : deftype( 0 ) { }
    virtual AlgebraicValue* foo( AlgebraicValue const& ) = 0;
    }

    and a derived class Fraction looking like:

    class Fraction : AlgebraicValue
    {
    public:
    Fraction( int, int );

    Fraction* foo( AlgebraicValue const& );
    }

    with a constructor

    Fraction::Fraction( int a = 0, int b = 1 )
    : deftype( 1 )
    {
    // bla bla
    }

    But g++ -Wall -pedantic says: "class 'Fraction' does not have any
    field named 'deftype'" so I tried writing AlgebraicValue::deftype in
    the constructor of Fraction, but then it said "class 'AlgebraicValue'
    does not have any field named 'deftype'". After that I was totally
    confused.

    I would really appreciate anyone helping me to resolve this problem.

    Best regards.
    InV4in, Jun 11, 2010
    #1
    1. Advertising

  2. InV4in

    Jonathan Lee Guest

    On Jun 11, 7:30 am, InV4in <> wrote:
    > But g++ -Wall -pedantic says: "class 'Fraction' does not have any
    > field named 'deftype'" so I tried writing AlgebraicValue::deftype in
    > the constructor of Fraction, but then it said "class 'AlgebraicValue'
    > does not have any field named 'deftype'". After that I was totally
    > confused.
    >
    > I would really appreciate anyone helping me to resolve this problem.
    >
    > Best regards.


    Add a constructor for AlgebraicValue that accepts a value for
    deftype. Then use that variation of the constructor in the initializer
    list of the derived type, Fraction. Like:

    AlgebraicValue(char c = '\0'):deftype(c) { ... }

    Fraction(int a = 0, int b = 1):AlgebraicValue(1), numerator(a),
    denominator(b) { ... }

    --Jonathan
    Jonathan Lee, Jun 11, 2010
    #2
    1. Advertising

  3. InV4in

    InV4in Guest

    On 11 Jun., 13:50, Jonathan Lee <> wrote:

    > Add a constructor for AlgebraicValue that accepts a value for
    > deftype. Then use that variation of the constructor in the initializer
    > list of the derived type, Fraction. Like:
    >
    > AlgebraicValue(char c = '\0'):deftype(c) { ... }
    >
    > Fraction(int a = 0, int b = 1):AlgebraicValue(1), numerator(a),
    >   denominator(b) { ... }
    >
    > --Jonathan


    Thank you. Works perfectly :)
    InV4in, Jun 11, 2010
    #3
  4. On Jun 11, 2:30 pm, InV4in <> wrote:
    > Hello,
    >
    > I have an abstract base class looking something like this:
    >
    > class AlgebraicValue
    > {
    >   public:
    >     const char deftype;
    >
    >     AlgebraicValue( void )
    >     : deftype( 0 ) { }
    >     virtual AlgebraicValue* foo( AlgebraicValue const& ) = 0;
    >
    > }
    >
    > and a derived class Fraction looking like:
    >
    > class Fraction : AlgebraicValue
    > {
    >   public:
    >     Fraction( int, int );
    >
    >     Fraction* foo( AlgebraicValue const& );
    >
    > }
    >
    > with a constructor
    >
    > Fraction::Fraction( int a = 0, int b = 1 )
    >               : deftype( 1 )
    > {
    >   // bla bla
    >
    > }
    >
    > But g++ -Wall -pedantic says: "class 'Fraction' does not have any
    > field named 'deftype'" so I tried writing AlgebraicValue::deftype in
    > the constructor of Fraction, but then it said "class 'AlgebraicValue'
    > does not have any field named 'deftype'". After that I was totally
    > confused.
    >
    > I would really appreciate anyone helping me to resolve this problem.
    >
    > Best regards.


    Hi

    You can't initialize base class data members in derived class's
    constructor.
    Please see the C++ Programming Language by Bjarne Stroustrup pages
    306-307.

    On the other hand, Abstract Classes' constructor are seldom useful.
    because you can't create object of abstract class AlgebricValue.
    I guess the deftype is a numeric (or enum ?) for the default type of
    classes
    like Fraction, Complex or other derived classes. You can use the
    deftype
    in derived ones:
    class AlgebricValue {
    virtual AlgebraicValue* foo( AlgebraicValue const& ) = 0;
    };

    class Fraction :public AlgebricValue {
    const int deftype;
    public:
    Fraction(int n, int d) : deftype(1) /* ... */ {}
    // ...
    };

    class Complex :public AlgebricValue {
    const int deftype;
    public:
    Complex(int r, int i) : deftype(2) /* ... */ {}
    // ...
    };

    Regards,
    -- Saeed Amrollahi
    Saeed Amrollahi, Jun 12, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    389
    Alf P. Steinbach
    May 23, 2007
  2. Replies:
    1
    Views:
    383
    myork
    May 23, 2007
  3. Replies:
    1
    Views:
    371
    Victor Bazarov
    May 23, 2007
  4. Javier
    Replies:
    2
    Views:
    542
    James Kanze
    Sep 4, 2007
  5. David
    Replies:
    3
    Views:
    386
    Grizlyk
    Jan 29, 2008
Loading...

Share This Page