C++ :: Why Are All Strings Constant
Apr 10, 2013why all strings are always constant?
View 2 Replieswhy all strings are always constant?
View 2 RepliesSo, I'm working with cocos2d-x and there's a specific function used to print true type fonts to the screen. My problem is that I want to feed a string into the function, but it only takes a string of const char (Which I don't even know what that is. AFAIK strings are a string of chars, not const chars).
Here's a definition of the function :
CCLabelTTF::create(const char *string, const char *fontname, float *fontsize);
an example of it being used
CCLabelTTF *pCoord = CCLabelTTF::create("Coordinates", "Arial", 42.0);
What I want to use
std::string coord;
//stream a constantly changing float into coord ...
CCLabelTTF *pCoord = CCLabelTTF::create(coord, "Arial", 42.0);
What happens : Error : no instance of overloaded function "...CCLabelTTF::create" matches the argument list. Argument types are (std::string, const char [6], int)
Basically, What I need to know is how would I feed a constantly updating string into the 1st argument?
Can distinguish between character constant and string constant giving examples
View 1 Replies View Related#include <iostream>
class Hello {
public:
void Test() {
[Code].....
As i know a non-constant member function cant be called inside a constant member function but how the above code has been compiled successfully and giving the expected result .
I am programming a translator, and I have it so that it detects words with spaces both in front of and behind them, so I did "string.append(space);" where space equals " ". That added a space to the end, but I still need a space added to the front.
View 1 Replies View RelatedI have a problem who must print the sentences who have lenght more than 20 characters. I dont know why, but it prints just the first words. Look what i made.
#include<stdio.h>
#include<conio.h>
int main()
[Code]....
For instance :
Give the number of sentences : 3
First sentence : I like the website bytes.com
Second sentence : I like more the website bytes.com
Third sentence : bytes.com
After I compile the program it should print the first two sentences.
line 27 and line 88 Im having a hard time figuring it out what the error is.
#include<iostream>
#include <cmath>
#include<algorithm>
[Code]....
Why pointer cannot be initialized with a constant like.
Code: int *p = 3000;
I have never seen anyone pass by const copy and there probably is a reason. I know that the compiler ignores top level const-ness of function arguments. There are functions which take arguments without manipulating those arguments return the result, for example the C Standard Library funcion double sqrt (double x). The function shouldn't modify it's argument, but it can since the argument isn't const.Take these two functions for example:
double square_root_1(double arg)
{
arg = 7; // we won't get the desired results
return arg * arg;
[code]....
So isn't it better to pass by const copy to make sure that you (or someone else) don't by accident modify the argument? The only disadvantage I see is that it makes the code too verbose.
//VALID:
const int CONSTANT=100;
int integerArray[CONSTANT]={ 0 };
//but after getting input let's say:
cin>>randomInteger;
int integerArray[randomInteger]; // This is invalid.
// VISUAL STUDIO 13 Says : randomInteger must be a constant; If so?
const int CONSTANT=randomInteger; //This is also invalid.
How to get user defined
//Input in a constant variable?
How to resolve this? I know dynamically allocation other than this.
I am using VISUAL STUDIO 13 ....
What is the difference between:
const int testFunction() &
int testFunction() const
#include<iostream>
using namespace std;
class Student{
public:
int age;
int rollNo,marks;
string name;
void AddEntry();
[Code] .....
error: non-member function 'void Display(Student*, int)' cannot have cv-qualifier|
why and how can I solve it?
I heard that const shall be preferred over #define . So I start to change my program accordingly.
But then below error message occurs during compilation:
#include "common.h"
#include "definition.h"
#include "particle.h"
int main() {
Particle *p = new Particle();
[Code] .....
I guess the error occurs because, when the line 9 of particle.h (File 4) is compiled, value of const int dimension is not seen by the compiler.
I have a class that I'm going to use to store a category. Right now there are seven options, but there is the potential for a whole lot more in the future. So I started off by storing an integer as the private member. I then used static constants to define the numeric values to represent each category, then a set of static constant strings that corresponds to those numbers in case I need their actual names. Finally I set up some static functions to convert between the integer value and the string, and vice versa.
I'm not sure if this is the best way to go about this. For one it makes the categories names and designations unchangeable. I thought that storing them in a file would be a better option, but then i needed a container that is the equivalent of a constant.
I thought of defining a class to contain an int and the associated string. It would be designed so that it can only be initialized with both items. Then provide no functionality to change the contents. So I've basically created my own constant.
Why do I get the error 'rec' cannot appear in a constant-expression ?
I have the following definitions:
... string rec[6];
list<rec> musicList;
...
double &val = 66.6; //illegal
const double &val = 66.6; //legal
I was just doing some demo programs and came through the above concept but not able to identify what exactly the need of the above concept . what magic exactly const is doing in the second case ?
Where exactly we can use this concept in real time programming ?
Instead of using:
Code:
x=x+k
y=y+k
z=z+k
Is there a more elegant method of adding the same constant to many variables?
Something like: Code: (x, y, z) = (x, y, z) + k ??
I am trying to use libXl to output text from a C++ program to an Excel file. The problem is coming with this library function:
bool writeStr(int row, int col, const wchar_t* value, Format* format = 0)
Writes a string into cell with specified format. If format equals 0 then format is ignored. String is copied internally and can be destroyed after call this method. Returns false if error occurs. Get error info with Book::errorMessage().
If I give input as a string literal like "Hello World" it is displayed correctly. However, if I try to give input as a variable of type const char*, it displays garbage.
Following is my code. MyCompany::company is a QString.
const char* companyName = MyCompany::company.toStdString().c_str();
sheet->writeStr(4, 0, companyName, companyFormat);
I have defined a pointer ptr2 as follows:
const void * ptr2 ( new A(20) );
In this case I expect the data pointed by ptr2 must be constant. But when running the program, the data could still be changed successfully. Why ?
Below is the program:
#include <iostream>
using namespace std;
[URL] ....
class A {
public:
A( int ID ) {
cout << "--Constructor with ID=" << ID << endl;
this->ID = ID;
[Code] .....
The output:
--Constructor with ID=20
ID of ptr2 is 20
ID of ptr2 is 21 after changed!
-~Destructor with ID=21
End of main()
Process returned 0 (0x0) execution time : 0.002 s
Press ENTER to continue.
Question: What is the significance of "const" in const void * ?
I am having a problem concerning a static const member variable I want to use to set a certain property of my class during development time. The question actually concerns proper implementation as I do have a solution that "works" at least. The variable should denote the size of a member array which I don't want to allocate on the heap due to serious performance issues. So here is my code:
//MyClass.h
class MyClass{
public:
static const int MyArraySize = 256;
private:
int MyArray[MyArraySize];
};
This works but it's not nice for two reasons:
1) It doesn't separate interface from implementation. I would prefer to define the variable in the corresponding .cpp file but it doesn't work:
//MyClass.h
class MyClass{
public:
static const int MyArraySize;
[Code] .....
If I delete the line int MyArray[MyArraySize]; the above code works but when I use it to define the size of the array I get a "constant expression expected" error for the line int MyArray[MyArraySize]; which makes sense as the compiler does not know the value of MyArraySize when he reaches int MyArray[MyArraySize]; and therefore can not allocate the memory. Of course I can move MyArray to the heap like that:
//MyClass.h
class MyClass{
public:
static const int MyArraySize;
static const int MyValue;
[Code] .....
But as I mentioned before this causes a remarkable loss of performance.
Something like the following does not work:
//MyClass.h
class MyClass{
public:
static const int MyArraySize = (int) pow(2, 8);
private:
int MyArray[MyArraySize];
};
This gives a "constant expression expected" error for the line static const int MyArraySize = (int) pow(2, 8);
Interestingly the following code works:
//MyClass.h
class MyClass{
public:
static const int MyValue;
};
//MyClass.cpp
#include "MyClass.h"
const int MyClass::MyValue = (int) pow(2, 8);
So if I use pow outside of the class definition I get no errors. Is there any solution to those problems? So what I want is:
1) Don't allocate the array on the heap
2) Separate interface from implementation
3) Being able to use functions like pow to define MyArraySize
4) Not use global variables
I've go this code that I need to use a Named Constant for the price of a t-shirt.
I've already done the code from a previous lab for college and I'm not sure how to proceed.
Here is the code:
int main()
{
int tno, price=12,cost;
float discount;
[Code].....
not asking to have this done for me just a hint at where to put the Named Constant
I've been given specific instructions to create an array inside a Class Matrix using a constant n. This is my class but I am getting errors. I thought that maybe I had to initialize the const and the array using the constructor function Matrix() instead of directly in the class, but I didn't have any luck with that either.
class Matrix
{
public:
Matrix();
private:
const int n=3;
int e[n][n];
};
How can you convert int type to const char*
View 2 Replies View RelatedThis is going to seem like a stupid question but how can I count the elements of a const array. Surely c++ compilers provide a count for const arrays?
const wchar_t* ItemHandler::itemNames[] = {L"Coins", L"Matches"};
const wchar_t* ItemHandler::itemIconLocations[] = {L"./media/Items/coins.png" , L"./media/Items/matches.png"};
const bool ItemHandler::itemStackable[] = {1, 1};
I want to count the elements so if the server sends a bad item id it won't crash every client in range lol. I heard that the sizeof keyword returns the size of the array in bytes. I used to think the size of keyword would return the element count but found out it isn't.
Here's a part of my program. 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
[Code]...
I need to iterate through a vector in a const function, and, as my function is called very often, to get more performances I need my iterator to be declared somewhere else than the function, so it doesn't have to get deleted and recreated over and over again. So here is my code:
class Shop {
public:
//methods
virtual void draw(sf::RenderTarget &target, sf::RenderStates states) const{
[Code] ....
Seems great? Well no. I actually get an error on the "for" line.
It tells me : "You can't use '=' here" and "You can't use the ++ operator here"
The thing is, if I actually declare my iterator in the loop, the compiler stops giving me warnings, but, as I said, I really want to avoid doing that.