Garbage Value of a defined namespace [duplicate] - c++

This question already has answers here:
How to print function pointers with cout?
(7 answers)
Why am I always getting output as 1 when printing a function?
(2 answers)
Closed 12 months ago.
So, I just started with C++ and was doing random things with a snippet of code I found on the net. The code is a simple use of defining namespace. After my changes in the code, it looked like,
#include <iostream>
using namespace std;
namespace ns1 { int value() {return 5;}}
namespace ns2 { int value() {return -5;}}
int main() {
cout << ns1::value<< '\n'; //5 will be displayed
cout << ns2::value<< '\n'; // -5 will be displayed
}
Now, i know that i have called the wrong function and it should be ns1::value() instead of ns1::value , but my question is why is the code still working? And why is it giving an output of 1?

Related

How can we declare short int as the return type in main function of C++? [duplicate]

This question already has answers here:
What should main() return in C and C++?
(19 answers)
Return type of main function [duplicate]
(5 answers)
Closed 11 months ago.
I am always returning a value of 0 from the main function. I was wondering if we could change the return type of the C++ main method to short int.
#include <iostream>
using namespace std;
short int main() {
cout << "Hello World" << endl;
return 0;
}
Something like this, but the above code returns an error saying that the return type should be 'int'. Can someone please guide me?

Function calling and return statement [duplicate]

This question already has answers here:
Omitting return statement in C++
(3 answers)
Why does this C++ snippet compile (non-void function does not return a value) [duplicate]
(7 answers)
Closed 2 years ago.
In the given C++ code when the statement n isn't present the function abc returns 10 .I.e def(10) acts as if it is a return statement but it isn't.
The output of the function is always 10 and doesn't lead to undefined behaviour when statement n as given in code isn't present in the code.
I am using sublime text 3.
So why it is behaving in such a manner or am I missing any concept
#include<bits/stdc++.h>
using namespace std;
int def(int x){
return x;
}
int abc(int x){
def(10);
return x;//statement: n
}
int main(){
cout<<abc(5);
}

vector subscript out of range error strange behavior in C++ [duplicate]

This question already has answers here:
Vector going out of bounds without giving error
(4 answers)
Accessing an array out of bounds gives no error, why?
(18 answers)
Closed 5 years ago.
I'm starting to learn C++ so the answer to my question might be obvious to you but I am really puzzled. I expect the program would crash when accessing a out of range index but instead, the program runs just fine. Here is the code.
#include <iostream>
#include <typeinfo>
#include <string>
#include <vector>
using namespace std;
int main() {
int x;
vector<int> v1;
while (cin >> x) {
v1.push_back(x);
}
cout << "out of bound: " << v1[10] << endl;
return 0;
}
And here is the output:
1 2 3
out of bound: 0
There are 3 elements in v1, meaning there is NO v1[10], only v1[0] through v1[2].
The behavior is not, strange. That’s what is supposed to happen, because std:: vector does not perform size checking for '[]'.
So, you are basically getting a garbage from v1[10].

Why can't I change the value of a const data (int in my case) using const_cast [duplicate]

This question already has answers here:
How is a variable at the same address producing 2 different values? [duplicate]
(4 answers)
Closed 8 years ago.
I have a piece of program as shown below:
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
const int i = 100;
cout<<"const i::"<<i<<endl;
const_cast<int&>(i) = 200;
cout<<"const i after cast::"<<i<<endl;
return EXIT_SUCCESS;
}
But the value of i is still 100. Aren't const_cast supposed to change the value of i?
Constant data is, by its very definition, constant, and attempting to change constant data leads to undefined behavior.

Changing contents of a std::string with a function [duplicate]

This question already has answers here:
When to use references vs. pointers
(17 answers)
Closed 9 years ago.
I feel like this answer will be quick and simple, but I cannot seem to figure it out right now.
#include <string>
#include <iostream>
using namespace std;
void change_thing (string x) {
x="not thing";
}
int main() {
string maybe_thing;
maybe_thing="thing";
change_thing(maybe_thing);
cout << maybe_thing << endl;
return 0;
}
I want maybe_thing to be "not thing" when it prints. I've tried a bunch of different pointer strategies, but nothing seems to work (which I could easily just be doing wrong; my knowledge of pointers is incomplete anyway because I'm new to c++).
Thanks in advance!
You don't need a pointer, just pass the string by reference:
void change_thing (string & x) {
x="not thing";
}