CONST in C++

  Рет қаралды 410,896

The Cherno

The Cherno

Күн бұрын

Пікірлер: 562
@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 6 жыл бұрын
this has to be the best explanation, awesome!
@suryarocks8647
@suryarocks8647 5 жыл бұрын
Thanks
@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)
@inserteunnombreapropiado9079
@inserteunnombreapropiado9079 4 жыл бұрын
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 5 ай бұрын
Const can be modified but in terms of constant there is no such a thing cuz it's literal...
@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
@KeybladeMasterCam
@KeybladeMasterCam 4 жыл бұрын
The C in C++ stands for Cherno. I'm convinced.
@chhayanksharma3926
@chhayanksharma3926 4 жыл бұрын
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:-
@basedworldsk8
@basedworldsk8 4 жыл бұрын
I'm taking a upper division c++ class at uni with a really old professor who can't teach.... you're saving my life
@nahuelarjona
@nahuelarjona 5 жыл бұрын
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
@vaishanthjv2519
@vaishanthjv2519 28 күн бұрын
I did the exact same thing and I had the same result but I didn't check any assembly files. Instead I took max_age in through std::cin so that its value isn't known at compile time(actually I had to input a temp variable and then assign that value to max_age because you cant directly std::cin a const variable) and then ran the program and the output was 90 90.
@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 KZbin. But this is the only video that solved all of my confusion on const. Thanks a lot man.
@sroentoel
@sroentoel 7 жыл бұрын
Bucky teaches the cpp fundamental level, you teach the advanced one. Brilliant guys on KZbin
@fburnrostro95
@fburnrostro95 6 жыл бұрын
ah @thenewboston is indeed ol' reliable
@alexandergonzalez5975
@alexandergonzalez5975 6 жыл бұрын
sroentoel Is Bucky coming back?
@rajesh09ful
@rajesh09ful 6 жыл бұрын
I Second that, also feel Master Bo Qian need a mention here, most def
@rajesh09ful
@rajesh09ful 6 жыл бұрын
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.
@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...
@4wen_main
@4wen_main 10 ай бұрын
These kind of details will take us countless hours to grasp from reading a book (with omission often), this is amazing.
@KishanKumar-mz3xr
@KishanKumar-mz3xr 4 жыл бұрын
Easy to remember like this: * const -> pointer(*) is constant(const) const * -> constant(const) value at pointer(*)
@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
@parth46767
@parth46767 5 жыл бұрын
"Now let's put const everywhere"
@yacinemathurin2013
@yacinemathurin2013 5 жыл бұрын
For people having trouble with the (const Entity& e), it's just that the function can't accept any instruction able to change the object (one of it's parameters) as you've set it const.
@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.
@pixarfilmz4769
@pixarfilmz4769 7 жыл бұрын
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
@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 KZbinrs with strong accents.
@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...
@knowsomething-b8d
@knowsomething-b8d 2 жыл бұрын
That’s why we have (tuples) in python.
@cexplr2431
@cexplr2431 5 жыл бұрын
"C++ man ... That's how it rolls.." HAAHAHAHAH
@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.
@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.
@Ginger_Hrn
@Ginger_Hrn 7 ай бұрын
Hello for my future self, I know I'll be watching this many times in the future
@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.
@piyushphodu
@piyushphodu 4 жыл бұрын
this video is the best const in C++ explanation till date i ever watched
@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
@AviPrakriti
@AviPrakriti 5 жыл бұрын
These videos are addictive. I am coding for long time and know most of it, still watching this playlist is so refreshing. I feel like exploring every details of C++ all over again and play with it. Great job @TheCherno! Your Game Engine series is also another gem engine! Big thanks
@YogeshSharma-hg4lx
@YogeshSharma-hg4lx 5 жыл бұрын
Geming engine lol
@DonceA
@DonceA 4 жыл бұрын
Here are 3 styles to create a pointer: 1) int* p1; 2) int *p2; 3) int * p3; Cherno uses first style, but I prefer third style and this video shows why I prefer it. 4) int* p1, p2; p1 will be pointer, but p2 will be just normal int variable. So it makes more sense to write it like this 5) int *p1, *p2; Now it looks more uniform. You say you will use int data, but at the same time you say that variables are pointers and don't store int value. But if you want to make pointer constant you need to write const keyword after asterisk. 6) int * const p1 = &var1, * const p2 = &var2; So if I use third style asterisk place are uniform for all variables, but first and second styles move asterisk around depending if you will declare multiple variables at same line and if you want to make pointer constant. 7) int const * const p1 = &var1;
@qidex
@qidex Жыл бұрын
I've been programming for years, but these details are things I largely didn't know. Now I hope I can improve my coding patterns accordingly, but still need to figure if I'm understanding everything correctly.
@furkanmacit4040
@furkanmacit4040 5 жыл бұрын
i have been searching c++ videos in several platforms for two weeks i think i have finally found a good one
@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.
@TW19567
@TW19567 Жыл бұрын
Absolutely the clearest explanation of const out there!
@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.
@MoolsDogTwoOfficial
@MoolsDogTwoOfficial Жыл бұрын
Note for people using C++20 or greater: There is also 'constexpr' for explicit compile time constants.
@virno69420
@virno69420 9 ай бұрын
🤓Ermm don't forget about consteval!
@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); }
@TheBellite
@TheBellite 6 жыл бұрын
Gold! As a longtime hobbyist programmer who's new to C++, these videos are amazingly helpful.
@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.
@tyroneslothdrop9155
@tyroneslothdrop9155 6 жыл бұрын
Thank you for using a dark theme with your coding examples. It makes a massive difference.
@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!!!
@mkhadka123
@mkhadka123 Жыл бұрын
you are a reason I fell in love with c++ language again, your c++ videos are simply awesome :)
@10bokaj
@10bokaj 7 жыл бұрын
dud, your knowledge exceeds the physical barrier of the realm
@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.
@shvideo1
@shvideo1 3 жыл бұрын
Comprehensive and to the point. A great tutorial. Crystal clear. Thanks a lot for your effort.
@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 KZbin 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
@0x_Anakin
@0x_Anakin 5 жыл бұрын
You have the best c++ tutorials in youtube dude
@zuhail339
@zuhail339 5 жыл бұрын
I really like how he does sarcasm in the middle of a serious lesson ! But I guess that's how it rolls😂
@handover007
@handover007 4 жыл бұрын
You are the best tutor on the planet .
@xwaresharex
@xwaresharex 5 жыл бұрын
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!!
@dhu1090
@dhu1090 3 жыл бұрын
You are so knowledgeable!! I don't know how I'd learn C++ without you!! 🙏🏻
@mytech6779
@mytech6779 7 жыл бұрын
Thinking way back to my short time with C. Constant was a term used when using the preprocessor directive #define to name literal values. The purpose was similar to why you might use a named function rather than writeing inline statements(only need to write it or change it in one place), the question is why not simply assign the value to a variable name. The given reason was that it helped produce faster running and more compact machine code because the compiler must consider that variables are potentially ... variable and mutable at run time while constant literals don't change at run time and can be more easily handled by the compiler in the optimizing stage. Of course that book was from 1993, compiler optimizers have improved enough since that they can likely take up the slack if the programmer takes the easy route like declaring a variable when a constant literal value is the intent. And that was C89 not C++14
@mikeorioles
@mikeorioles Жыл бұрын
The best video on the const keyword, and it's not even close. Good stuff!
@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.
@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
@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 .
@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++
@jimmylander2089
@jimmylander2089 5 жыл бұрын
I keep coming back to this video because I always forget what const means depending on its position and it's so ANNOYIIIIIINNNNGGGG
@eyalpery8470
@eyalpery8470 4 жыл бұрын
Thanks! Refactored my whole program at work
@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!
@XpressCrosSs
@XpressCrosSs 4 жыл бұрын
it is often optimized into an immediate operand in instructions and stored in text segment
@Prashantkumar-pn6qq
@Prashantkumar-pn6qq 3 жыл бұрын
03:28 "Now let's start adding const everywhere" - Cherno be like - let the games begin baby😂
@sri95
@sri95 2 жыл бұрын
Thanks for clearly explaining ~10 contradicting concepts in 10 min ...
@drwisdom1
@drwisdom1 5 жыл бұрын
I never knew about the const after member function usage. It is interesting, but not of much use. In the early days of c they didn't have prototypes (it was taken from C++), so if you got one argument wrong or the wrong number of args it would compile but crash at run-time. Prototypes moved the issue of wrong args from run-time to compile time, making the programmer's life a lot easier. The purpose of const is similar. It prevents you from mixing up your arguments. Additionally, it prevents you from modifying the wrong data. Const is a tool that helps the compiler point out where you made programming mistakes, preventing them from appearing at run-time. You should use const in all applicable situations. You could also use const to create different member functions with the same name, but that would be bad technique.
@alecmather
@alecmather 2 жыл бұрын
Literally amazing video, just answered all my questions about const (and more) in 12 minutes, you're a GANGSTER
@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?
@372leonard
@372leonard 7 жыл бұрын
are you ever gonna do function pointers?
@Steven-tw7iz
@Steven-tw7iz 7 жыл бұрын
Paul Leonard Boon I definitely think he should. That would be an awesome topic
@Hopsonn
@Hopsonn 7 жыл бұрын
I hope not, that is more a C thing. In C++, "std::function" should be used
@brod515
@brod515 7 жыл бұрын
Why are you Guys on this playlist
@romanprykhodchenko1551
@romanprykhodchenko1551 4 жыл бұрын
You are the best!!! Really structured and easy to understand lessons!) Very useful for any kind of the projects) Thanks bro!
@udayyadav3489
@udayyadav3489 5 жыл бұрын
who the hell are these 14 people to dislike such an amazing video...........
@Weredah
@Weredah 7 жыл бұрын
Thank you so much for clearing up const's I really appreciate this series :D
@jingfenghong2312
@jingfenghong2312 7 жыл бұрын
Thank you. Could you also introduce a little bit more about return constant, return reference and return constant with reference? I'm confused about these three things.
@Hetp111
@Hetp111 6 жыл бұрын
Thanks! The level of depth you go in is amazing!
@GfastGao
@GfastGao 6 жыл бұрын
Man, I start to think if I really should pledge through patreon to this series, because you are the correct one for me. INDEED.
@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
@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!
@SahilKumar-ni7su
@SahilKumar-ni7su 4 жыл бұрын
new subscriber from india amazing teaching technique bro keep it up
@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 ...
@aubreymatende6497
@aubreymatende6497 4 жыл бұрын
im loving these videos, i am asking if you can make a video on friend functions
@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.
@mohamadelmahdihoumani4239
@mohamadelmahdihoumani4239 3 жыл бұрын
Can you make the video on how you type this fast in VS? Amazing content btw !
@TheMR-777
@TheMR-777 4 жыл бұрын
Hey man, I've never seen these in-depth concepts before in my University! Many thanks for this!
@thomasmathews4592
@thomasmathews4592 6 жыл бұрын
Great video. Could you consider doing constexpr as well?
@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?
@2k23_hovercars7
@2k23_hovercars7 2 жыл бұрын
Cnt0=Obj1.Date(); Cnt1=Numbers Cnt2=Count of Count
@hts2370
@hts2370 3 жыл бұрын
very clear explanation
@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)
@danielketcheson1965
@danielketcheson1965 2 жыл бұрын
Before video:🙄🤕 During video:🤔🤯🥰 After video:🧐
@ellisha4185
@ellisha4185 2 жыл бұрын
When you talk about putting the const before and after the pointer, are you talking about top/low level const?
@sachinnair8375
@sachinnair8375 4 жыл бұрын
Thanks man ..... your tutorials are very clear to follow.✌️
@greggas87
@greggas87 4 жыл бұрын
"One thing I'll point out, just quickly - haha 'point out' ... " .. Had to laugh here
@celestialmaat9462
@celestialmaat9462 2 жыл бұрын
I spent like 20 minutes and then looked you up🤯🤯🤯! Thanks
@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
@ezequielzion
@ezequielzion 4 жыл бұрын
te amo cherno acá te estamos viendo con un amigo muchas gracias!!!!
@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
@Vectoradc
@Vectoradc 2 жыл бұрын
Love how I don't even study coding but I somehow make it to the past half now
@kartikxramesh
@kartikxramesh 4 жыл бұрын
This was such a good video!!! Multiple *holy shit* moments for me throughout the video. This is better than Breaking Bad ngl.
@sangdilbiswal30
@sangdilbiswal30 Жыл бұрын
this is a complete video I was looking for thanks sir.
@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 👍
@danielc4267
@danielc4267 5 жыл бұрын
Remember, Cherno's promise to put links at the video corners is a just CONST :P
@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 :) :)!!
The Mutable Keyword in C++
6:56
The Cherno
Рет қаралды 176 М.
Harder Than It Seems? 5 Minute Timer in C++
20:10
The Cherno
Рет қаралды 214 М.
Каха и лужа  #непосредственнокаха
00:15
Ice Cream or Surprise Trip Around the World?
00:31
Hungry FAM
Рет қаралды 21 МЛН
Copying and Copy Constructors in C++
20:52
The Cherno
Рет қаралды 436 М.
Arrays in C++
18:31
The Cherno
Рет қаралды 431 М.
Templates in C++
17:58
The Cherno
Рет қаралды 601 М.
lvalues and rvalues in C++
14:13
The Cherno
Рет қаралды 324 М.
WHY did this C++ code FAIL?
38:10
The Cherno
Рет қаралды 293 М.
31 nooby C++ habits you need to ditch
16:18
mCoding
Рет қаралды 824 М.
C++ From Scratch: Constexpr
9:45
CoffeeBeforeArch
Рет қаралды 10 М.
POINTERS in C++
16:59
The Cherno
Рет қаралды 1 МЛН
Macros in C++
19:36
The Cherno
Рет қаралды 241 М.
Каха и лужа  #непосредственнокаха
00:15