How to make function final in CPP

H

Hitesh Patel

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
 
M

Matej Pivoluska

Hitesh said:
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)?
 
G

Gianni Mariani

Hitesh said:
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.
 
J

Jeff

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
 
J

Jeff Schwab

Jeff said:
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.
 
H

Hitesh Patel

Matej Pivoluska said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,156
Messages
2,570,878
Members
47,404
Latest member
PerryRutt

Latest Threads

Top