473,395 Members | 1,629 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

can I override private functions?

I take it this is wrong:-

class Direct_draw
{
public:
Direct_draw ();

virtual ~Direct_draw ()
{}

private:
virtual void draw_primary () = 0;
};

class Dd_animation: public Direct_draw
{
public:
Dd_animation()
{}

~Dd_animation()
{}

private:
virtual void draw_primary ()
{}
};

Direct_draw::Direct_draw ()
{
draw_primary();
}

int main (void)
{
Direct_draw* animation = new Dd_animation();
return 0;
}

it gives a linker error for draw_primary()
--
Nick Keighley

Mar 13 '07 #1
7 5172
On Mar 13, 11:04 am, "Nick Keighley"
<nick_keighley_nos...@hotmail.comwrote:
I take it this is wrong:-

class Direct_draw
{
public:
Direct_draw ();

virtual ~Direct_draw ()
{}

private:
virtual void draw_primary () = 0;

};

class Dd_animation: public Direct_draw
{
public:
Dd_animation()
{}

~Dd_animation()
{}

private:
virtual void draw_primary ()
{}

};

Direct_draw::Direct_draw ()
{
draw_primary();

}

int main (void)
{
Direct_draw* animation = new Dd_animation();
return 0;

}

it gives a linker error for draw_primary()
You can certainly override private virtual functions; you just can't
call virtuals from the ctor:

http://www.parashift.com/c++-faq-lit....html#faq-23.5

Cheers! --M

Mar 13 '07 #2
Nick Keighley wrote:
I take it this is wrong:-
Yes.
class Direct_draw
{
public:
Direct_draw ();

virtual ~Direct_draw ()
{}

private:
virtual void draw_primary () = 0;
};
....
Direct_draw::Direct_draw ()
it gives a linker error for draw_primary()
Polymorphism in constructors and destructors works different from what you
seem to expect. In Direct_draw's constructor, the object is a Direct_draw,
since the derived class constructor hasn't yet been called. So regarding
polymorphism, when calling draw_primary() from Direct_draw's constructor,
the draw_primary() from that class is called. However, you don't provide an
implementation, so you get a linker error.

Mar 13 '07 #3
mlimber wrote:
On Mar 13, 11:04 am, "Nick Keighley"
<nick_keighley_nos...@hotmail.comwrote:
>I take it this is wrong:-

class Direct_draw
{
public:
Direct_draw ();

virtual ~Direct_draw ()
{}

private:
virtual void draw_primary () = 0;

};

class Dd_animation: public Direct_draw
{
public:
Dd_animation()
{}

~Dd_animation()
{}

private:
virtual void draw_primary ()
{}

};

Direct_draw::Direct_draw ()
{
draw_primary();

}

int main (void)
{
Direct_draw* animation = new Dd_animation();
return 0;

}

it gives a linker error for draw_primary()

You can certainly override private virtual functions; you just can't
call virtuals from the ctor:

http://www.parashift.com/c++-faq-lit....html#faq-23.5
The FAQ claims that dynamic binding isn't happening in constructors and
destructors, but that's actually not true. Dynamic binding does happen, but
only up to the class the constructor/destructor belongs to. Consider the
following example:

#include <iostream>

class Base
{
public:
void test() { virtual_function(); }
virtual void virtual_function() { std::cout << "Base\n"; }
};

class Derived : public Base
{
public:
Derived() { test(); }
virtual void virtual_function() { std::cout << "Derived\n"; }
};

int main()
{
Derived d;
}

Without dynamic binding, this would print "Base", but it does
print "Derived".

Mar 13 '07 #4
* Rolf Magnus:
mlimber wrote:
>>
http://www.parashift.com/c++-faq-lit....html#faq-23.5

The FAQ claims that dynamic binding isn't happening in constructors and
destructors
Well no, it doesn't claim that. But it might seem to. My fault, for
insisting on discussing dynamic binding down to the class used to
instantiate the object (the next FAQ item).

There is a shortage of useful terminology, with otherwise suitable words
and terms already having established, unsuitable meanings.

"Dynamic Binding During Initialization" was a compromise (Marshall's
idea, I wanted "virtual construction"), which I think is about the best
that can be done, but as you point out it seems to erronously indicate
that you don't get dynamic binding during initialization by default.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 13 '07 #5
On Mar 13, 11:04 am, "Nick Keighley"
<nick_keighley_nos...@hotmail.comwrote:
I take it this is wrong:-

