Тёмный

CONST in C++ 

The Cherno
Подписаться 661 тыс.
Просмотров 405 тыс.
50% 1

Опубликовано:

 

27 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 559   
@jean-naymar602
@jean-naymar602 7 лет назад
For people having trouble remembering the order in which const keyword is to be used, here's a quick tip. You have to read it backward, like the compiler does. For instance : -const int * A; ==> "A is a pointer to an int that is constant." (or, depending on how you prefer to write it) int const* A; ==> "A is a pointer to a const int" but both are the same as explained in the video. -int * const A; ==> "A is a const pointer to an int." -const int* const A; ==> "A is a const pointer to an int that is constant". EDIT : As mentionned by simo simo in the comments bellow, const int * A does not mean that A actually points to a const variable. It just means that the compiler will not allow you to modify the pointed value *through* A. for instance : int val = 10; int const * a = &val; *a = 30; //this will NOT compile, you're not allowed to modify "val" through "a". val = 30; //this will compile. Same with : int val = 10; int val2 = 30; const int * const A = &val; A = &val2; //will NOT compile : you can't modify what A points to. *A = 30; //will NOT compile : you can't modify val through A val = 30; //this will compile, val is not constant
@h.hristov
@h.hristov 7 лет назад
Jean-Nay Mar Awesome tip. Thanks :)
@afterbunny257
@afterbunny257 6 лет назад
lol, it works out perfectly!
@TheBellite
@TheBellite 6 лет назад
Great tip! Thanks.
@kmattrichards64
@kmattrichards64 5 лет назад
this has to be the best explanation, awesome!
@suryarocks8647
@suryarocks8647 5 лет назад
Thanks
@inserteunnombreapropiado9079
@inserteunnombreapropiado9079 3 года назад
More simply: a _const_ is like a: "if I modify this, please give an error calling me an idiot because I modified a _const_ ".
@yashbhatt4498
@yashbhatt4498 3 месяца назад
Const can be modified but in terms of constant there is no such a thing cuz it's literal...
@KeybladeMasterCam
@KeybladeMasterCam 4 года назад
The C in C++ stands for Cherno. I'm convinced.
@chhayanksharma3926
@chhayanksharma3926 3 года назад
you mean Charno?
@victormamede7004
@victormamede7004 3 года назад
What about the C in C?
@chhayanksharma3926
@chhayanksharma3926 3 года назад
@@victormamede7004 The C in C stands for C++
@chhayanksharma3926
@chhayanksharma3926 3 года назад
@@AbhishekBM it stands for C
@Brahvim
@Brahvim 2 года назад
@@chhayanksharma3926 *C -Sorry, shouldn't question a joke :joy:-
@sroentoel
@sroentoel 6 лет назад
Bucky teaches the cpp fundamental level, you teach the advanced one. Brilliant guys on RU-vid
@fburnrostro95
@fburnrostro95 6 лет назад
ah @thenewboston is indeed ol' reliable
@alexandergonzalez5975
@alexandergonzalez5975 6 лет назад
sroentoel Is Bucky coming back?
@rajesh09ful
@rajesh09ful 5 лет назад
I Second that, also feel Master Bo Qian need a mention here, most def
@rajesh09ful
@rajesh09ful 5 лет назад
I Second that, also feel Master Bo Qian need a mention here, most def
@MsJavaWolf
@MsJavaWolf 4 года назад
@@rajesh09ful Yes his videos are great. I think he stopped uploading sadly.
@nahuelarjona
@nahuelarjona 4 года назад
Funny short story, demonstrating all acquired Cherno-skills: I went ahead and compiled the code showed on 2:53. I wanted to see *if I could force a change to the const MAX_AGE using the a pointer* . *The code:* _const int MAX_AGE = 90;_ _int* a = new int;_ _a = (int*)&MAX_AGE;_ _*a = 85;_ _std::cout
@thanostitan.infinity
@thanostitan.infinity 4 года назад
wow! that's amazing seriously! I had this doubt too, and was thinking and playing with it to understand it. So double wow and thanks haha.
@spyrex3988
@spyrex3988 2 года назад
its been 2 years but thats crazy
@swaw11
@swaw11 Год назад
Thanks! I did the same and was confused!
@BinGanzLieb
@BinGanzLieb Год назад
puh thats is crazy and error-prone for beginners. i have to keep that in mind, thank you
@rayysw
@rayysw 3 года назад
"const is basically sort of like a promise that you give, in which you promised that something will be constant, that is it's not going to change. However, it's just a promise and you can bypass that whole 'promise', and you can break your promise just like you can in real life." I came here to learn more about C++ but I get legit life lesson instead...
@serkanozturk4217
@serkanozturk4217 2 года назад
Personal notes: - reading backwards helps understanding what is constant - making a class method constant says that that method will not change any of the class member - defining a function outside of the class and passing class instance as a constant reference makes sure that the instance won’t change. Thus, any method in that function must be const, otherwise you get error
@TheJorge100
@TheJorge100 5 лет назад
For anyone still struggling. A good way to read these statements is from right to left. Example: const int * (pointer to an int constant), int const * (pointer to a constant int), int * const (constant pointer to an int), const int* const (constant pointer to a constant integer)
@ehsan18t
@ehsan18t 3 года назад
Man! I never understand this const properly. I learned everything I could find in books, lectures and other c & c++ videos on RU-vid. But this is the only video that solved all of my confusion on const. Thanks a lot man.
@HornyVegan
@HornyVegan 7 лет назад
Your C++ vids are the best on youtube, don't stop ^^
@websurfer5283
@websurfer5283 6 лет назад
If people like, please visit his patreon page. I will certainly be contributing come 1st of next month. As I think u get a months benefit from start of each month.
@jeremymesloh1981
@jeremymesloh1981 4 года назад
Agreed!
@mjthebest7294
@mjthebest7294 4 года назад
Also check ChiliTomatoNoodle, he is impressive as well. :)
@rudiger86
@rudiger86 2 года назад
Can’t stop, won’t stop
@Aragubas
@Aragubas Год назад
true x3
@soniablanche5672
@soniablanche5672 4 года назад
Every programming language ever: let's create a "constant" array ... then change the values inside it :^)
@thomascreel6522
@thomascreel6522 3 года назад
the size is constant
@cvbattum
@cvbattum 3 года назад
@@thomascreel6522 That's true for all arrays. As soon as you change the size, you reassign it with a different array, so it's no longer the same array with the old size. Now, of course, const prevents that and only that.
@thomascreel6522
@thomascreel6522 3 года назад
@@cvbattum No, the number of elements in the array is immutable and you must create a new array of a different size and move the old array elements into it.
@AlFredo-sx2yy
@AlFredo-sx2yy 2 года назад
@@thomascreel6522 uh... that's exaclty what the other guy said tho...
@rafaelmenna8384
@rafaelmenna8384 2 года назад
That’s why we have (tuples) in python.
@parth46767
@parth46767 5 лет назад
"Now let's put const everywhere"
@786balgoo
@786balgoo 4 года назад
You saved my life cherno... I love your tutorials and I would love it even more if you make 2d game tutorials in c++.
@LucidStew
@LucidStew 7 лет назад
Learned a few things, and this clarified my thinking on const greatly, so thank you, sir.
@4wen_main
@4wen_main 8 месяцев назад
These kind of details will take us countless hours to grasp from reading a book (with omission often), this is amazing.
@gorilla543210
@gorilla543210 5 лет назад
/* my note of CONST Use for 3 cases */ 1. const int* a => can't modify the value store in a (address) 2. int* const a => can't modify the address which a points to 3.use in member function which promise not to modify the data member of the class => add const to all member functions which don't modify data members , since it might be const reference! p.s. add "mutable" if you want to break the promise wrap up const int* const Getter() consr {}
@siAppDesign
@siAppDesign 4 года назад
Nice summary Thank you
@pixarfilmz4769
@pixarfilmz4769 6 лет назад
This is how I see c++: const *int &int const ***const const *** &const
@MsJavaWolf
@MsJavaWolf 6 лет назад
We had something like that as questions in university, but every reasonable programmer avoids stuff like that.
@angelstojanov2346
@angelstojanov2346 6 лет назад
Same here, when I studied C I had a test with an expression full of illogical pointers
@bombrman1994
@bombrman1994 6 лет назад
schools teach and test programming using the worse method and discouraging students so much. In my C++ course i thought about droping the class so many times bcuz of my mark, on the other side i have good understanding on C++. It never makes sense bcuz schools care for mark and never for the diamond someone got in his brain
@haroldfinch360
@haroldfinch360 6 лет назад
If you have a good understanding of C++, how can your grades be bad then?
@bombrman1994
@bombrman1994 6 лет назад
Harold Finch because when my mother language is not English and the question in a test is a paragraph long asking to write a single line code. That’s when I start making mistakes. Programming instructors are not often found in every school
@calecacciatore5422
@calecacciatore5422 6 лет назад
dont forget that const makes a variable local to the file. that is, only visible in the actual translation unit. you can have 2 const variables with the same name in different files and it will compile fine. so basically they re implicitly static.
@Prashantkumar-pn6qq
@Prashantkumar-pn6qq 3 года назад
03:28 "Now let's start adding const everywhere" - Cherno be like - let the games begin baby😂
@RoaiDude1
@RoaiDude1 6 лет назад
You are seriously the best code teacher I've seen and I bought something like fifty udemy courses.. keep the good work and combining assembly and c++ together! Its great and im learning alot even as a long time java programmer! Thank you a lot!!!
@rhyscabonita5933
@rhyscabonita5933 5 лет назад
As someone who's only approaching intermediate: Ahhhh.... Wait, holy. hell. what?!?.... Okay.. What.
@Deadshot-fz5gx
@Deadshot-fz5gx 4 года назад
I feel that bro
@MirrorsEdgeGamer01
@MirrorsEdgeGamer01 4 года назад
@@Deadshot-fz5gx Me too.
@unstablethermalpaste3366
@unstablethermalpaste3366 2 года назад
You are the best one I found so far with a clear eng accent that is able to help me learn C++. No offense to other RU-vidrs with strong accents.
@theOmKumar
@theOmKumar 2 года назад
***EASY MENTAL MODEL TO PERMANENTLY REMEMBER THESE*** think of '*' as boundary, whatever is in contact with const is meant to be constant eg1: int const *ptr -> int const |*| ptr eg2: const int *ptr -> const int |*| ptr -> const is in contact with int only, and ptr is separated thanks to boundry '*', this means: 1. we can't modify int values 2. we can modify pointer eg3: int * const ptr; -> int |*| const ptr -> const is in contact with ptr while int is separated, this means: 1. we can't modify pointer. 2. we can modify int values. Also You can read backwards as suggested by @Jean-Nay Mar
@manonthedollar
@manonthedollar 3 года назад
Thank you for all your videos! I just finished working through a C++ book, and I'm at the point where I try to make my first "thing," and I go "uh oh I still don't know anything." These videos are excellent for solidifying my understanding of things that I remember reading about, but do not yet have an instinct to know if I'm using them correctly yet in my own work.
@karmaindustrie
@karmaindustrie 5 лет назад
const bool confusion = true;
@kushnayak1619
@kushnayak1619 4 года назад
ok
@karmaindustrie
@karmaindustrie 4 года назад
@@kushnayak1619 I want to know the mindset that led you to write "ok" and I also want to know the mindset that led somebody to give you a like after at most 8 hours. Because I want to benefit from the law of attraction fully.
@kushnayak1619
@kushnayak1619 4 года назад
@@karmaindustrie ok
@karmaindustrie
@karmaindustrie 4 года назад
@@kushnayak1619 So I guess the answer is: You don't really want to benefit from the law of attraction. What a pity. I want to inform you that I am very cool.
@karmaindustrie
@karmaindustrie 4 года назад
@@kushnayak1619 And also, to all the other people: It looks like you are liking your own comments because you got one like again after 23 minutes. This feels like a psychological disease to me, right now :) - Correct me if I'm wrong. Plausible. Try.
@10bokaj
@10bokaj 7 лет назад
dud, your knowledge exceeds the physical barrier of the realm
@zuhail339
@zuhail339 4 года назад
I really like how he does sarcasm in the middle of a serious lesson ! But I guess that's how it rolls😂
@more_than_just_sentient
@more_than_just_sentient 4 года назад
Hey, I saw just two videos and clicked the sub button , your videos are really high quality and I left with a lot of answers than questions when I finish watching . Keep up the hard work . Cheers .
@mikeorioles
@mikeorioles Год назад
The best video on the const keyword, and it's not even close. Good stuff!
@hpeterh
@hpeterh 2 года назад
Pretty clear, thank you. It should be noted, if a global variable is declared as "const" or a variable is declared "static const", then it is placed by the linker into a non writable CODE or CONST segment. When it is written to by some pointer trickery, an access violation exception will probably happen.
@daniil9263
@daniil9263 Год назад
since people are still watching/commenting. A great example why const on member functions should be used. map[ ] operator (as in dict["key"] = value. it's sometimes (depending on the implementation of map) not const. So this dict["kye"] = value would compile and work, until it doesn't because you just inserted >value< at "kye". If you don't know the framework in&out, be const correct. This particular thing was taken from the yt-video: c++ bugs at facebook.
@mkhadka123
@mkhadka123 Год назад
you are a reason I fell in love with c++ language again, your c++ videos are simply awesome :)
@Ginger_Hrn
@Ginger_Hrn 5 месяцев назад
Hello for my future self, I know I'll be watching this many times in the future
@rcookie5128
@rcookie5128 7 лет назад
daym, so much to take care of when working with const stuff..
@ericrussell5304
@ericrussell5304 7 лет назад
In some ways the opposite is true. If you get const right in your program, you'll have to worry less about what you do with your objects. Without const it would be like having a bunch of global variables that might get changed without warning. With const, I can give you an const object and not worry about you changing it...like a map's key.
@robertmoats1890
@robertmoats1890 Год назад
"If you want both members to be pointers, you have to put the asterisk before every member" - this is why every programmer should immediately adopt my style of placing pointer asterisks on the name side instead of the type side! Then it works perfectly when you define multiple pointers on one line. Well, until you try to define a function that returns a pointer, then your asterisk doesn't know where it wants to be. "We still need to mark the method as const, but we just want to touch this variable" - a good example of when this pops up a lot is in "on demand" situations, such as load-on-demand, where an object loads its data in the background the first time it is required. In such a case, it is usually necessary to modify the object from a const situation, even though the method itself is not really the purpose of the modification, just the reason it is needed.
@reflectiveradiosity6664
@reflectiveradiosity6664 5 лет назад
Was really confused and this made it a whole bunch clearer, again your videos are straight to the point, and relatively easy to follow! Thanks!
@alecmather
@alecmather Год назад
Literally amazing video, just answered all my questions about const (and more) in 12 minutes, you're a GANGSTER
@ashiinsane90
@ashiinsane90 7 лет назад
bro your channel is a gold mine i just started learning C++ also new to programming but your vids help alot. would love to see u do games like asteroids and snake etc with C++
@romanprykhodchenko1551
@romanprykhodchenko1551 4 года назад
You are the best!!! Really structured and easy to understand lessons!) Very useful for any kind of the projects) Thanks bro!
@GfastGao
@GfastGao 5 лет назад
Man, I start to think if I really should pledge through patreon to this series, because you are the correct one for me. INDEED.
@xwaresharex
@xwaresharex 4 года назад
You really did make me understand what const's are and how do they work, have been trying to find answer to it from so many websites and videos I can't believe I now understood it all. Really thank you so much!!
@tyroneslothdrop9155
@tyroneslothdrop9155 6 лет назад
Thank you for using a dark theme with your coding examples. It makes a massive difference.
@Weredah
@Weredah 7 лет назад
Thank you so much for clearing up const's I really appreciate this series :D
@piyushphodu
@piyushphodu 3 года назад
this video is the best const in C++ explanation till date i ever watched
@HappyMatt12345
@HappyMatt12345 Год назад
So if I understand it right, writing const before a pointer means the data at that pointer is cannot be modified, after a pointer means the pointer itself is constant but the data at that memory address can be rewritten, and both before and after makes that pointer completely read-only, you cannot redefine the pointer itself nor write to the data at the memory address it points to, that's pretty useful to know. As an experiment I wrote a # define (had to put a space between # and define to keep RU-vid from treating it as a hashtag) to see if I have a solid understanding of it, if anyone is curious here's what I wrote: # define readonlyptr(T) const T* const
@filiperei6055
@filiperei6055 4 года назад
Question: How come we can change a const char* value? And don't even have to de-refference it? const char* example = "Some text."; example = "Some different text."; //didn't have to type * before example, and it allows us to change the value despite being an array of const chars? Thanks for all your videos! You're the best teacher I've come across on these subjects!
@handover007
@handover007 4 года назад
You are the best tutor on the planet .
@celestialmaat9462
@celestialmaat9462 2 года назад
I spent like 20 minutes and then looked you up🤯🤯🤯! Thanks
@vitaliipaprotskyi3815
@vitaliipaprotskyi3815 4 года назад
const applies to the thing left of it. If there is nothing on the left then it applies to the thing right of it. Object * const obj; // read right-to-left: const pointer to Object Object const * obj; // read right-to-left: pointer to const Object Object const * const obj; // read right-to-left: const pointer to const Object
@greggas87
@greggas87 4 года назад
"One thing I'll point out, just quickly - haha 'point out' ... " .. Had to laugh here
@lewisb8634
@lewisb8634 7 лет назад
Fantastic video Cherno as usual, thank-you for uploading! What is your opinion on using const everywhere possible? I came across big flame war on StackOverflow recently between people that supported the 'security' and 'consistency' of using const wherever physically possible and those that said it was an eyesore. We're talking about using const literally everywhere - a simple 2 line function that takes in one integer to use in some quick calculation? It must take in a const int because the value is not modified. Would you say it is an eyesore to have it everywhere or do you think there is some benefit?
@jimmylander2089
@jimmylander2089 4 года назад
I keep coming back to this video because I always forget what const means depending on its position and it's so ANNOYIIIIIINNNNGGGG
@nedboulter9187
@nedboulter9187 7 лет назад
Really nice tutorial, I already know C++ and I just ran across this, but it still cleared a couple of things up, in terms of the mutable keyword at whatnot. I will probably keep watching these in case there is anything else I missed :) :)!!
@muneebj.4010
@muneebj.4010 3 года назад
if you are planning to work with classes, assigning objects to one another and "return" object types then the CONST keyword is one of the few things you must know. Some places where you NEED to use const copy constructor, code: {className(const className &object); } assignment =("is equal to" symbol) overloaded operator, code: {void operator=(const className &object); }
@Impulse_Photography
@Impulse_Photography 4 года назад
You should make some videos on specific Design Patterns, in particular (for me) the Strategy Pattern in C++. Maybe, recommend some books for learning the main Design Patterns every programmer should know ...
@XpressCrosSs
@XpressCrosSs 4 года назад
it is often optimized into an immediate operand in instructions and stored in text segment
@Katniss218
@Katniss218 4 года назад
The question is why in C++ you generally put * (pointer token) next to the identifier instead of next to the type. It would make more sense to me if it was placed next to the type.
@RushilKasetty
@RushilKasetty 4 года назад
Luckily you can do it either way. I always put it next to the type because it makes more sense to me.
@Katniss218
@Katniss218 4 года назад
@@RushilKasetty Same. I always wonder why most people do it the other way around.
@givup_
@givup_ 4 года назад
@@Katniss218 One reason is that if you define multiple variables on the same line, like: "int* a, b;" , b is actually not a pointer. So by declaring your pointers like: "int *a, *b;" it makes it easier not to make this mistake, and the first example can be a bit misleading, especially for a beginner. Of course you could just declare your variables on a one-per-line basis.
@Katniss218
@Katniss218 4 года назад
@@givup_ And that's really stupid and unintuitive design. If I were designing the C++ specs, I'd do it so that both a and b would become pointers. And if you don't want that, just initialize each variable on it's own.
@Katniss218
@Katniss218 4 года назад
@Peterolen I wonder why it was the standard in C then. It's *very* counter-intuitive to beginners and makes the code less readable (imo). I'm glad I'm not forced to use it.
@danielc4267
@danielc4267 6 лет назад
const video* const Upload() const {} Cherno, please upload videos constantly and make it an immutable fact. :D
@aubreymatende6497
@aubreymatende6497 4 года назад
im loving these videos, i am asking if you can make a video on friend functions
@hts2370
@hts2370 3 года назад
very clear explanation
@can3792
@can3792 2 года назад
i am on the patreon and donated . i cant thank you enough. please carry on taking video in c++ :)😀 keep up the good work
@sri95
@sri95 2 года назад
Thanks for clearly explaining ~10 contradicting concepts in 10 min ...
@ameerhamza8300
@ameerhamza8300 4 года назад
in a function, when should we pass as pointer and when should we pass as reference? For example the PrintEntity function could have argument (Entity *e) or (Entity &e) what should be used when
@petrkassadinovich2705
@petrkassadinovich2705 6 лет назад
Thank you Yan for your tutorials! PS: now I can understand: const int* const Method(const int& const p1) const;
@SimonK91
@SimonK91 3 года назад
Only problem is that references are already "hard pointers", so "const int& *_const_* p1" doesn't work (2+ years old comment, and 4+ year old video, though for some reason I ended up here)
@marius2k8
@marius2k8 5 лет назад
//QUESTION: (THIS SEEMS TO BE A BUG IN VS/VisC++) // Where in the hell are the two different // numbers being stored? const int j = 100; //int *q = &j; //Compiler disallows int *q = (int*)&j; //By some magic, now allowed *q = 300; //After this line, j = 300 in debugger cout
@marius2k8
@marius2k8 5 лет назад
And it does this when I build a release and run it externally, so it's not the debugger holding on to something for me...
@marius2k8
@marius2k8 5 лет назад
Like, does the C++ compiler keep a const table separate from the values of the variables themselves, or something?
@thomasmathews4592
@thomasmathews4592 6 лет назад
Great video. Could you consider doing constexpr as well?
@sachinnair8375
@sachinnair8375 4 года назад
Thanks man ..... your tutorials are very clear to follow.✌️
@danielc4267
@danielc4267 5 лет назад
Remember, Cherno's promise to put links at the video corners is a just CONST :P
@1Naif
@1Naif 7 лет назад
*You the best.*
@user-em9mw9ch3y
@user-em9mw9ch3y 6 лет назад
[Error] Expected " 're " after the string "You"
@ninjacat7580
@ninjacat7580 6 лет назад
Maybe you can use the insert function to fix it ^_^
@apenasmeucanal5984
@apenasmeucanal5984 4 года назад
i appreciate ya
@mohamadelmahdihoumani4239
@mohamadelmahdihoumani4239 3 года назад
Can you make the video on how you type this fast in VS? Amazing content btw !
@Garbaz
@Garbaz 5 лет назад
0:16 Why doesn't the compiler use the fact that a variable is const for optimization? This would only work if it weren't a promise of course. An example that could be optimized: const int SCALAR = 90; int x = 2 * SCALAR * y; If the compiler knows that SCALAR is constant, it could calculate `2 * SCALAR` at compile time and would only have to do one multiplication at runtime. Or does it do that already, just by being clever and recognizing constant variables even without the const keyword?
@PANCHO7532
@PANCHO7532 2 года назад
at 2:50 why you cast into (int*) instead of (int)? I mean sure, it doesn't compile when using (int) but *why* do we need or what's the reason of casting into an int*?
@YektaSarioglu
@YektaSarioglu 5 лет назад
Simple and clear explanation. Keep up the good work, friend 👍
@PythonisLove
@PythonisLove 2 года назад
awesome explanation
@SahilKumar-ni7su
@SahilKumar-ni7su 4 года назад
new subscriber from india amazing teaching technique bro keep it up
@udayyadav3489
@udayyadav3489 5 лет назад
who the hell are these 14 people to dislike such an amazing video...........
@IskeletuBr
@IskeletuBr 2 года назад
Now I still have a question, why would I want to use const in the first place? Like, does the compiled code gets any faster, or is it just a restriction to myself?
@rjle2446
@rjle2446 Год назад
Super helpful. Thank you!
@danielketcheson1965
@danielketcheson1965 2 года назад
Before video:🙄🤕 During video:🤔🤯🥰 After video:🧐
@raymondyoo5461
@raymondyoo5461 6 лет назад
What a clarity I found here :) Thank you soooooo much!
@arihanttonage7436
@arihanttonage7436 3 года назад
Best explanation
@cajintexas7751
@cajintexas7751 2 года назад
I played around with this. With just const int MAX_AGE and int* a, I don't understand why I can assign the address of MAX_AGE to a, then modify the contents of a, and printing both a and &MAX_AGE shows they are equal, yet the contents of a no longer equal MAX_AGE. On the other hand, *a and *&MAX_AGE both equal the new contents of a. It makes my head swim.
@czarcarlo5459
@czarcarlo5459 4 года назад
Hi, at 3:18 I thought I would be able to change the value inside MAX_AGE if we get a to point to it i.e. a = (int*)&MAX_AGE; *a = 10; std::cout
@thanostitan.infinity
@thanostitan.infinity 4 года назад
True, I have the same question. If we do cout
@czarcarlo5459
@czarcarlo5459 4 года назад
@@thanostitan.infinity no I have not found the answer - still searching for the truth
@thanostitan.infinity
@thanostitan.infinity 4 года назад
@@czarcarlo5459 actually I found the answer yesterday sir. What's happening is, Use your debugging feature and breakpoints, you'll see after the the pointers value is changed, the value of memory of MAX_AGE is changed too. Means we were successful, and the memory which contains MAX_AGE has been changed, yet we continue to get same result. Why?? Because, if you check the assebmly code, you'll notice that after program was compiled, the compiler replaced the MAX_AGE everywhere with the value 90 itself, during compile time itself to not keep extra variable. Hence cout
@czarcarlo5459
@czarcarlo5459 4 года назад
@@thanostitan.infinity Ah dude, Thanks man. I found that If I do this in C printf-ting it displays the right value after modifying So at the end its the C++ compiler being too clever? Is this a bug?
@jiakaili173
@jiakaili173 2 года назад
Can we have a video on constexpr as well?
@Popart-xh2fd
@Popart-xh2fd 2 года назад
9:50 I'm confuse, if by using a reference I can't change the contents of the class variables then what about the talk of using references instead of pointers to change variable contents? Object references can't call any function of the respective class whenever a variable of that class is changed by that function, is that it?
@anthonysteinerv
@anthonysteinerv 3 года назад
if my member is a pointer, should I always write the getter as const type* const getMember() const{ return member;} ? or with const befor {} is enough?
@pararera6394
@pararera6394 2 года назад
Few things, for embedded. const char* str = "Hello World"; will place those 12 bytes in ROM while char* str = "Hello World"; will place those bytes in ROM and RAM. Btw. I don't get it why I cannot call const func from class if it reference isn't const.
@Zentamusic
@Zentamusic 4 года назад
soooo helpful man thanks
@informativecontent4778
@informativecontent4778 6 лет назад
man o man you are awesome and most the programming i learnt is beacuse of you and buckey
@AmCanTech
@AmCanTech 2 года назад
int *myX; const int * const getX() const{return myX;} 1 - return (pointer cant be modified) 2 - contents of pointer cannot be modified 3 - getX not modify actual class
@2k23_hovercars7
@2k23_hovercars7 2 года назад
Cnt0=Obj1.Date(); Cnt1=Numbers Cnt2=Count of Count
@Vectoradc
@Vectoradc 2 года назад
Love how I don't even study coding but I somehow make it to the past half now
@IshakHeor
@IshakHeor 3 года назад
One of the best teachers ever!!!
@tezza48
@tezza48 7 лет назад
I was reading Const in the C++ Primer, confused the hell out of me when it got to the complex stuff.
@vtgordo
@vtgordo Год назад
Need help - using VS2019 - when Cherno enters code that will not compile, he gets a 'squiggly' under the error right away - how do I set that up?
@abdelrhmanahmed1378
@abdelrhmanahmed1378 4 года назад
i used int f=10; int *p; const int*const get() const { *p = 10; cout
@vinniciusrosa8284
@vinniciusrosa8284 4 года назад
THANK YOU SO MUCH
@Impulse_Photography
@Impulse_Photography 5 лет назад
I thought when you assign a pointer to some value that the copiler sets aside memory for both the pointer and the actual thing being pointed to. If I reassign the pointer and later delete it then i still have 'that original value' still dangling out there somewhere. I was told, it is best to assign a pointer and delete it then recreate and assign again. ... or just use two pointers for as long as you need them.
@chrischauhan1649
@chrischauhan1649 5 лет назад
Awesome video. By the way which IDE you are using in this video??
@rajcodes100
@rajcodes100 5 лет назад
Great Video thanks - I learnt some new stuff here .
Далее
The Mutable Keyword in C++
6:56
Просмотров 173 тыс.
Stack vs Heap Memory in C++
19:31
Просмотров 569 тыс.
11 ming dollarlik uzum
00:43
Просмотров 279 тыс.
Se las dejo ahí.
00:10
Просмотров 661 тыс.
Watermelon magic box! #shorts by Leisi Crazy
00:20
C++ Super Optimization: 1000X Faster
15:33
Просмотров 320 тыс.
CONSTANTS in C++
8:31
Просмотров 27 тыс.
POINTERS in C++
16:59
Просмотров 1 млн
31 nooby C++ habits you need to ditch
16:18
Просмотров 787 тыс.
lvalues and rvalues in C++
14:13
Просмотров 315 тыс.
Templates in C++
17:58
Просмотров 592 тыс.