[C++] Virtual Classes

Started by
7 comments, last by SouthernMunk 18 years, 3 months ago
Is it possible to create a purely virtual class without declaring any member functions as purely virtual? E.g.

virtual class Abstract
{
public:
    virtual int SomeFunc();
};

...instead of the following:

class Abstract
{
public:
    virtual int SomeFunc() = 0;
};

Woooooooot!
Advertisement
Not in standard C++.
Oh well. That's OK, no problem.

However, I have another question. Is there a way to allow some members of a base class to NOT be inherited by a derived class?

E.g.

class Base{public:    // An inherited member function.    int ThisShouldBeInherited();    // A virtual member function.    virtual int ThisShouldBeRedefined();    // A member function that I do not want inherited, but I do want public.    int ThisShouldNOTBeInherited();};


I know I could omit the third member function and implement it in another derived class, but it might be easier to just declare it as public, yet non-inheritable (if that's possible).
Woooooooot!
There is no way to prevent a function from being inherited. (that I know of)

I would suggest re-thinking your design to be something like the following...


class AbstractA
{
virtual int A()=0;
};

class AbstractB : public AbstractA
{
virtual int B()=0;
};

class A : public AbstractA
{
virtual int A(){return 0}
};

class B : public AbstractB
{
virtual int A(){return 0}
virtual int B(){return 0}
};
Nope, and I can't think of any situation where you would *want* to do that in a well designed class.
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V
you can define as class with a pure virtual function but still define it.

like so:

class PureVirtual{private:    void someFunctionThatCannotBeUsedByDerivedClasses(){;}public:    virtual void someFunction() = 0;};void PureVirtual::someFunction(){   this->someFunctionThatCannotBeUsedByDerivedClasses();}class Derived : public PureVirtual{public:   void someDerivedFunction(){;}   virtual void someFunction();};void Derived::someFunction(){   PureVirtual::someFunction();//explictly call the pure virtual implementation   someDerivedFunction();}int main( int argc, char **argv ){    // this will cause a compiler error if uncommented     //PureVirtual v;    Derived d;    return 0;}


if a function is private it will not be accessable from the derived classes, but public and not inheritable? no. if it isnt virtual then no polymorphic behaviour can occur, but otherwise what your basically saying is that the superclass has some property or action that a derived class cannot have, i cant think of any inheritance that would include this. what is your use for this?
Maybe you want to read here:

http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.12
Thanks for the help guys, I really appreciate it. The reason I'm doing this is to wrap Winsock up so that it's object oriented and easier to use.

So far, I have three classes (I'm not sure whether to make four or not):

- WinsockConnection: A base class for network communication.
- WinsockClient: Connects to a server for communication.
- WinsockServer: Accepts client connections and communicates with it.

I'm currently deciding how to implement the Accept() member function of the WinsockServer class. I was thinking of taking a WinsockConnection reference parameter and initialise it ready for use. Does this sound feasible and/or appropriate?

I'm still learning and am very exhausted from lack of food and sleep, so I think I'll take a break and have a snack. My last new years resolution was to not eat McDonald's for 1 year - so today I'm going to give myself a Mc HeartAttack. :p
Woooooooot!
Although I started this thread because it related to general programming concepts (as I'm still learning), it also relates to my Winsock wrapper class as well (and hence not appropriate for this forum - sorry moderators).

If you want to look at my Winsock wrapper class (and hopefully give me some feedback), you can find the thread here.

Or you can download the source here.
Woooooooot!

This topic is closed to new replies.

Advertisement