spot7.org logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

Problems with changing a const char pointer


If you use std::string to store the description in the appointment class then you can make things much easier for yourself and for the people who end up working on your code. The changeDescription method would then become this:

#include <string>

void appointment::changeDescription(std::string
const& s){
    this->desc = s;
}

And change the calling code to this:

std::string desc;

Then all of the annoying memory management that causes the problems you are facing are fixed pretty much for free (in terms of programming effort that is). Generally speaking this is considered to be better idiomatic c++ code than using null terminated C-style char arrays.

Another thing about your code is that you really should check that s is not null before you try to copy it, not afterwards.


Categories : C++

Related to : Problems with changing a const char pointer
Understanding Pointer to constant pointer to integer constant (const int * const * variable)
But i was wondering why is this happening since pcpci is not a constant pointer No, but *(pcpci+1) is. It has type const int* const. Obviously you can't assign anything to that.

Categories : C
Getting null pointer exception along with a few other problems in a permutations program? (Java)
In your constructor, you neglect to initialize L when list.size() == 0: if(list.size() == 0) { hasNext = false; } should be if(list.size() == 0) { hasNext = false; L = new ArrayList<E>(); }

Categories : Java
Universal member-function definition, instanceable from both 'const' & 'non-const' objects
@dyp's answer by way of comment was good, but I would use a static member function rather than a friend function: friend functions have no access control. template <typename T> struct sList { void DoForEachList(auto pFunc) { DoForEachListHelper(*this, pFunc); } void DoForEachList(auto pFunc) const { DoForEachListHelper(*this, pFunc); } private: st

Categories : C++
According to the spec, is const required with auto for const references?
Yes, this behaviour is entirely standard-mandated: [C++11: 7.1.6.4/5]: A program that uses auto in a context not explicitly allowed in this section is ill-formed. [C++11: 7.1.6.4/6]: Once the type of a declarator-id has been determined according to 8.3, the type of the declared variable using the declarator-id is determined from the type of its initializer using the rules for template arg

Categories : C++
const vector implies const elements?
Yes, const vector provides access to it's elemets as if they were const (that is, gives you const references). In your second function not the objects of type A are const, but pointers to them. A pointer being const does not mean that the object the pointer is pointing to is const. To declare pointer-to-const, use the type A const *.

Categories : C++
Recently Add
C++: error C2143: syntax error : missing ';' before '<'
Converting 2s compliment to a decimal. Output is double
how do you compare a string to a vector value?
Converting a long double to double with upward (or downward) rounding
C++: operator<< overloading in the nested classes
Unclear behavior with csv processing using getline
C++ Visual Studio Error: IntelliSense: expected a statement
Game of Nim - showing the remaining numbers and letting a player pick their name?
malloc 1D array in struct
How to create an array of smart pointers?
c++ swapping unique_ptr's
Functions as arguments
Any Fast & Efficient way to generate a 3D Grid?
Is string[] not a type?
C++ typeid(x).name() returns ph
Counting / Printing Path - From (1,1) to (m, n)
C++ input function that calls itself
Error: array must be initialized with a brace-enclosed initializer
SDL_ConvertSurface() causes break
C++ std deviation function?
libcURL Progress Function not being called
What is QList's maximum size?
Run batch with C++
Read a file and write its contents to another C++
extract from stringstream into 2D vector
How to stop Scons adding lib infront of a shared library
Blocking vs non-blocking mode in TCP sockets using C++
SDL2 toggle SDL_WINDOW_RESIZABLE state for fake fullscreen
How can I assign value to specific vector's index?
Operator Overloading with Constant Iterators
© Copyright 2017 spot7.org Publishing Limited. All rights reserved.