about const keyword as a parameter in function [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I still don't understand what is the difference between these two code i mean if we put const
keyword in front of data type parameter in function what happened to my our code
#include <iostream>
using namespace std;
void function1(const int x);
int main(){
};
And if we don't put const keyword what happened to our code?
#include <iostream>
using namespace std;
void function2( int x);
int main(){
};

The const keyword prevents a variable from being modified.
It mostly used to prevent an object that is passed by reference from being modified. It can also prevent mistakes at compile time like writing = instead of == in boolean statements.
In your case, the variable is passed by value and not by reference. In both function declarations, the variable inside the function is a copy of the variable you pass to that function. This means that even if you change a variable that is passed by value, its value will only change inside the scope of that function.
Relate to this article about scoping.

Related

How to pass private class member by constant reference? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
This is my class
#include <vector>
class MyClass
{
public:
void print(); // prints X to console
private:
std::vector<int> X (4, 100);
};
When I call the print() method I want X to be passed by constant reference to print(). How can I do that? Currently is looks like as X is passed by value.
Ok, maybe this will help you to start thinking in c++, even if you did not make clear explanation what you need, if I may guess, what you probably think of is that you want to be able to call print() member function on const instances of MyClass, or rvalue has been bound to const lvalue reference, so you need to mark print as const member function of type MyClass.
class MyClass
{
public:
void print() const; // prints X to console
private:
std::vector<int> X (4, 100);
};

Why can't I assign a value to the class member variable in a bool function that returns const? C++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
bool Car::isEnoughFuel(int miles) const
{
drivableMiles = fuelGauge.getCurrentFuel() * MPG; // Error here
bool status = true;
if (miles > drivableMiles)
status = false;
return status;
}
Error: Expression must be a modifiable lvalue.
Declaring a member function with the const keyword specifies that the function is a "read-only" function that does not modify the object for which it is called. A constant member function cannot modify any non-static data members or call any member functions that aren't constant. (Source)
In the line drivableMiles = fuelGauge.getCurrentFuel() * MPG; you are trying to modify the object isEnoughFuel() is called on. You probably don't want to have your function as const. However one work around would be to using copies instead.

Mixed values of arguments, when I call pointer member function [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have called a pointer of member function from another class. This function has 2 arguments.
It is working... But in this member function, in the debugger, I see that values of arguments were swapped (mixed). I see the correct values but not in correct variables(arguments). May be it is BUG...
Qt Creator c++
void SmartCTL::OnReadBufferA(int shm_id, int length); //declaration
hookedIDs[HOOKIDPOINTERS::READBUFFERA] = (void *)&SmartCTL::OnReadBufferA; // Save pointer of function
memcpy(hookedIDs[smartCTL.shellID],smartCTL.hookedIDs,8*sizeof(void *));// copy pointers
void (*pf)(int,int )= (void(*)( int,int ))hookedIDs[i][HOOKIDPOINTERS::READBUFFERA];// set pointer
pf(shells[i]->readBuff.shm_id,shells[i]->readBuff.length); // call hear
In result I get value hells[i]->readBuff.shm_id in length and value shells[i]->readBuff.length in shm_id
This isn't a bug. What you have is undefined be behavior. A member function is not the same as a regular function. There is a this in the member function and the way it gets that is through an implicit function parameter of the class type. So
void SmartCTL::OnReadBufferA(int shm_id, int length)
is really something like
void SmartCTL::OnReadBufferA(SmartCTL& this_ref, int shm_id, int length)
And this is why you can't cast a pointer to a member function to a pointer to a regular function.
If you need to have both member functions and regular functions in your "array" then you are going to need a std::function. It uses type erasure to allow you to to store different types of function objects that have the same interface. For example you could have something like
std::vector<std::function<void(int, int)>> functions;
functions.push_back(some_regular_function);
functions.push_back([](int shm_id, int length){ SmartCTL s; return s(shm_id, length); });
Another option is to make the member function a static function. A static function does not have a instance of the class bound to it so you can treat it as a regular function.

What's the difference between namespaces & classes? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
In C++13/4 environment, what's the difference between a namespace and a class?
The way I see it;
namespace foo
{
int a : 4;
int b : 4;
}
and
class bar
{
public:
int a = 0;
int b = 0;
}
is the same thing...
Yes, they're accessed different as such;
namespace....
foo::a=20;
foo::b=30;
class....
bar alpha;
alpha.a ...
alpha.b ...
But in general, what's the advantage of one over the other?
There are plenty of differences. A namespace is a grouping mechanism for names, nothing more or less. On the other hand, classes:
Are types.
Can be instantiated.
Can be derived from.
Can have public, protected, and private members.
Can have virtual functions.
And so on.
If you find yourself wondering whether you should use a namespace or a class, then you are probably just looking for a way to control the scope of names---so the choice is clear: use a namespace.
Namespaces allow you to group entities into having a local scope rather than global scope. This is common with the standard library, std, for instance.
An example group of names may be streams such as cout and cin.
Without using a namespace, you have to define the namespace scope std.
std::cout << "Hello, world!";
With a namespace, you have revealed the scope once and no longer need to declare it again.
using namespace std;
cout << "Hello, world!";
However, in the latter case, you could not use another variable named cout that is user-defined or contained within another library.

What kind of of object does this function return? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
Imagine that you have a situation like this:
class className{
...
}
className func(){
className cl;
...
return cl;
}
int main(){
...
func();
}
What does the function func() return when you call it in the body of the program? A temporary copy of the object cl?
I don't understand this, since in the body of the function func() you can get the address &cl, but you get an error if you try to call &(func()) inside the function main().
Inside the function you are dealing with a so-called lvalue shortly speaking with an object which address is known because the object is defined explicitly.
The return value of the function is a temporary object (it is a so-called rvalue). Its address is not known. We do not know
where the compiler defined this object. So we may not apply operator & to a temporary object.
Another similar example
struct A
{
int x;
};
A f() { return A(); }
int main()
{
f().x = 10; // here the compiler will issue an error
}
This code shall not be compiled though for example MS VC++ 2010 will compile it due to either a bug or its language extension.:)