Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I am replacing my #defines, for instance #define NUM_SLIDER_POSITIONS 5 for constant variables. Should I keep the old naming like:
const unsigned int NUM_SLIDER_POSITIONS = 5;
Or should I use something more like:
const unsigned int kNumSliderPositions = 5;
.
EDIT: The post has been put on hold, but anyway I'd like to sum up your answers:
Other option would be using underscores as a separators using lower case letters:
const unsigned int num_slider_positions = 5;
Constant identifier.
Regarding the use of a prefix as a way of identifying constants , the most common options are not using it, as it may not add relevant information:
const unsigned int num_slider_positions = 5;
Use a "k" before the name:
const unsigned int k_num_slider_positions = 5;
Or declaring the variable inside a class or namespace, in order to avoid polluting the global scope and providing a more self-explanatory name:
namespace defaults // or "config", or "settings" or something like that
{
const unsigned int num_slider_positions = 5;
}
Client code:
int slider_positions = defaults::num_slider_positions;
I am replacing my #defines for constant variables.
Kudos! :)
Should I keep the old naming like: [all-caps]
If the coding conventions of your project designate constants to be in all-caps, you should (as it spares you an effort). Otherwise, you should not (because it will be confusing later, for maintenance).
Or should I use something more like: [bastardized hungarian convention]
This is up to you. Personally I do not like to add weird letters for my constants, because when reading the code - or writing it - I do not care much that they are constant (and if I try to write into them, the compiler will let me know).
My (personal) choice would be to use a namespace for providing context (instead of a prefix), along these lines:
namespace defaults // or "config", or "settings" or something like that
{
const unsigned int num_slider_positions = 5;
}
Client code:
int slider_positions = defaults::num_slider_positions;
I find this to be a superior alternative, because the context is more self-explanatory (than a "k" in front of it, or a "g" or a whatever else).
It's up to you for any name convention. But for C++ code you may also consider putting constants inside a class that use it, instead of pollute the global scope.
the naming convention of constant name with C++ is use a k followed by mixed case,
for constants defined globally or within a class, As a convenience to the reader, compile-time constants of global or class scope follow a different naming convention from other variables. Use a k followed by words with uppercase first letters
const unsigned int kNumSliderPositions = 5;
see more
Related
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 1 year ago.
Improve this question
So I had this question at an exam. And I don't know which answer is correct.
#include<bits/stdc++.h>
using namespace std;
struct Test{
int note;
char exam[20];
}qwerty;
int main()
{
cout<<qwerty.exam;
}
What is the type of qwerty.exam in the previous code? Is it char or char[20]?
They said the right answer is char[20]. But if the type is char[20], why can't I declare something like: char[20] exam; ?
Moreover, I found this on cplusplus website:
Like a regular variable, an array must be declared before it is used. A typical declaration for an array in C++ is:
type name [elements];
where type is a valid type (such as int, float...), name is a valid identifier and the elements field (which is always enclosed in square brackets []), specifies the length of the array in terms of the number of elements.
So I understand that the type does not depend on the number of elements. Please try to explain.
Thanks in advance!
But if the type is char[20], why can't I declare something like: char[20] exam; ?
That's not the correct syntax. The correct syntax is char exam[20]. You could also do something like this:
using char_array = char[20];
char_array exam;
So I understand that the type does not depend on the number of elements.
That's exactly the opposite of what's the case. Arrays of different sizes have completely different types. Therefore, char[20] and char[21] are completely different types.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I am new to C++ and have come across the const keyword. I looked it up online and read that The main use is to prevent the changing the value throughout the program. I have these two snippets below
const int size = 56;
Compared to using
int size = 56;
Why should I use one over the other? What is the idea behind using it.
Yes, you should make all variables const if you are never going to change its value after initialization. This prevents bugs where you accidentally change a value you're not supposed to. You seem to be aware of this already.
In addition, there is something that you might not be aware of, which is that making an int variable const also makes it a constant-expression, so long as the initializer itself is also a constant-expression, e.g. the int literal 56. This allows you to use it in contexts where you need a constant-expression, e.g. as the dimension of a static array:
const int size = 56;
int a[size]; // ok
int size = 56;
int a[size]; // error
In c++ CONST is used when you want to declare something which will not change its value throughout the program. But if you accidentally try to rewrite its value, the compiler will through an error. Most of the time it is recommended to declare a variable of int type so you can redefine its value when it's required. CONST is used when you want to declare a static array i.e.
const int a = 100;
int sampleAray[a];
And int is used when you, the value of the variable will be modified at some point i.e.
int a = 12;
int arr[4] = {11,22,33,554};
for (int i=0; i<4; i++){
if(arr[i]%2 == 0){
a+=arr[i];
}
}
When you have a const variable holding something that should not change and accidentally write code that modifies the variable anyway, you get a nice compiler error and can correct your mistake easily. If the variable is non-const, the compiler cannot help you and you now have a bug that may be hard to find.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
Codes bellowing compiles:
namespace x
{
namespace y
{
namespace x
{
struct C
{
C ()
{
std::cout << "x::y::z::C" << std::endl;
}
};
}
}
struct C
{
C ()
{
std::cout << "x::C" << std::endl;
}
};
}
x::C and x::y::x::C are not same, but it's sometimes confusing.
Why is x::y::x allowed in C++? Isn't it more clear to forbid this?
Why is x::y::x allowed in C++? Isn't it more clear to forbid this?
No offense, but I think your premise is seriously flawed.
Maybe you didn't notice but having names being the same on different levels of nesting is something very natural. Consider constructors. The fully qualified name of a constructor of class foo is foo::foo(). Nothing unusual is it?
Now what if I want to put my class inside a namespace called foo. I am not arguing that this is the best naming scheme, but from the top of my head I also see no reason to outright forbid it. The constructor would be foo::foo::foo() then.
Having a rule that would disallow such naming would lead to lots of frustration to anybody that wants to use such (possibly suboptimal, but thats just opinions) naming scheme while having absolutely zero gain for someone that does not want to use such naming. In total there would be no benefit.
It's similar to variables having the same name in different scopes. Technically valid. After all, at the assembly level there are no names, just pointers and sizes.
void foo()
{
int x = 1;
if (true)
{
int x = 2;
x = 3; // Whops
}
}
C++ is not a forgiving language, if you mess up with anything, including variables naming, you are on your own. If you want the language to save you, there are (plenty of) other languages to pick.
That said, MSVC (and probably other compilers) issues a warning when a declared variable hides another variable in an outer scope, so by reading compiler warnings you can be helped.
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 3 years ago.
Improve this question
I got an array at the end of my class and i don't know how to use it.
the bus[10] is so hard to understand. I don't know why it can access driver and what does empty() function really do.
#include "conio.h"
#include "stdio.h"
#include "iostream.h"
#include "string.h"
#include "graphics.h"
#include "stdlib.h"
#include "dos.h"
static int p=0;
class a
{
char driver[10];// driver
public:
void install();// for installing
}bus[10];//here we declare the number of buses we can have.
void a::install()
{
cout<<"Enter bus no: ";//ques
cin >> bus[p].driver;// what does this mean
bus[p].empty();//what does this mean
p++;
}
This is syntax for defining a type, and an instance of that type, at the same time.
For example:
struct Foo {} foo;
is the same as:
struct Foo {};
Foo foo;
So your example defines the type a, and also creates an array of 10 as called bus.
It would be more clearly written thus:
class a
{
char driver[10];
public:
void install();
};
a bus[10];
In this manner we can now more easily see that you've created a global array called bus, which you can use like you'd use any other array.
Since p is zero (to begin with), bus[p] just gives you the "first" a object in the array (to begin with). As p is increased, subsequent buses are accessed.
So, this:
cin >> bus[p].driver;
reads into the driver member of the pth bus.*
And this:
bus[p].empty();
means nothing, because a does not have a member function called empty().
* Well, the p+1th bus, because array indices begin at zero but English doesn't!
P.S. You can do funny (read: stupid) things with this syntax!
This is a very strange looking code, probably from an old workbook.
I could help you with achieving the action that you want, but it's hard to understand anything from this snippet.
Wrong: As far as I remember adding a identifier at the end of an unnamed struct gave it a name just like the usual approach.
struct {
float x, y;
} Point;
//is equal to
struct Point {
float x, y;
}
However I'm not familiar with the array syntax you provided.
I suppose std::cin >> bus[p].driver is meant to read the "name" that the char[10] driver field is. But using a char array here is troublesome and it's much better to replace it with std::string and shortening it to 10 characters after input.
The empty() method is often used as a container function returning boolean and telling the programmer whether the container is empty or not. Here however this function is undeclared and the code won't compile either way.
Not to mention that non-const variables placed out of function scope, like the static int p = 0, are a grave mistake.
Not true: In conclusion this is a very messy code and without the knowledge of what you want to achieve nobody could help you here.
See the answer below for better explanation.
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 3 years ago.
Improve this question
Yes I know using namespace std is bad practice, but I have already written a majority of the code with this declaration in place, and I don't think I have the time to go back and modify it.
The reason for the global variable is that I'm using multiple threads which need access to this variable and need to modify it.
My question is, I have int remainder = 0; declared globally, and within my master thread I call remainder = 13 % 5; for example.
This gives me an error saying 'int remainder' redeclared as a different kind of symbol and I've read that the reason is that using namespace std overrides the std::modulus operator, if I understood that correctly.
What other methods can I use to perform this function, keeping using namespace std and remainder as a global variable?
#include<iostream>
#include<cmath>
using namespace std;
int remainder = 0;
void testing();
int main(){
testing();
cout << remainder << endl;
return 0;
}
void testing(){
remainder = 13 % 5;
}
The problem is your global variable name conflicts with std::remainder from the standard library. Example on Compiler Explorer.
The problem with using namespace std; is that it brings so many symbols into the global namespace that this error is almost inevitable. It's a bad practice for anything but the simplest toy programs.
The conflict is with std::remainder, not with %. The variable name you've chosen conflicts with a function in the std namespace. You already know using namespace std; is bad, so I'll spare you.
Options:
Lose the using statement.
Rename the remainder variable.
Put the remainder variable in its own namespace, and explicitly refer to it through that namespace.