Hiding rule and pure virtuals

T

Thomas Kowalski

Hi everyone,
following situation:

class A
{
void m(T value) =0;
}

class B : A
{
void m(TA value);
void m(TB value);
}

class T
{
}

class TA : T
{
}

class TB : T
{
}

The original methode A::m(T) is not overriden, just hidden by B::m. Is
B an "abstract" class and have to overwrite m(T) specificly? Can a
methode call like this be resolved like expected according to the
C++Spec:
B b;
TA ta;
b.m(ta);


Thanks in advance,
Thomas Kowalski
 
A

ashishyadav26

I am not sure whether you can do that. I am using gcc 4.1.0 here is
what I compiled and the error I got.

#include <iostream>

using namespace std;

class T;
class TA;
class TB;

class A
{
virtual void m ( T value) = 0;

};

class B : A
{
public:
void m ( TA value);
void m ( TB value);
};

class T
{

};

class TA : T
{

};

class TB : T
{

};

int main()
{
B obj;
TA obj_ta;

obj.m ( obj_ta);
}

class.cpp: In function 'int main()':
class.cpp:39: error: cannot declare variable 'obj' to be of abstract
type 'B'
class.cpp:16: note: because the following virtual functions are pure
within 'B':
class.cpp:11: note: virtual void A::m(T)
 
B

benben

Thomas said:
Hi everyone,
following situation:

class A
{
void m(T value) =0;
}

I guess you probably want the following:

class T;

class A
{
public:
virtual void m(T value) = 0;
};

Note that:
1. Type T is declared before it is used.
2. Member function m() is now a public member.
3. m() is also made virtual. Otherwise the "=0" is just an utter error.
4. The class definition ends with a semicolon.
class B : A
{
void m(TA value);
void m(TB value);
}

Again, I guess you want the following:

class TA;
class TB;

class B: public A
{
public:
void m(TA value);
void m(TB value);
};
class T
{
}

class TA : T
{
}

class TB : T
{
}

Reminder: Both TA and TB privately inherits from T, and all three
classes need semicolon to end with.
The original methode A::m(T) is not overriden, just hidden by B::m. Is
B an "abstract" class and have to overwrite m(T) specificly? Can a
methode call like this be resolved like expected according to the
C++Spec:

Class A is abstract, it declares a pure virtual member function. Unless
class B explicitly implements the pure virtual member function A::m, it
remains abstract.
B b;
TA ta;
b.m(ta);

Technically, if B is abstract then you can't instantiate an object of
type B, so never mind what happens to the object.

On the other hand if you make B non-abstract by supplying an
implementation of A::m in B, e.g.

class B: public A
{
public:
void m(T);
void m(TA);
void m(TB);
};

then overload resolution will guarantee the right overload gets invoked.
Thanks in advance,
Thomas Kowalski

Ben
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top