By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,260 Members | 1,340 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,260 IT Pros & Developers. It's quick & easy.

How to make function final in CPP

P: n/a
hi friends,

read following code and my problem.

class B
{
private:
public:
void fun1(void)
{
}
virtual void fun2(void) = 0;
};

class D : public B
{
private:
public:
void fun1(void)
{
}
void fun2(void)
{
}
};

Here class D is derived from class B. and overload both functions fun1() and
fun2().

but I want that if class D overload fun1() then compiler give error. I know
that in java if we make function as final then derive class never overload it.
but I don't found this type of facility in CPP.

if anyone has solution then please help me.

Thanks in Advance
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hitesh Patel wrote:
class B
{
private:
public:
void fun1(void)
{
}
virtual void fun2(void) = 0;
};

class D : public B
{
private:
public:
void fun1(void)
{
}
void fun2(void)
{
}
};
but I want that if class D overload fun1() then compiler give error. I
know that in java if we make function as final then derive class never
overload it. but I don't found this type of facility in CPP.


I believe that in C++it is inpossible to do your way. Perhaps some compiler
specific pragma defines can do it, but I don't know about any standard
language construction that can do it.

What do you want to do with this construction (exactly)?

--
mP

http://pivoluska.matfyz.cz/
Jul 22 '05 #2

P: n/a
Hitesh Patel wrote:
hi friends,

read following code and my problem.

class B
{
private:
public:
void fun1(void)
{
}
virtual void fun2(void) = 0;
};

class D : public B
{
private:
public:
void fun1(void)
{
}
void fun2(void)
{
}
};

Here class D is derived from class B. and overload both functions fun1() and
fun2().

but I want that if class D overload fun1() then compiler give error. I know
that in java if we make function as final then derive class never overload it.
but I don't found this type of facility in CPP.

if anyone has solution then please help me.


Are you talking about "overriding" or "overloading" ?

from your example above if we do this:
D * d = new D;
B * b = d;

b->fun1(); // *allways calls B::fun1*
b->fun2(); // calls fun2 the most derived class.

You can only override virtual functions if the rest of the application
only has access to the base class definition.

However, you can't prevent D::fun2 from being overridden from any
derived class.

Jul 22 '05 #3

P: n/a
You don't mean "overload," you mean "override."

Methods in C++ are "final" by default, in the sense that they cannot
be overridden unless they are delcared "virtual."

I don't know of any way to prevent sub-classes from using a particular
method name used by the base class. However, if a method of an object
of the derived class is called, and a public, non-virtual method of
the same name exists in a publicly derived base class, the method of
the base class will be the one invoked.

Hth,
Jeff

hi*********@rediffmail.com (Hitesh Patel) wrote in message news:<52*************************@posting.google.c om>...
hi friends,

read following code and my problem.

class B
{
private:
public:
void fun1(void)
{
}
virtual void fun2(void) = 0;
};

class D : public B
{
private:
public:
void fun1(void)
{
}
void fun2(void)
{
}
};

Here class D is derived from class B. and overload both functions fun1() and
fun2().

but I want that if class D overload fun1() then compiler give error. I know
that in java if we make function as final then derive class never overload it.
but I don't found this type of facility in CPP.

if anyone has solution then please help me.

Thanks in Advance

Jul 22 '05 #4

P: n/a


Jeff wrote:
If a method of an object
of the derived class is called, and a public, non-virtual method of
the same name exists in a publicly derived base class, the method of
the base class will be the one invoked.


If it's invoked through a pointer to the base type, that is.

Jul 22 '05 #5

P: n/a
Matej Pivoluska <sp**@spam.com> wrote in message news:<br**********@crax.cesnet.cz>...
Hitesh Patel wrote:
class B
{
private:
public:
void fun1(void)
{
}

virtual void fun2(void) = 0;
};

class D : public B
{
private:
public:
void fun1(void)
{
}
void fun2(void)
{
}
};
but I want that if class D overload fun1() then compiler give error. I
know that in java if we make function as final then derive class never
overload it. but I don't found this type of facility in CPP.


I believe that in C++it is inpossible to do your way. Perhaps some compiler
specific pragma defines can do it, but I don't know about any standard
language construction that can do it.

What do you want to do with this construction (exactly)?


I had Created one TIMER class which call work_to_do() function of its derived
class. this TIMER class is Threaded and contains run() method which is called
when schedule() method of TIMER class is called. here work_to_do() method is
called by run() method after given time. but if we override run() method. then
my TIMER class does not work.

Hitesh Patel
Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.