Re: Whats the deal with 'const'?




Malcolm wrote:
The second problem is this

struct employee
{
char *name;
float salary;
};

void payroll(const employee *emp)

? void payroll(const employee **emp)

{

/* evil hacker */
strcpy(emp[0]->name, "Fred the Hacker");

? strcpy(emp->name, "Fred the Hacker");

pay(&emp[0]);
}

void payroll(const employee *emp);

In this case, the const keyword qualifies the object it points to as
constant. The object `name' is that one pointed by emp, so this is an
error:
/*(*emp).name = "C";*/

The object `*name' is pointed by name, not emp. That const does not
prevent following from being done:
*(*emp).name = 'c';

? void payroll(const employee **emp)

So does the const keyword applying to multiple-level pointer.
/*(**emp).name = "C";*/
*(**emp).name = 'c';

lovecreatesbeauty

.



Relevant Pages

  • Re: PPP Payroll Example
    ... > public abstract void accept; ... > public class SalariedClass extends PayClass{ ... it is truly obfuscation to replace a simple IF in employee. ... instantiate a temporary relationship with the correct subclass. ...
    (comp.object)
  • Re: Whats the deal with const?
    ... void payroll(const employee *emp) ... void payroll ... So does the const keyword applying to multiple-level pointer. ...
    (comp.lang.c)
  • Re: problems in saving to a text file
    ... > when adding the 3rd employee data, this is what appears in the text file ... int search_id(EMPLOYEE *p, const char *fname, const char *id)) ... void getstring; ...
    (comp.lang.c)
  • RE: Protected keyword
    ... Only the Employee class internally can see the base class RaiseEvent method. ... public void HoldBreath{ ... a child that inherits from it so as to be able to call the protected method ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: confused with the output c++
    ... > void printShort(ostream &out, Employee &e) ... > //save new record in the database ... > //ask user to enter int parameter with specified prompt ...
    (alt.comp.lang.learn.c-cpp)