开发者

C++ protecting a private char * of a class

I understand the differences of inheritance of public, private, protected with respect to class method / property constuct开发者_如何学Cs. However, my question is specifically associated with pointers to null terminated strings..

class MyClass
{
   private: 
      char * SomeValue;

   ...
   ...
}

Now, somewhere through processing, the MyClass->SomeValue gets allocated and populated with a string value. No problem. Now, I want some calling source that has an instance of my "MyClass" object created and needs the string value from this. Since C++ can do lots of damage with pointers, and pointers to pointers etc, I want to return the pointer location to the string of chars allocated, but don't want anyone to change values. Is this default controlled inside the compiler and memory management? Its a low risk that anyone would be using this class as its primarily for internal purposes, but just more of my understanding.

Thanks


Typically you would return a const pointer to the chars. There is nothing that can stop someone casting the return value to a non const. But C++ isn't designed to defend against malicious coding.

class MyClass {
  char* someValue;
public:
  const char* get_SafeSomeValue() const {
    return someValue;
  }
};


No, you can't really do that.

Even if you do something like other people are suggesting, for example:

class MyClass {
    char* someValue;
public:
    const char* get_SafeSomeValue() const {
        return someValue;
    }
};

You still can invoke delete myClassInstance->get_SafeSomeValue().


Why would you use a naked pointer to hold a string in C++ when std::string provides a cleaner, safer method of containing and transporting a string around? What you want to do, it sounds like, is get at the string's value without allowing another class/function/method to change the value? Just return it with const as a modifier:

const char* foo();


You can use:

private:
    const string someValue;
public:
    const string& getReadOnlyValue() { return someValue;};


You could do this:

class MyClass
{
   public:
      const char * getValue(); // return a pointer to a const char that can't be modified
   private: 
      char * SomeValue;

   ...
   ...
}


If you don't users to change the data that they point to you can have functions that return const char *.

class MyClass
{
  private:
    char * SomeValue;


  public:
    const char * getSomeValue() const
    {
       return SomeValue;
    }
};

You need to be careful about objects of your class being copied and assigned. For practical purposes you might want to replace your char* with a std::string or std::vector< char>. Failing that you can either:

  • Make your copy-constructor and assignment operator private and not implement them.
  • Overload them to work properly.
  • Derive from boost::noncopyable.


What you want to do is not possible. c++ provides no mechanism to prevent people from shooting themselves in foot. For example, how would you prevent someone from calling delete on that pointer?


It is possible - its just os-dependent to implement.

These things are handled via protected memory pages - you can allocate several memory pages and set them to eg. "read-only" - every write access will then fail with an exception. It just needs a little programmatical overhead.

In WindowsAPI VirtualAllocEx is a good starter...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