Комментарии:
"I like to write empty blocks of code with the same title multiple times" I heard. That's the part I don't understand about classes and apparently constructors. It looks like you're just repeating code without content.
ОтветитьWhy do they somethimes use curly braces like this: myclass::myclass(const std::string& message) : _message {message} {}
Instead of : myclass::myclass(const std::string& message) : _message(message) {}
Thanks!
ОтветитьYou are the best cherno.
ОтветитьIs this also how I should be initializing struct members variables?
ОтветитьWhat about const members?
ОтветитьCherno, you are C++ God. Do you think one can understand this better if you give practical examples. E.g. instead of entity or example, use of Cars, houses, dollars etc.
ОтветитьWhat is the downside of initializing a variable in the header instead of the constructor especially for the primitive data types?
ОтветитьI am quite sure, that if you try this in Release mode, then you get the same result with init list and without.
ОтветитьThanks!
Ответитьnice mic quality nerd
ОтветитьCan you please do a video on padding and alignment as well as the pIMPL idiom?
Ответитьwhy do you pass it in by reference into the constructor?
ОтветитьI have no idea what this is lol
Ответитьwhat if you used like std::string& then would it still douplicate it
Ответитьi don't see a good point behind this, just like the puzzle of who is first the chicken or the egg:
class chicken{ egg brown; chicken(); chicken(egg); };
🔴HELP :
I have 3 constructors : a default constructors with no parameters, a constructor with a parameter, and a copy constructor
the constructor with the parameter works fine. the copy constructor works fine,
but for the default constructor, C++ says "expression must have class type but it has type "ClassName (*)()""
what is going on and how to fix it ?
I used created a point class (has default and copy construct with initializer list )and used its object in circle class(has default and copy construct with initializer list, initializes point object).
struct point
{
int x;
int y;
point():x(0),y(0){std::cout<<" point default construct..!!"<<std::endl;}
point(int X,int Y):x(X),y(Y){std::cout<<"Point Copy construct..!!"<<std::endl;}
};
class circle
{
struct point p;//the default constructor will be called if you dont provide instantiate list below
int radius;
public:
circle():p(0,0)
{
std::cout<<"Circle Default construct.."<<std::endl;
}//default constructor
circle(const point& P):p(P.x,P.y)
{
//p.x=P.x;
//p.y=P.y;
std::cout<<"Circle Copy Construct.....!!"<<std::endl;
}
int main()
{
circle c0(point(3,3)); the point copy constructor gets called 2 times here. so no performance benefit. ??????
//Similarly here point contruct gets called 2 times:???????
point p {9,4};//Curly brace initialization, point copy construct
circle c1(p); //point copy construct, circle copy construct
}
Personal Notes:
- Way of constructing objects by assigning members values
- Not only a coding style, if you don't use that you initialize objects twice , one when declaring the member, and the other one when assigning it to a different object again inside constructor which takes parameters
You didn't mention that you can also give the intializer list a private static function to initialize a member variable. Sometimes you need to do something more complicated than just a simple assignment. Maybe that's a new language feature in 14, 17, or 20. Dunno. You can also give it a constructor for a nested class or whatever. The initializer list is kinda clunky tbh but I feel forced to use it, but the only real downside is that when using the initializer list AND you want to use the constructor for other things to set up the class without using a secondary manually called init function, you're FORCED to implement it in the header instead of the source file so you can't hide the implementation details... holy run-on sentence, Batman. That's a weird design decision... flaw, if you ask me. Some other stuff is also weird... like you can't use a static function for array initialization in the initialization list. You. Just. Can't.
Ответитьa bit fast but overall good explanation = thank you
ОтветитьWould it be correct to use member lists to initialize variables in a struct?
I'm using a struct to group a bunch of variables (integers, small arrays, etc.) that are going to be filled with data once I read a binary file using .read() from std::ifstream.
Even in this case, should I be using a member initializer list instead of initializing these variable inside the constructor?
It's really good to learn that.
ОтветитьCharno !!
ОтветитьPlease do a video kn std containers. Lists and algos like for each
ОтветитьHello I have a question can we use c++ for web application's the part of backend ? can you teach how to create web-backend or desktop applications or how can we use c++ for mobile (some percentage) please give me answer.
ОтветитьShould use uniform initialization '{}' instead of '()' because it will warn on implicit narrowing conversions. '()' will silently allow, for example, an int to be truncated to a char.
So this is better
public:
Entity() : m_Name{"Unknown"}, x{0}, y{0}, z{0}
{ std::cout << "Members now initialized." << std::endl ; }
why not use "this" inside the class ?
ОтветитьDude thank you so much I love your videos
ОтветитьCherno does not miss
ОтветитьNice but only if your consistent, decent trade off
ОтветитьEverytime I go for on of your videos I wonder how deep I will go into the rabit hole, because your content is often packed with things I don't know yet xD, edit: this time was smooth.
ОтветитьLove you man, C++ for life <3
Ответитьthank you
ОтветитьAlso, if a default constructor is not available you can't even avoid using the initializer list.
Ответитьu r the goat
ОтветитьDo you use inititializer lists just with constructor or can / should you use on regular functions?
ОтветитьGreat
ОтветитьI just have a doubt here! when there were two classes example and entity. You called example from entity. Don't you need to override?
Ответитьbrillant!!
ОтветитьYOU ARE AMAZING!
ОтветитьIf the default constructor initializes a member variable to some default value. Wouldn't it be simpler to just assign the default value when declaring it?
ОтветитьThank you for this!
ОтветитьWhy not immediate initialize them inside the class instead of inside constructor?
Ответитьi can hear a robot talking in background lol
confirmed cherno is not a human