 Hey everybody, this is Brian and in this episode we're gonna talk about Q objects and well you cannot copy them There's a reason for it. Let's go ahead and demonstrate super quick right click add new and we're gonna make a class And I'm just whipping through this at light speed Cat we're gonna make a custom Q object here It's going to be a Q object with our Q object macro. It's gonna give us you guessed it what we're gonna be adding Yeah, let's go ahead and add that in Okay, so far so good seems super simple now We've got this cat class and we can do some really awesome things with our cat class So let's go ahead and try and do awesome things with it. So I'm gonna say include cat and Then let's go ahead and make a function And this is where a lot of newbies get super super frustrated right off the bat with Q But there's a reason for this. So we've got our cat and let's go ahead and Say we want to make an instance of cat. Let's call this kitty and Then we're gonna go ahead and just test with our kitty Usually they do something like this and then oh wait what I'm just This makes no sense. I'm just making an instance of a class and I'm putting it over. Why won't this work? Well, that's what we're gonna dive into in this video from a newbies perspective This is super frustrating. You're used to just copying things away to your little hearts content But you see when I mouse over here, it says Q disable copy class disables use of copy constructor What is this Q disable copy? Well, if I go down here then mouse over this you see we have some issues and Following it down it goes into Q object So we're inheriting Q object and that's what's going on something inside of Q object is Doing this Q object has been explicitly marked deleted here and it gives us a little link this I absolutely love this I click this and it takes us right into the code Q disable copy. This is the macro that's causing all this heartburn This is keeping us from copying our object in memory And if I right-click this and I go to follow symbol under cursor or press F2 Takes me right to the code that's doing this so I can go. Oh, this is what's going on You guessed it Copy constructors have been deleted and there's also a Q disable move and disable move or disable copy move, which means We cannot copy this and we cannot move it It is basically permanently at that memory location and there's a specific reason why we're gonna talk about in the next section But I wanted to highlight two distinct things first you can jump right into the source of Qt and This is what's causing it this Mac or right here, which is baked right into Q object, which is in the Q global dot H It's part of Qt For some of us old-school programmers. This is a little hard to swallow I mean I copy things all day long and I never have a problem. What makes Q object so Special that I'm not allowed to copy it. How dare Qt keep me from copying my objects I am the ruler over all of my code It makes no sense until you really understand that Q object is a very complex object and Copying it could have disastrous consequences. So let's go ahead and jump in here And let's simplify this we have a switch and a light and we want to make a signal slot So when we flip the switch on the light goes on we flip the switch off the light goes off You notice these are two different memory addresses because they're two different objects We then take this light and we go to throw it into a function that's going to copy it But we've got the signal slot set up Something like this is going to happen Where C++ is going to copy every aspect of that into a completely new object At a new memory address Now you have a switch that is controlling two different lights and we didn't mean for that to happen and That could have disastrous consequences, especially in very large complex applications And it becomes a nightmare for Qt to track on the back end code and it makes it even worse if you were to Take this guy and copy him Because now what's going on is you've got this Like this While you have two switches each switch is controlling two lights and they're all at different memory locations And you don't know which one was which That could be very bad that could be disastrous if you tried to do this Now There are ways around this, but I wanted to drive a fundamental concept home when we talk about Q objects I'm going to toggle comment selection You notice how 99% of the time when we print something out we can say Q info I will just print out the class. So in this case, I'm going to print out our kitty And let's go ahead and say the address of Save and run and what is it going to spit out? The memory address so it's going to give us the type and the memory address because when it comes to Q object Qt has said Every Q object must be unique to avoid that problem we just discussed And what defines its uniqueness is its memory location There are ways around certain things, but they you'll have disastrous consequences don't even try to fight it And this is the main reason why you're going to end up in this nightmare scenario where signals and slots start going berserk And everything starts going crazy in the background and it gets even worse if you try to delete this thing later on If you know c++ the answer to this is pretty obvious how you get around this is simply don't copy the object And let's go ahead and demonstrate here. So I'm going to just uncomment all this This of course will create a copy. I'm going to Use the address of And I'm going to say test to My preferred method and you'll see this used heavily in Qt is pointers So you're just simply in a point to the object makes it super ridiculously simple Then you just avoid that whole thing. Let's go ahead and grab this Let's go ahead and say cat And cat now If I try to use it up here, you notice how it's still going to say invalid Binary expression. This is why I almost Always use a pointer simply because I don't have to mess around with that every time I Q info something out So now we can just you guessed it And we can purify these things a little bit here. So I'm gonna say ETR And through the magic of copy and paste Absolutely love copy and paste probably best invention ever Go ahead and give that the address and voila. It just works. It's super ridiculously simple So you can see it is the exact same memory location because we're just passing you guessed it the address of and the pointer to it Or in this case actually the address of now one other little thing I want to do is let's say you have multiple cats And let's call this I need a good one. Hmm What do I want to call my cat? Hmm Let's call him death because cats just seem to kill everything. I swear they do How do you differentiate these two? So for example, let's say I do Something like this Pats are really nature's murder machines. I swear they are it's a bit morbid forgive me But they really do just annihilate everything So save run How do you really differentiate? I mean we've just got memory addresses and we can see the memory addresses are different But it doesn't really help us Well, that's where object name really comes into play and you'll see me do things like this some say kitty set object name and then Let's call death here. Let's get death like a better prettier name and let's say the grim reaper Because why not that cat just annihilates every insect that it sees And let's go ahead save run and you'll see what cute does whenever you queue info something out Is it actually has the object name right in there? So you can really differentiate between the objects rather than the memory address I wanted to include this though because the object name Does not have to be unique. Let's go ahead and demonstrate that. So I'm going to take death incarnate here and I'm going to say It is kitty Notice there are two different objects, but I've set the same name. It's going to save run. This will not keep us from doing this and it gets very confusing So the major takeaway here is the memory address really defines the object and not the name So if you're gonna do things like this, hmm, be very careful You're gonna want to set the object name to something unique, but it's up to you to track that uniqueness because cute is once again Using the memory address to track the uniqueness of that object I hope you enjoyed this video. You can find the source code out on github.com If you need additional help myself and thousands of other developers are hanging out in the void realms facebook group This is a large group with lots of developers and we talk about everything technology related not just the technology that you just watched And if you want official training, I do develop courses out on udemy.com This is official classroom style training if you go out there and the course you're looking for is just simply not there Drop me a note. I'm either working on it or I will actually develop it I will put a link down below for all three of those and as always Help me help you smash that like and subscribe button The more popular these videos become the more I'll create and publish out on youtube. Thank you for watching