Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: pure virtual functions as non-const in derived classes

Reply
Thread Tools

Re: pure virtual functions as non-const in derived classes

 
 
Gianni Mariani
Guest
Posts: n/a
 
      08-27-2003
Victor Hannak wrote:
> All the documentation I have looked through (the FAQ, textbooks) all show
> pure virtual functions declared as const.


No problem whatsoever.

Why not give it a go before posting ?

>
> Is there anything wrong with instantiating a non-const pure virtual
> function? What if I need the polymorphed version of the function to be able
> to modify private data members of the derived class? I wrote some code with
> the pure virtual function not defined as const, and it seems to work fine,
> but is this bad coding style? Why doesn't any of the documentation have
> examples like this?


Bad docs ?

>
> I understand why the definition of the pure virtual function in the ABC is
> const. After all, it can't/shouldn't do anything in the ABC. But if I make
> it const, then I also have to make the polymorphed versions of it const as
> well in order for the declaration to match, right? (Otherwise, it seems
> that the compiler does not see the relationship between the abstract and
> derived version of the function, and I get an error about my derived class
> also being an ABC)
>


class ABC
{
public:
virtual void F();
};

class DC : public ABC
{
public:
int x;
virtual void F()
{
x=1;
}
};

.... try that !

 
Reply With Quote
 
 
 
 
Victor Hannak
Guest
Posts: n/a
 
      08-27-2003
>
> class ABC
> {
> public:
> virtual void F();
> };
>
> class DC : public ABC
> {
> public:
> int x;
> virtual void F()
> {
> x=1;
> }
> };
>
> ... try that !
>


I am not trying to nitpick, but I want to understand this...the virtual
declaration for F() in class ABC above is missing the " = 0 " (see FAQ
22.4), and therefore is _not_ a pure virtual function, right? Thus, you
would have to define the functionality of F() in class ABC and class ABC
would no longer be abstract. Is this right?

Thanks, Vic


 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      08-27-2003
Victor Hannak wrote:

>
> I am not trying to nitpick, but I want to understand this...the virtual
> declaration for F() in class ABC above is missing the " = 0 " (see FAQ
> 22.4), and therefore is _not_ a pure virtual function, right?


YES.

Thus, you
> would have to define the functionality of F() in class ABC and class ABC
> would no longer be abstract. Is this right?


YES

>
> Thanks, Vic


I'm glad you asked..

All F() = 0 means is that the class is abstract (abstract meaning that
it may not be instantiated without being derived). It does not mean you
can't call it. See below.

Also, a const method is distinct from a non-const method of the same name.


#include <iostream>

class ABC
{
public:
virtual void F() = 0;
virtual void F() const = 0;
};

class DC : public ABC
{
public:
int x;
virtual void F()
{
x=1;
std::cout << "DC::F\n";
}

virtual void F() const
{
// can't do x=1;
std::cout << "DC::F const\n";
}
};

void ABC::F()
{
std::cout << "ABC::F\n";
}

void ABC::F() const
{
std::cout << "ABC::F const\n";
}


int main()
{
ABC * p = new DC;

p->F();

const ABC * pc = p;

pc->F();

pc->ABC::F();

delete p; // VERY BAD - MUST MAKE ABC destructor virtual
}

code prints:

DC::F
DC::F const
ABC::F const

Any class with a virtual method should have a virtual destructor.



 
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::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 12:07 AM
Wrapping classes with pure virtual functions gabriel.becedillas@gmail.com Python 3 12-17-2006 06:14 PM
private virtual functions and pure virtual functions with bodies John Goche C++ 10 12-08-2006 04:00 PM
redeclaring (pure) virtual functions in derived classes? Steven T. Hatton C++ 5 05-22-2005 11:53 PM
Re: pure virtual functions as non-const in derived classes jeffc C++ 0 08-27-2003 05:55 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57