Sign in to follow this  
Sync Views

Class question

Recommended Posts

How can I have it so a function can be called by a pointer to the class but not those of derived classes eg? I'm pretty sure it has to do with if the derived class is :public Object, :protected Object or :private Object but i'm not exactly sure how to do this...
class Object
{
 void MyFunction();
};
class Object2: public Object
{
 void SomeFunction()
 {
  MyFunction();//not allowed
 }
};

int main()
{
 Object* MyObject = new Object;
 Object2* MyObject2 = new Object2;

 MyObject->MyFunction();//allowed
 MyObject2->MyFunction();//not allowed
 return 0;
}

Share this post


Link to post
Share on other sites
ok how about just this?

class Object
{
void MyFunction();
};
class Object2: public Object
{
void SomeFunction()
{
MyFunction();//not allowed
}
};
class Object3: public Object
{
void SomeFunction()
{
MyFunction();//allowed
}
};

Share this post


Link to post
Share on other sites
I'm not sure why you would want to do this, but you could always simply provide an implementation of the function in the derived class that either does nothing or otherwise signals an error of some sort, such as an exception. This could be automated with a helper macro:


#define ILLEGAL(t, x) t x() {throw something;}

class Object
{
public:
void function();
};

class Object2: public Object
{
public:
ILLEGAL(void, function);
};



But, this has limited usefulness in more ways than one, and I'm not sure when you would ever need to do something like this.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this