Advanced C++: const and Functions

  Рет қаралды 130,881

Bo Qian

Bo Qian

Күн бұрын

There are 3 ways a const can be used with a function:
1. const parameter;
2. const return value;
3. const function.
Notes can be downloaded from: boqian.weebly.com

Пікірлер: 117
@thachnnguyen
@thachnnguyen 2 жыл бұрын
This person is among the best out there on youtube. Right straight to the point and no BS content to waste your time. Mostly comprehensive (if not all the time). Explanations succinct. Shows work and thought were put into making these tutorials, not just on the fly stuff that can be unorganized and wasting one's time. Keep up the good work.
@toth1982
@toth1982 Жыл бұрын
I totally agree. Very unique gem about C++.
@onkarmahajan4125
@onkarmahajan4125 9 жыл бұрын
your Videos are really good and detail oriented ... I really appreciate your work - keep it up .. thank you
@sandeepkalwale7707
@sandeepkalwale7707 2 жыл бұрын
I agree 👍
@sandeepkalwale7707
@sandeepkalwale7707 2 жыл бұрын
Very clear and unique content. All important concepts covered through series. Thanks for your valuable contribution. Please prepare more playlist on C++.
@YM-vn2ts
@YM-vn2ts 7 жыл бұрын
One of the best C++ videos on youtube. Thank you
@hearthrob4u
@hearthrob4u 11 жыл бұрын
Awesome videos !!!! Clear explanations better than going through books!
@drunkmadala
@drunkmadala 5 жыл бұрын
I have bettling with this concept for so long, cheers!!!
@lazarbecic2199
@lazarbecic2199 2 жыл бұрын
Your work is greatly appreciated master Bo Qian~
@a1988ditya
@a1988ditya 10 жыл бұрын
you are the best ...great set of c++ playlist ..really helpful ... \m/
@DeanWinchester844
@DeanWinchester844 11 жыл бұрын
I like your videos. You explain it very good, very detailed. It's better than any book I've read so far.
@shvideo1
@shvideo1 6 жыл бұрын
Excellent detailed video. Very educational. Thank you for taking the time to share your knowledge with the rest of the world.
@ВиталийВасилевич-р4л
@ВиталийВасилевич-р4л 7 жыл бұрын
Your video is a perfect material which can help to learn programming as well as technical English. Thank you for your great job!!
@riwawaves
@riwawaves 9 ай бұрын
no wonder i was so confused, my professor introduced this week 2 into the course. tysm for the clear explanation!
@malharjajoo6237
@malharjajoo6237 7 жыл бұрын
you have a good clarity on separation of concerns.. good job
@plutonnico1733
@plutonnico1733 5 жыл бұрын
MANY THANKS BO QIAN!! Subjects are very good, explanation too!
@toth1982
@toth1982 Жыл бұрын
8:00 This behavior that when we overload a member function with cost is something which can lead to unexpected errors imho. Suppose you writer of the class writes a const variant. It will run for non-const object. Then the writer of the class writes a non-const variant. From now on the new (non-const) function will be called for the same call. For me this can lead to unexpected errors.
@oualice9410
@oualice9410 5 жыл бұрын
Clear logic with concise examples. Great video!
@00gamer2
@00gamer2 10 жыл бұрын
I think "passive function" would be more appropriate name for const functions; constant variables keep the internal state of themselves constant though out. They are only worried about themselves; they don't care if they change the state of an external variable. Const functions differ in this respect. If const variables are children, then the functions are carers, always making sure the function and variables that are in their care are not hurt in any way. These const functions _care_ about others, you see. They are unsung heroes of this great paradigm of programming that we call "C++", because they CARE. _THEY CARE_. Calling these role-models "const functions" is an insult! They are _not_ children, _not_ those egotistic const variables playing it safe! To express my respect for these functions, I shall, from now on, call them: "mother functions". *THE TRUTH HAS BEEN SPOKEN*
@SkSafowan
@SkSafowan 3 жыл бұрын
Man great detail and very easy to understand video. It's a shame that you stop uploading videos.
@МаратСаидов-н3ю
@МаратСаидов-н3ю 7 жыл бұрын
Thank you for this course. Continue in the same way!
@priyanshipatel7804
@priyanshipatel7804 7 жыл бұрын
indeed these videos are very comprehensive
@kbillore28
@kbillore28 4 жыл бұрын
A function that takes const reference parameter can also be overloaded with a function which take reference parameter by passing const or non const parameter.
@azadalishah2966
@azadalishah2966 2 ай бұрын
Have you tried that? Const parameter function can be called by passing non-const variable as well. I believe const-parameter function is called when literal value is passed as argument
@7thAttempt
@7thAttempt 11 жыл бұрын
Excellent video! Thank you very much for sharing your C++ knowledge! :)
@pseudounknow5559
@pseudounknow5559 3 жыл бұрын
Nice explanation, usefull in 2021 !!!
@pdd3
@pdd3 Жыл бұрын
Bo Qian should be the president of Earth.
@edlugoa
@edlugoa 10 жыл бұрын
Awesome video setting all the possible occurrences!!!
@ghigopigo8317
@ghigopigo8317 3 ай бұрын
I just stumbled into this tutorial, i really like the style of it. My only worry is that it may not be up to date with modern c++ (?)
@zili8317
@zili8317 8 жыл бұрын
Your tutorial videos are really helpful! Thank you!
@jarusll
@jarusll 6 жыл бұрын
Awesome bro, well explained. These are really useful
@isharthrastogi782
@isharthrastogi782 6 жыл бұрын
exactly what i was looking for!
@FRChang-g2o
@FRChang-g2o Жыл бұрын
Thanks for sharing such a good tutorial!!
@azadalishah2966
@azadalishah2966 5 жыл бұрын
At 5:55, non-const object d can call const function, does it mean that const function is called always by const or non-const object if no overloading non-const function?
@s.d.gentry1354
@s.d.gentry1354 9 жыл бұрын
Very well done and informative.
@davidbohadana8274
@davidbohadana8274 9 жыл бұрын
It was amazing, thank you so mach for grate lesson :)
@xty007
@xty007 7 жыл бұрын
amazing lessons, thank you so much!
@alltheway99
@alltheway99 2 жыл бұрын
5:26 name is not a temporary, is in fact a data member
@leelamanohar2001
@leelamanohar2001 8 жыл бұрын
Please find the sample program for the exercise: #include using namespace std; class Dog{ public: void set_age(const int &) { cout
@altafmahmud6095
@altafmahmud6095 7 жыл бұрын
After following your lecture on lvalue and rvalue reference, I understood that "void setAge(const int& a)" will be invoked by following two types of invocations: 1. Passing an rvalue parameter: Dog d; d.setAge(10); 2. Passing a const int parameter: const int n = 10; Dog d; d.setAge(n); And "void setAge(int& a)" will be invoked by any lvalue parameter: Dog d; int n = 10; d.setAge(n); Now my question is with following code: int n = 10; const int& m = n; Why this works? In this context "d.setAge(n)" should have been ambiguous, isn't it?
@4Seaofthoughts
@4Seaofthoughts 7 жыл бұрын
d.setAge(n) This calls setage(int &a),there is no ambiguity as n is lvalue.
@maximus5415
@maximus5415 11 жыл бұрын
Great video, explained extremely well :]
@elephiant8696
@elephiant8696 10 жыл бұрын
Seriously good tutorial, thanks! :D
@stshsngh1
@stshsngh1 8 жыл бұрын
Great Work !!
@ResZRyou
@ResZRyou 6 жыл бұрын
Thanks for sharing the material.
@bahalason
@bahalason 9 жыл бұрын
Does it make sense to make a const function if the class doesn't have any private variables? Like a parser class for example. I could set every functions to const, but is there a need?
@raviraja0559
@raviraja0559 10 жыл бұрын
Very useful video...Thank you
@omersagsoz9261
@omersagsoz9261 9 жыл бұрын
do you have any tutorials, which contain the same topic but has pointers instead of references (e.g. const and pointers and functions)
@williamchamberlain2263
@williamchamberlain2263 6 жыл бұрын
Nice summary
@BrianVandrian
@BrianVandrian 9 жыл бұрын
Thanx I was trying to understand when to use and not use consts this weekend, so I made every conceivable function I could think of (return parameters and input parameters and I made this error 3:20 nd was curious about it). I'm going through templates and they are explaining them using const (Bleh...)
@AmCanTech
@AmCanTech 2 жыл бұрын
Thank you so much!!
@aakupsp
@aakupsp 8 жыл бұрын
Why we can change pointer member variables inside a const function?
@ninakoch1799
@ninakoch1799 7 жыл бұрын
i love youuu! you saved my life!
@Bopek
@Bopek Жыл бұрын
genius, thank you
@aviadshalom66
@aviadshalom66 9 жыл бұрын
I LOVE YOU
@layssi
@layssi 6 жыл бұрын
Best explanation ever
@slowthinker369
@slowthinker369 10 жыл бұрын
Great tutorials. Subscribed.
@priyanshipatel7804
@priyanshipatel7804 7 жыл бұрын
I want to ask,which one is better ide ,codeblocks or visual studio?
@Kromush1995
@Kromush1995 6 жыл бұрын
Got a question: when is good practice to return by const &? I know that by returning as const & you avoid calling the copy constructor on the object you return but also I've heard that it makes it thread unsafe, which is a little unclear to me why.
@gunjan94_
@gunjan94_ 4 жыл бұрын
If we return a reference to an object and the caller keeps the reference too long - meaning the object may clear up or it gets changed before the caller uses it - this can lead to undefined behaviour in your program or it can work perfectly well as well in case its intentional - depends on how you have designed the program. Note that even if you return copy of the object, you still need to make things thread safe incase multiple threads are involved - lock on the object whose member is being accessed.
@dmgkarthik
@dmgkarthik 9 жыл бұрын
can I invoke const functions from objects which are non constant ?
@Colonel1954Dz
@Colonel1954Dz 8 жыл бұрын
You can cast them when invoking the value: static_cast(d).printDogName(); // This would print dummyconst
@abdelaziz2788
@abdelaziz2788 3 жыл бұрын
where is the lvalue and rvalue video???
@foodlover3128
@foodlover3128 5 жыл бұрын
How you get this good in c++. You know everything
@riteshgupta5922
@riteshgupta5922 5 жыл бұрын
Hi Bo, I tried to get answer when which function get call setAge(const int& a) or setAge(int& a). I tried by passing const and non const value in both case setAge(const int& a) get called. However for the same case if have these function as global then const version called for const value and non-const one for non-const value. Please help me know why only const version get called for class and how it works if function are global?
@yogeshwalke1834
@yogeshwalke1834 4 жыл бұрын
If you pass rvalue or const value to the setAge function then it will call setAge (const int&a) . If you pass non const lvalue to SetAge function then it will call SetAge(int& a)
@zahidkhankhan
@zahidkhankhan 10 жыл бұрын
well explained. thanks for sharing. really appreciable..
@badmuthahubbard
@badmuthahubbard 4 жыл бұрын
Thank you for this. I have a question: Why bother passing a const reference as an argument? I mean, if you can't change it inside the function, why not just pass it by value?
@Bing.W
@Bing.W 4 жыл бұрын
because it can be faster with const ref, where you save the copying.
@spyrex3988
@spyrex3988 2 жыл бұрын
for optimization
@ligzer01
@ligzer01 9 жыл бұрын
keep up the good work!
@bougpan88
@bougpan88 8 жыл бұрын
What is the ide that you use to make this c++ programming ? Thank you very much
@nikolaikalashnikov4253
@nikolaikalashnikov4253 7 жыл бұрын
He's using Netbeans IDE in this video.
@bougpan88
@bougpan88 7 жыл бұрын
thank you very much
@mandaputtra
@mandaputtra 3 жыл бұрын
Thanks!
@ragnarw.eliansson5299
@ragnarw.eliansson5299 7 жыл бұрын
Very good!
@mini_frank
@mini_frank 7 жыл бұрын
const referenced parameter will be invoked whent he caller is a const? regular reference parameter will be invoked when caller is a non-const? I am not sure, someone let me know if I am wrong.
@mini_frank
@mini_frank 6 жыл бұрын
seems like const ref parameter will be invoked when setAge(5), else regular ref parameter function.
@alditopalli9530
@alditopalli9530 10 жыл бұрын
what compiler do you use i have visual studio 2012?
@madspace1234567
@madspace1234567 9 жыл бұрын
Code Art this is NetBeans on Windows w/ MinGW.
@BKJoe116
@BKJoe116 10 жыл бұрын
Could there be a const non-member function, or is it a feature for member functions only
@cobblin_gock
@cobblin_gock 10 жыл бұрын
any function can be const
@gavinlangley8411
@gavinlangley8411 8 жыл бұрын
+kayvan Mansoorshahi a const type qualifier is not valid on non member function the point is what is not going to change? all global variables?
@mshri3286
@mshri3286 11 жыл бұрын
what is the IDE or editor being used ?
@SAURAV1403406SAXENA
@SAURAV1403406SAXENA 6 жыл бұрын
age and name defaults to private right?
@yonizerbib9934
@yonizerbib9934 Жыл бұрын
thanks!!
@UltraAnalisis
@UltraAnalisis 8 жыл бұрын
your voice sounds a bit difficult to understand, but you talk slow, that is good; thanks good video.
@maciejkowalski3019
@maciejkowalski3019 7 жыл бұрын
Are you a native speaker? I come from Poland and I have no problems with his pronunciation.
@saturn7_dev
@saturn7_dev Жыл бұрын
OMG - now I can see why I hate C++ so much. Its all so unnecessarily complex and confusing. I prefer to spend my brain energy on using simpler code that just works rather than on all these small details that can easily stuff you up - limiting how much actual useable code you write.
@huyvole9724
@huyvole9724 6 жыл бұрын
what relative beween double setAge and getName??
@martinnieva8484
@martinnieva8484 6 жыл бұрын
What? Be more specific with your question.
@williamchamberlain2263
@williamchamberlain2263 6 жыл бұрын
4:05 , 5:35,6:25,6:55
@yonizerbib9934
@yonizerbib9934 Жыл бұрын
so, a class attribut is never const ?
@jamesrockford2626
@jamesrockford2626 9 жыл бұрын
very nice
@junjieyue7281
@junjieyue7281 6 ай бұрын
博主你好,你是中国人嘛,我最近也在学c++可不可以加个微信什么的,想向你学习一下😁
@AkhileshPratapShahi
@AkhileshPratapShahi 8 жыл бұрын
const function..... please do repeat once more
@sortysciaofiscia
@sortysciaofiscia 10 жыл бұрын
my mind: ............BLOOOWNN
@vlad_serg
@vlad_serg 9 жыл бұрын
sortysciaofiscia Your mind have not seen rValue-reference yet)
@carlosamaral6917
@carlosamaral6917 4 жыл бұрын
Many people says wonders about this channel, but actually I find it quite confusing and not that helpful for a beginner! There are far better options around!
@SecurityManager
@SecurityManager 6 жыл бұрын
const int& a == int a ?
@ldxyz-s1e
@ldxyz-s1e 7 жыл бұрын
3:12 6:33 7:03
@delpacho
@delpacho 7 жыл бұрын
where is the answer to the question???
@RohitKumar-kr7ce
@RohitKumar-kr7ce 6 жыл бұрын
d.setAge(static_cast(5)); will call void setAge(int& a) because 5 is casted into rvalue using && operator.
@azadalishah2966
@azadalishah2966 6 жыл бұрын
5 is already rvalue, no need to cast
@azadalishah2966
@azadalishah2966 6 жыл бұрын
setAge(40) will call setAge(const int& a) because literal 40 is rvalue and literal rvalue can’t be assigned to non-const lvalue reference.
@ShivaPrasad-hm5lk
@ShivaPrasad-hm5lk 2 жыл бұрын
9yrs 😯
@ApoorvSaxena8
@ApoorvSaxena8 3 жыл бұрын
very usfeull
@DanHartwigMusic
@DanHartwigMusic 8 жыл бұрын
love your videos! but you name your dog dummy.... 🙁
@BoQianTheProgrammer
@BoQianTheProgrammer 11 жыл бұрын
Netbeans.
@jonnymcgrath4816
@jonnymcgrath4816 Жыл бұрын
Evil way to write codes
@deborah_chrysoprase
@deborah_chrysoprase 9 жыл бұрын
why give your dog such a mean name :(
@virtuosisimo
@virtuosisimo 6 жыл бұрын
dogs are mean
@kejianshi9196
@kejianshi9196 4 жыл бұрын
one of the conventions those old unix/bell lab people use. dummy variable. It's like _ in python : for _ in list
@lisamichalski6402
@lisamichalski6402 5 жыл бұрын
Why is your name Lisa Jones Because I’m not a man
@rohitalawadhi
@rohitalawadhi 7 жыл бұрын
I love you
@tianyuez
@tianyuez 6 жыл бұрын
1.5x speed is good
Advanced C++: Logic Constness and Bitwise Constness
6:07
Bo Qian
Рет қаралды 68 М.
Advanced C++: Understanding rvalue and lvalue
12:06
Bo Qian
Рет қаралды 140 М.
Леон киллер и Оля Полякова 😹
00:42
Канал Смеха
Рет қаралды 4,7 МЛН
Mom Hack for Cooking Solo with a Little One! 🍳👶
00:15
5-Minute Crafts HOUSE
Рет қаралды 23 МЛН
СИНИЙ ИНЕЙ УЖЕ ВЫШЕЛ!❄️
01:01
DO$HIK
Рет қаралды 3,3 МЛН
REAL or FAKE? #beatbox #tiktok
01:03
BeatboxJCOP
Рет қаралды 18 МЛН
do you know how "return" works under the hood? (are you SURE?)
5:08
CONST in C++
12:54
The Cherno
Рет қаралды 418 М.
Should I pass by const reference or by value?
10:45
The Cherno
Рет қаралды 114 М.
Const correctness in C++
10:02
Code for yourself
Рет қаралды 3,9 М.
you will never ask about pointers again after watching this video
8:03
C++ 11: Rvalue Reference -- Move Semantics
14:22
Bo Qian
Рет қаралды 151 М.
Lambdas in C++
11:54
The Cherno
Рет қаралды 353 М.
WHY IS THE STACK SO FAST?
13:46
Core Dumped
Рет қаралды 198 М.
Advanced C++: Disallow Functions
10:09
Bo Qian
Рет қаралды 51 М.
Леон киллер и Оля Полякова 😹
00:42
Канал Смеха
Рет қаралды 4,7 МЛН