|
-
November 4th, 2010, 06:12 AM
#13
Re: [Discussion] Modifying a class' behaviour - inheritance pitfalls
 Originally Posted by Zaccheus
Interesting, but I don't see the relevance as it talks about derived classes changing the behaviour. That article however shows why I dislike non-private data members.
If the classes in your example have non-virtual assignment operators, then the code below would change the contents of the string without the assignment operator of the derived class being called.
Code:
void foo(String& s)
{
s = String("foo");
}
void bar()
{
FastString s("foobar");
foo(s);
// uh oh, s.length() is incorrect
}
For this example to work correctly, the base class' assignment operator has to be virtual and the derived class needs an overloaded assignment operator taking a base class object (to override the virtual function in the base class).
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|