|
|
Hello everyone,
I have tried compiler allows to change const property of an overloaded method. Here is my proof of concept code.
My question,
1. is it good code or good practice?
2. It yse in (1), are there any practical usage of this type of "overloading"?
class Base {
public:
const int foo() {return 200;};
};
class Derived : public Base {
public:
int foo() {return 100;};
};
int main()
{
Base b;
Derived d;
int rtn = b.foo();
rtn = d.foo();
return 0;
}
thanks in advance,
George
|
|
|
|
|
There's no real point to the code in your example, so of course it can't be said to be "good". However, i've used similar constructs in past, here are a couple of scenarios where they made sense:
When returning references to internal data, using a const and a non-const access method to return const and non-const references.
This is helpful when, for instance, most users of the API will hold only const references to the object implementing it, but in at least one scenario (a client that creates or modifies the object) there will be callers that hold a non-const reference and need non-const access to the internal data.
For private/protected methods: used to provide subtly different behavior to public access and mutator methods.
I was writing a wrapper class for INI access last night. Two different methods, GetValue() and SetValue(), both need to retrieve a section pointer from the data store. The Get method will not modify the section, and should fail if the section is not found, while the Set method will modify the section, creating it if it is not found. So, i added two GetSection() methods for use by the public API, one const, the other not, each implementing one of the needed behaviors.
But who is the king of all of these folks?
|
|
|
|
|
Thanks Shog9,
Your experience is good. But you are not talking about specific to overloading or overriding, but general rules, right?
regards,
George
|
|
|
|
|
George_George wrote: But you are not talking about specific to overloading or overriding, but general rules, right?
Not at all. Consider my second example: without overloading, you could write two separate methods - GetSection() and FindOrCreateSection() - but that breaks the abstraction by requiring callers to explicitly specify when section creation is done. Overloading GetSection() to provide a const version that will simply fail if the section isn't found lets you keep the calling code relatively consistent for const and non-const scenarios.
But who is the king of all of these folks?
|
|
|
|
|
Thanks Shog9,
1.
You mean the following scenario will make componet more complex and coupled with caller?
Shog9 wrote: that breaks the abstraction by requiring callers to explicitly specify when section creation is done.
2.
How about overriding? If we change some const property of some overridden methods (return values and input parameters), is that good practice or anti-pattern? Any practical values/scenarios?
regards,
George
|
|
|
|
|
Hi friends ..is thr any doubt on C,C++,MFC .....plz send ur questions to my mail id also ..i ll send my answers...
This is my mail id :
rajugis2007@hotmail.com
*****THANKS N ADVANCE****
Mathen.K
(I WILL TRY MY LEVEL BEST )
|
|
|
|
|
rowdy_vc++ wrote: Hi friends ..is thr any doubt on C,C++,MFC .....plz send ur questions to my mail id also ..i ll send my answers...
This is my mail id :
rajugis2007@hotmail.com
i thought u r buying the OOP & C++ book based on CP comments to u [^], but it seems u bought C++ FAQ book.
|
|
|
|
|
Mr .Rajkumar ..plz dont tease me ..becoz ..i wanna improve skill ..thts wat i am saying ..
*****THANKS N ADVANCE****
Mathen.K
(I WILL TRY MY LEVEL BEST )
|
|
|
|
|
Just curious - Where did you learn this idiotic spelling (becoz, ur, plz,...)?
|
|
|
|
|
mr .cranky did u understood or not?
*****THANKS N ADVANCE****
Mathen.K
(I WILL TRY MY LEVEL BEST )
|
|
|
|
|
Yes we can understand, but it is annoying.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
|
|
|
|
|
u can answer the questions through CP itself. Is n't it?
just curious, do you have MPD (multiple personality disorder), i am seeing you in your signature as raju.k (male), Nisha.S(female), now in Mathen.K(male), i think you are none of them.
|
|
|
|
|
That creature is a retard.
Nobody can give you wiser advice than yourself. - Cicero
.·´¯`·->Rajesh<-·´¯`·.
Codeproject.com: Visual C++ MVP
|
|
|
|
|
What we understand when we see txtpeak is that we're dealing with someone who is 8, or at least, acts that way. It's annoying and it makes you look silly.
Christian Graus - Microsoft MVP - C++
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
There has never been any doubt that there are many VB programmers out there who have doubts about C and C++...
|
|
|
|
|
k guys ..leave it ..if u r interest ..always welcome ..otherwise no need...
k na Mr.rajkumar,cranky,and pallni
*****THANKS N ADVANCE****
Mathen.K
(I WILL TRY MY LEVEL BEST )
|
|
|
|
|
You are inviting comments like these. Your original post does not make any sense. Nobody knows what you want.
|
|
|
|
|
k ..Mr.cranky .thanks to your replays ..in feature i will be able to follow
*****THANKS N ADVANCE****
Mathen.K
(I WILL TRY MY LEVEL BEST )
|
|
|
|
|
I recommend buying a book on C++, and one on English. We are an international community, we are pretty forgiving, but your original post made no sense at all, you need to at least be able to communicate your intention if you want meaningful replies.
Christian Graus - Microsoft MVP - C++
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
But why you dont want to answer to questions on this forum,maybe your answers will be helpful for other members?
|
|
|
|
|
Please stop spamming the message boards with pointless posts.
Nobody can give you wiser advice than yourself. - Cicero
.·´¯`·->Rajesh<-·´¯`·.
Codeproject.com: Visual C++ MVP
|
|
|
|
|
No, the doubt keyword was removed with the latest service pack. Thanks for playing, though.
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Hi,
The lib //aubio.org/beta/win32/ did not include the def, how can I can generate the lib file in order to call it under vc?
if I get the some binary files and dll files in this lib,the binary file(exe )need to call the function in dll, so how can I make it happen?
and anyone can tell me how to use this lib under windows in order to get the runnng result?
|
|
|
|
|
You may use runtime linking [^].
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
|
|
|
|