Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Problems with the use of const in derived class

Reply
Thread Tools

Problems with the use of const in derived class

 
 
InV4in
Guest
Posts: n/a
 
      06-11-2010
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.
 
Reply With Quote
 
 
 
 
Jonathan Lee
Guest
Posts: n/a
 
      06-11-2010
On Jun 11, 7:30*am, InV4in <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
InV4in
Guest
Posts: n/a
 
      06-11-2010
On 11 Jun., 13:50, Jonathan Lee <(E-Mail Removed)> 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
 
Reply With Quote
 
Saeed Amrollahi
Guest
Posts: n/a
 
      06-12-2010
On Jun 11, 2:30*pm, InV4in <(E-Mail Removed)> 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 ublic AlgebricValue {
const int deftype;
public:
Fraction(int n, int d) : deftype(1) /* ... */ {}
// ...
};

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

Regards,
-- Saeed Amrollahi
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Derived Structure in Derived Class?? David C++ 3 01-29-2008 07:38 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 01:44 PM
Derived::Derived(const Base&) developereo@hotmail.com C++ 4 05-23-2007 09:32 AM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 12:07 AM



Advertisments