Jump to content
  • Advertisement
Sign in to follow this  
SouthernMunk

[C++] Virtual Classes

This topic is 4556 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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;
};

Share this post


Link to post
Share on other sites
Advertisement
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).

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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}
};

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!