Why Returning Reference Is Bad Some Time In C++?

  Рет қаралды 11,036

CppNuts

CppNuts

Күн бұрын

JOIN ME
-----
KZbin 🎬 / @cppnuts
Patreon 🚀 / cppnuts
COMPLETE PLAYLIST
------------
C++ Tutorial For Beginners: • Introduction To C++
STL (Standard Template Library): • STL In C++
ThreadIng In C++: • Multithreading In C++
Data Structures: • Data Structure
Algorithms: • Binary Search
Design Patterns: • Factory Design Pattern...
Smart Pointers: • Smart Pointer In C++
C++14: • Digit Separator In C++
C++17: • std string_view in C++...
C++ All Type Casts: • static_cast In C++
INTERVIEW PLAYLIST
------------
C++ Interview Q&A: • Structural Padding & P...
C++ Interview Q&A For Experienced: • How delete[] Knows How...
Linked List Interview Questions: • Find Kth Node From Bac...
BST Interview Questions: • Search Element In Bina...
Array Interview Questions: • Reverse An Array
String Interview Questions: • Check String Is Palind...
Bit Manipulation Questions: • Find Set Bit In Intege...
Binary Tree Interview Question: • Invert Binary Tree
Sorting Algorithms: • Bubble Sort
C++ MCQ: • Video
C MCQ: • What printf returns af...
C Interview Questions: • Designated Initializat...
QUICK SHORT VIDEOS
-------------
C++ Short : • C++ Short Videos
C Short : • Shorts C Programming MCQ
In this video we'll learn about Why Returning Reference Is Bad Some Time In C++?
And it is very important interview question.
Actually if we return reference and once the scope of the function is finished the variable is destroyed but as we have returned as reference so caller is trying to access destroyed memory hence it is really bad practice to return as reference.
It is ok to return reference but not all the time and let's see when we should and shouldn't return as reference.
#cpp #programming #tutorial #coaching #beginners #computerscience #softwareengineering

Пікірлер: 13
@Anonymous-wy4ld
@Anonymous-wy4ld 5 жыл бұрын
One reason we use this is to avoid copying a large object, Another reason is to allow you to use a function call on the left side of the equal sign.
@CppNuts
@CppNuts 5 жыл бұрын
Correct!!
@DarkLevis
@DarkLevis 7 жыл бұрын
I'd like to point out that you can actually get the correct value because 10 might still be at that address. Of course you can also get garbage, often depending on the rest of your code.
@kanishkjain7137
@kanishkjain7137 4 жыл бұрын
Thank you for the amazing videos. I had tried running the same code in Visual studio. It did not give me an error and also no garbage value as you said at 1:33 timestamp. Can you please explain to me why?
@akshhaayabitkaar607
@akshhaayabitkaar607 4 жыл бұрын
returning correct value for following code snippet
@CppNuts
@CppNuts 4 жыл бұрын
Got it, mean they are not erasing the memory right away.
@akshhaayabitkaar607
@akshhaayabitkaar607 4 жыл бұрын
int& fun() { int j = 10; return j; } int main() { int& c = fun(); return 0; } tested on VS
@CppNuts
@CppNuts 4 жыл бұрын
Soo, did it returned ?? If it did that mean they are not erasing the memory right away.
@akshhaayabitkaar607
@akshhaayabitkaar607 4 жыл бұрын
@@CppNuts yes, they are not erasing the memory.
@bluehornet6752
@bluehornet6752 7 жыл бұрын
Why would you need to return a reference to an object that you already have a reference to? That's the point of passing in a reference in the first place. Your videos are generally good, but this one is pretty weak. I humbly suggest you should slow down a bit and think a little about the examples you give. This one doesn't make much sense.
@CppNuts
@CppNuts 7 жыл бұрын
Hi Tom B, Thanks for your suggestion this is really valuable comment. But i feel we can return reference if we have reference of something, because there could be a function chaining in caller side. Example: fun1(obj).fun2(); here in fun1() if we return reference to obj then fun2() will be called on reference to obj and make sense.
@bluehornet6752
@bluehornet6752 7 жыл бұрын
So you'd have a reference to a reference then? Certainly you could do that if you wanted to chain function calls, but for this example...wow, that's a bit wacky. This function, the way you've presented it, uses a reference because you want to alter the value of 'x'. That's fine. It's perfect actually--just pass the reference, and change the value. Done. It does not need to return a reference, as you are not chaining function calls. So for the code you've presented, you simply don't need (or want) to return a reference as I see it. You could have shown a nice example of creating a dynamic array of integers, or of some other object, inside your function. And *then* your returned reference would have made sense. That said, I really enjoy your videos. You present some very good topics, and in general I think you do a great job--so that you very much for taking the time to make the videos and upload them to KZbin. I have found them to be a great refresher for me, as I haven't been writing in C++ for over a year now.
@CppNuts
@CppNuts 7 жыл бұрын
Tom B, Thanks for your suggestion. :D
Леон киллер и Оля Полякова 😹
00:42
Канал Смеха
Рет қаралды 4,7 МЛН
黑天使只对C罗有感觉#short #angel #clown
00:39
Super Beauty team
Рет қаралды 36 МЛН
you will never ask about pointers again after watching this video
8:03
This Pointer In C++
12:22
CppNuts
Рет қаралды 34 М.
C++ Tutorial:  Returning values by reference
11:34
Professor Hank Stalica
Рет қаралды 4 М.
Should I pass by const reference or by value?
10:45
The Cherno
Рет қаралды 114 М.
Master Pointers in C:  10X Your C Coding!
14:12
Dave's Garage
Рет қаралды 347 М.
Difference Between Reference And Pointers In C++
10:02
CppNuts
Рет қаралды 65 М.
Lambda expressions in modern C++ (in depth step by step tutorial)
18:35
When To Use Reference Over Pointer and Vice Versa In C++
8:40
Why We Must Return Reference In Copy Assignment Operator?
4:46
Леон киллер и Оля Полякова 😹
00:42
Канал Смеха
Рет қаралды 4,7 МЛН