a better way to use a const ptr as class member?

W

wenmang

Hi,

I like to use a const pointer to a shm inside a class, and I don't want
to instantiate it through class ctor, is there any other way around?
The main purpose for const pointer is to avoid somebody accidently
re-assign the pointer to something else in realtime.
 
V

Victor Bazarov

I like to use a const pointer to a shm inside a class, and I don't
want to instantiate it through class ctor, is there any other way
around? The main purpose for const pointer is to avoid somebody
accidently re-assign the pointer to something else in realtime.

Please give an example. If you don't want to initialise it in the
class c-tor, where would you initialise it (hint: c-tors are the
only place where it's legally possible)? If you don't want "somebody"
to "accidentally re-assign" it, how are _you_ different from that
"somebody"? You are trying to [re]assign it somewhere, aren't you?
If you don't initialise it, the only other way to give it a value is
to assign it, right?

V
 
M

mlimber

I like to use a const pointer to a shm inside a class, and I don't want
to instantiate it through class ctor, is there any other way around?
The main purpose for const pointer is to avoid somebody accidently
re-assign the pointer to something else in realtime.

The way you should strongly prefer is to initialize it in the ctor so
that you can establish the pointer's validity as an invariant for the
life of the object. You could come up with some ugly hack like writing
a pointer wrapper with a private function for assignment but giving
friend access to your class' function that does the assignment, but the
former method is much to be preferred for maintainability and clarity.

Cheers! --M
 
H

Howard

Hi,

I like to use a const pointer to a shm inside a class, and I don't want
to instantiate it through class ctor, is there any other way around?
The main purpose for const pointer is to avoid somebody accidently
re-assign the pointer to something else in realtime.

Who are you protecting it from? Other code in the same class? Code in
other classes? Other programmers?

If you're talking about protecting it from code outside the class, you could
just make it private instead of const. You can add a public "getter"
function (returning a const pointer) for code that might need to use the
pointer from outside the class.

If the initialization of that pointer also needs to be made from code
outside the class, then you can also make a public "setter" function. In
that function, only allocate the new object for that pointer if the pointer
is NULL. (Naturally, this means setting the pointer to NULL in the
constructor.)

If that's not what you're talking about, you'll need to explain further.

-Howard
 
J

Jim Langston

Hi,

I like to use a const pointer to a shm inside a class, and I don't want
to instantiate it through class ctor, is there any other way around?
The main purpose for const pointer is to avoid somebody accidently
re-assign the pointer to something else in realtime.

Untested code:

class MyClass
{
private:
shm* MyPointer;
public:
MyClass(): MyPointer( NULL ) {}
const shm* getPointer( ) const { return MyPointer; }
void setPointer( const shm* Pointer ) { MyPointer = Pointer; }
};

This should do it for you I would think? No one can "accidently" change the
pointer, the only way to change the pointer is by calling
myinstance.setPointer( somepointer ); and I dont' think anyone could do that
on "accident".

If you're worried about MyClass changing the pointer itself, then you'd need
to make it a const pointer and initialize it in the initialization list.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,691
Members
48,796
Latest member
Greg L.

Latest Threads

Top