Pure virtual functions: declaring a function in a base class

M

Martin

Why doesn't this work? Is there a way to achieve it?
Compiler complains about func1() not being defined in class C, which
inherits it from class B.

class A
{
public:
virtual void func1() = 0;
}

class B
{
public func1()
{
printf("test");
}

};

class C : public B, public A
{

};

int _tmain(int argc, _TCHAR* argv[])
{
C c;
c.func1();
return 0;
}
 
A

Alf P. Steinbach /Usenet

* Martin, on 25.08.2010 05:11:
Why doesn't this work? Is there a way to achieve it?
Compiler complains about func1() not being defined in class C, which
inherits it from class B.

class A
{
public:
virtual void func1() = 0;
}

class B
{
public func1()
{
printf("test");
}

};

class C : public B, public A
{

};

int _tmain(int argc, _TCHAR* argv[])
{
C c;
c.func1();
return 0;
}

Presumably you want the implementation in B to override the one in A.

And although you're not posting your real code, the typing mistakes that you've
made seem to indicate that you come from a Java background. Which is helpful in
helping you. But in general, don't assume that your readers are telepaths and
can see what's only in your head or on your screen: that's just stupid. Post
*real code*. See the FAQ about how to ask about Code That Does Not Work.

Now, B::func1 is not virtual, so it can't override anything.

However, if you make B::func1 virtual then you still don't get an override, only
a call ambiguity. The compiler sees 2 possibilities for which function you're
trying to call. And nothing decides between them.

You can either

* add an override in class C, e.g. calling the B::func1, or

* make A an abstract class, which means change the inheritance so that C
inherits B inherits A (in this case func1 becomes virtual in B since it's
already virtual in A, and B::func1 becomes an override of A::func1), or

* make A a logical interface, which means let B inherit virtually from A and
let C inherit virtually from A, and in this case B implements the A
interface A for class C, at some run time and design level cost.

That said, there is no such thing as '_tmain' in C++. It's a Microsoft
monstrosity, and moreover it's a monstrosity (only) in support of Windows 9x,
which one may presume that you're not out to support? Use a standard 'main'.

You don't use the 'main' arguments so no point in declaring them.

Finally you can omit the 'return 0', since 'main' returns 0 by default.


Cheers & hth.,

- Alf
 
M

Martin

Hey Alf,
Thanks for the reply.
I don't see why I should declare a function "virtual" to override
a virtual function, I noly need to declare it virtual in the base
class, not in the derived class, but maybe I misunderstood what you
said.
That being said, I still don't understand why the compiler won't
take B's func1 as an override for class A. I understand your proposals
and agree with them, but I would like to understand why the compiler
won't override func1(). Maybe I just need to go back and read about c+
+ compiler implementation...

You understood the problem perfectly, I'll go and read the FAQ
about posting...

Thanks,
Martín.





* Martin, on 25.08.2010 05:11:




Why doesn't this work? Is there a way to achieve it?
Compiler complains about  func1() not being defined in class C, which
inherits it from class B.
class A
{
public:
   virtual void func1() = 0;
}
class B
{
   public func1()
   {
           printf("test");
   }

class C : public B, public A
{

int _tmain(int argc, _TCHAR* argv[])
{
   C c;
   c.func1();
   return 0;
}

Presumably you want the implementation in B to override the one in A.

And although you're not posting your real code, the typing mistakes that you've
made seem to indicate that you come from a Java background. Which is helpful in
helping you. But in general, don't assume that your readers are telepaths and
can see what's only in your head or on your screen: that's just stupid. Post
*real code*. See the FAQ about how to ask about Code That Does Not Work.

Now, B::func1 is not virtual, so it can't override anything.

However, if you make B::func1 virtual then you still don't get an override, only
a call ambiguity. The compiler sees 2 possibilities for which function you're
trying to call. And nothing decides between them.

You can either

   * add an override in class C, e.g. calling the B::func1, or

   * make A an abstract class, which means change the inheritance so that C
     inherits B inherits A (in this case func1 becomes virtual in B since it's
     already virtual in A, and B::func1 becomes an override of A::func1), or

   * make A a logical interface, which means let B inherit virtually from A and
     let C inherit virtually from A, and in this case B implements the A
     interface A for class C, at some run time and design level cost.

That said, there is no such thing as '_tmain' in C++. It's a Microsoft
monstrosity, and moreover it's a monstrosity (only) in support of Windows 9x,
which one may presume that you're not out to support? Use a standard 'main'.

You don't use the 'main' arguments so no point in declaring them.

Finally you can omit the 'return 0', since 'main' returns 0 by default.

Cheers & hth.,

- Alf
 
F

Felix Palmen

* Martin said:
I don't see why I should declare a function "virtual" to override
a virtual function, I noly need to declare it virtual in the base
class, not in the derived class, but maybe I misunderstood what you
said.

I don't think this makes much sense. Assume a function X that is virtual
in class A, non-virtual in class B and overriden in class C with
inheritance graph A -> B -> C.

What should happen when you call X() on an instance of C through a
pointer of type A? You think B::X() should be invoked? In my opinion,
this would be ill-defined.
That being said, I still don't understand why the compiler won't
take B's func1 as an override for class A. I understand your proposals
and agree with them, but I would like to understand why the compiler
won't override func1(). Maybe I just need to go back and read about c+
+ compiler implementation...

The notion of "overriding" implies inheritance. In your example, you
present a class that happens to comprise a function with the same name
as another class. But as these classes do not have any relation to each
other, the functions are unrelated as well.

Any other assumption would violate agnosticism. Without any interface
implementation or inheritance, your class would need knowledge about
derived classes to know that it's method should be an implementation of
a virtual method in another class.

So, I don't think this is related to compiler implementation, more to
language sematics.

Regards, Felix
 
M

mingze zhang

It is very hard to guess what you do know and what you don't know from
your questions..
What you probably wanted to do is this,

class A
{
public:
virtual void func1() = 0;
}

class B : public A
{
public:
void func1()
{
printf("test");
}
};

class C : public B
{
};

int _tmain(int argc, _TCHAR* argv[])
{
C c;
c.func1();
return 0;
}
 
F

Felix Palmen

* mingze zhang said:
It is very hard to guess what you do know and what you don't know from
your questions..

Please, take some care about thread structure when posting. I did not
ask any question here.

Regards, Felix
(f'up2 poster)
 

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,054
Latest member
TrimKetoBoost

Latest Threads

Top