class Direct_draw
{
public:
Direct_draw ();

virtual ~Direct_draw ()
{}

private:
virtual void draw_primary () = 0;

};

class Dd_animation: public Direct_draw
{
public:
Dd_animation()
{}

~Dd_animation()
{}

private:
virtual void draw_primary ()
{}

};

Direct_draw::Direct_draw ()
{
draw_primary();

}

When the Direct_draw constructor is called, the object is still a
Direct_draw object, not a Dd_animation object as you might expect.
This is why, during the Direct_draw constructor call, draw_primary()
is still a pure virtual method. Your linker error is probably
something along the lines of "attempt to call pure virtual method"?

One solution to this problem is to have a public initialize() method,
which must be called manually after any Direct_draw-derived object is
created. Any initialization you need to do, including virtual method
calls, can be included in initialize(). The problem with this is that
you might forget to call it, resulting in bugs in your code.

Another solution would be lazy initialization. Here you'd call
initialize() in each method that requires the object to be
initialized. The initialize() method would only run the initialization
code if it has not already been run. The problem with this is that you
will probably have to remember to call initialize() in each of your
derived methods. If you forget, you might run a method on an
uninitialized object, resulting in bugs.

Other people in this group will probably suggest better solutions.

Regards,
Markus.

Mar 13 '07 #6

mlimber wrote:
On Mar 13, 11:04 am, "Nick Keighley"
<nick_keighley_nos...@hotmail.comwrote:
I take it this is wrong:-

class Direct_draw
{
public:
Direct_draw ();

virtual ~Direct_draw ()
{}

private:
virtual void draw_primary () = 0;

};

class Dd_animation: public Direct_draw
{
public:
Dd_animation()
{}

~Dd_animation()
{}

private:
virtual void draw_primary ()
{}

};

Direct_draw::Direct_draw ()
{
draw_primary();

}

int main (void)
{
Direct_draw* animation = new Dd_animation();
return 0;

}

it gives a linker error for draw_primary()

You can certainly override private virtual functions; you just can't
call virtuals from the ctor:

http://www.parashift.com/c++-faq-lit....html#faq-23.5

thanks! I knew I was doing something dumb, but I just couldn't work
out
what it was! I even suspected there'd be a FAQ.
--
Nick Keighley

Walking on water and developing software from a specification
are easy if both are frozen. (Edward V Berard)

Mar 13 '07 #7
Alf P. Steinbach wrote:
* Rolf Magnus:
>mlimber wrote:
>>>
http://www.parashift.com/c++-faq-lit....html#faq-23.5

The FAQ claims that dynamic binding isn't happening in constructors and
destructors

Well no, it doesn't claim that. But it might seem to.
Some parts (especially question 23.6 and its answer) seem to imply it.
My fault, for insisting on discussing dynamic binding down to the class
used to instantiate the object (the next FAQ item).

There is a shortage of useful terminology, with otherwise suitable words
and terms already having established, unsuitable meanings.

"Dynamic Binding During Initialization" was a compromise (Marshall's
idea, I wanted "virtual construction"), which I think is about the best
that can be done, but as you point out it seems to erronously indicate
that you don't get dynamic binding during initialization by default.
I think both aren't really good, but I don't have an idea for a better term.

Mar 13 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Ruben Van Havermaet | last post by:
Hi, I have a problem using member functions in derived classes that override virtual member functions of base classes. The following pieces of (simplified) code don't work. Can anybody give...
14
by: JPRoot | last post by:
Hi I use the following syntax to have events inherited from base to child classes which works nicely (virtual and override keyword on events). But I am wondering if it is a "supported" way of using...
9
by: Ken Varn | last post by:
Is there anyway to override a public virtual method or property so that it is private in my derived class? I tried using new on the property and making it private, but no luck. --...
2
by: Jason Huang | last post by:
Hi, In my C# Windows form MyForm, it has a function MyFunction which is a big function and has lots of codes. I am thinking the override for MyFunction, one MyFunction has a parameter which...
8
by: puzzlecracker | last post by:
The statement is taken from FAQ . What about non-virtual functions? Can they be overriden? I still don't see a good justification to prefer private inheritance over composition. In fact, I have...
23
by: Chris Gordon-Smith | last post by:
Hello All I have a base class called Action_Request, and a set of classes corresponding to different kinds of Action_Request, each of which inherits from Action_Request. Eg:- class ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.