Problems with the use of const in derived class

I

InV4in

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.
 
J

Jonathan Lee

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
 
I

InV4in

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 :)
 
S

Saeed Amrollahi

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top