difference between const and static const, more effectively. I know the basic concept of const and static but I need clear explanation of declaring "const" and "static const"
I've been having a problem concerning the initialization of const static integral members with floating point calculations. I'll let his sample program do the explaining:
class Foo { public : Foo() {} const static int samplerate = 44100; const static unsigned short tempo = 120;
[Code].....
I know you can't initialize const static non-integral types on the same line on which they're declared, but I don't see why even an implicit cast to an integral type should be disallowed. I make my calculations using doubles, so I'm surprised that even though it should degenerate into an integer - it's still a problem for the compiler.
i have this rather large class, which (in a way) somehow resembles a custom dialog control). This control is supposed to display data, which it does just fine. To do so, it maintains a
byte settings[10];
array, which holds information on how to display the data.
There are multiple ways to represent this custom set of data.In order to remain flexible in representing it, i thought of implementing some sort of DisplayProvider, which can be registered to the base class and provides that settings byte array.
Preferably, i would now have a set of static const instances of this provider.Using a struct would work nicely here:
The problem: The DisplayProvider would have to do some pre-processing, before handing over control to the base class, which then does the main work.I would end up with something like this:
PHP Code:
class DispalyProvider { baseclass* owner; int settings[10]; void PreProcessing(...);//ends up calling the owner.Processing(...) function };
The main thing here is, that i dont really see a way to create a stock of default "static const DisplayProvder = {...}"s, as i could when using a struct.
I have a little problem with template classes and their specialization. Here is a short example:
template <typename T> struct A{ // some typedefs
[Code]....
The above example is not compiling, because of the assignment of the const static double. Double needs a constructor, but that doesn't work (or seems not to work) with static.
I'm not sure, if it works at all in C++ that way. All I want is a template struct with some typedefs and a constant which is different for different specializations. Don't think it has to be static, but that would be better style, wouldn't it?
Are there other ways of calling a const/non-const override? I want to defined some functions in terms of others, particularly accessors which might or might not require constness- in order to not copy & paste code. This is my current solution:
Is there any way to cast a non-const variable to const one?
I want to read variable n from file and then use it to declare array "int arr[n]", but because n is non-const, the compiler doesn't allow me to do that.
I want to use a const char* as a buffer. I am reading values from a file and adding them to a buffer. How to extract the values is simple enough. I am reading through a filestream, reading each character into a char pointer and progressing that char pointer every time. I have another char pointer marking the start positon
eg.
char *mychar = new char; char *char1 = new char; char *char2 = new char; const char *constchar ; char2 = char1; while(filestream.read(mychar,1) { *char1 = *mychar; ++char1; }
Then I get this problem: constchar = mychar; // const char* = char*.
Constchar does not catch all the data in other words. At some stage some data is lost due to zeros in the data.. How can I put values into a const char and get around this problem? The const char* will //only record everything up until the first zero.
I know that passing arguments by const instead of value is more efficient and allows us to avoid allocating a temporary local variable of the argument type. But is this always true? Or are there some cases when calling functions with constant arguments should be avoided? If so, is passing by pointer the most efficient way?
What is the best way to define const strings when there are separate header and source files?
For example, I have a header that only declare some enums. In that same header I would like to add string representations of those enums so that I can print them easily i.e string_representation[my_enum] for debug and error printing and so on.
If I define them in the header file, I will get a linker error for multiple definitions. If I remove the definition, then I can not define it in the source file.
I'm currently finishing up an assignment that was half written by my professor. Below in the testGrades section of code there are two errors both are the same message.
Error: no matching function for call to Grades:: Grades(const char [15])
Test Grades
//Purpose: Test program for the class Grades // Create stu1 Grades object // Add 5 grades to stu1 - only 3 can be stored in stu1 - other 2 discarded // Create stu2 Grades object // Add only 2 grades
What I need to know is how I can pass an argument to the Book constructor so I can change the const data member Category (with cascading capacity if possible. I also posted some of my set functions for further comprehension.
class Book { friend void CompPrice(Book &,Book&); //friend function that has access to the member functions of this class //The arguments sent to it are by address, and of type the class Book, so that it can have access to its member functions private: //private data members
i am trying to describe the unusual situation where you declare a class member function with this format:
bool class::function_name(void) const
Specifically where the 'const' follows the parameter list. It is my understanding this is a very useful way of ensuring that whatever code you put in the function definition cannot change any data members of its class.
However I have recently read that this form of declaration should not be used as it leads to less optimized and slower code. Is this correct?
I'm getting a "passing...discards qualifiers" error on my if statement and not sure why because I'm not changing anything. I know removing const or making test mutable fixes the issue. I've been taught to always make a function const if it doesn't change anything, in which case, have I finally come across an acceptable time to use mutable?
in a header file and the header file is included in several C files.
Questions:
At run time,
Is there just one copy of the const variable my_fl_dark_gray or are there multiple copies for the multiple C files?If a function uses the const variable, does the initialization statement "my_fl_dark_gray=fl_color_cube(...);" run every time the function is called or does it just run once and then when the function is called it just uses the value stored in memory?
When I try to compile functionB in Visual C++ it gives me this error:-
glibmm/refptr.h(199) : error C2440: 'initializing' : cannot convert from 'const Gdk::Window *' to 'Gdk::Window *' Conversion loses qualifiers
And this is the code from glibmm/refptr.h
Code: // The templated ctor allows copy construction from any object that's castable. Thus, it does downcasts: // base_ref = derived_ref template <class T_CppObject> template <class T_CastFrom>
[Code] .....
I don't actually want to change anything in the member variable canvas_event_box. I just want to be able to call one of its functions from my 'const' member function. Is there any syntax I can use to tell VC++ that I'm not actually changing the variable - just using it.
I have data that is coming into my buffer via popen (process data, not a file). Every seven records is a new set [0-6]. I am trying to 'print out the array line/element value' and 'change the value of element [2] to 0', but my loop appears to be looping through every character and not just every line?
What is the programmers responsibility with respect to const char * returned by various functions, like the C++ string class c_str() function which returns a const char * to an c style string array? In VC++ I cannot delete a const char * which holds a string literal. Take the following code for example:
Code: void func() //a useless function with illustrative code { string s1("abcd"); string s2("efgh"); const char * cc1 = s1.c_str(); //c_str() returns a const char * c style string pointer s2.c_str(); //this returns a const char *, which must be allocated on the heap right? delete cc1; //produces run time error in Release mode in VC++ }
The problem with the above code snip is that space is allocated on the heap (or so I believe) for the const char *'s returned by the 2 calls to c_str(). The delete attempt fails and there is no opportunity to delete the space allocated by const char * because its not assigned to anything (however I see c_str() used this way extensively)
So, if I cannot delete a const char *, how does the memory get recovered? Perhaps the string objects s1 and s2 themselves have pointers to the items on the heap made by c_str() calls and they get deleted by the destructors of s1 and s2 when the function ends?
I have data that is coming into my buffer via popen (process data, not a file). Every seven records is a new set [0-6]. I am trying to 'print out the array line/element value' and 'change the value of element [2] to 0', but my loop appears to be looping through every character and not just every line?
199729173 2014-11-16 10:09:34 Found String! 198397652 2014-11-14 15:10:10 Found String! 198397685 2014-11-14 15:10:13 Found String! 198398295 2014-11-14 15:11:14 Found String!
The problem with that is that string.data isn't considered const during the initialization of the String struct so the compiler throws an error. It doesn't feel very elegant to do it like this either way.
Is there an elegant solution to this problem? I would like to avoid making a copy of the string literal.
I have a class that defines a window (a popup dialog of sorts), and I want the name of that window to be constant. The only problem is that the name of the popup needs to match the title of the parent window, and I get the name of the parent in the constructor. So how do I go about defining this member variable to be constant and initializing it with a value in the constructor?
I want to do something like this, but I know this isn't allowed:
/* class.h */ class foo { public: foo(*parentWindowPtr);
[Code] .....
I should mention that yes the name of the parent window is const char *, and I would like to keep it this way.