By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,605 Members | 1,415 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,605 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.

Nov 15 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
we*****@yahoo.com wrote:
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[s], 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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 15 '06 #2

P: n/a
we*****@yahoo.com wrote:
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

Nov 15 '06 #3

P: n/a

<we*****@yahoo.comwrote in message
news:11*********************@k70g2000cwa.googlegro ups.com...
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

Nov 15 '06 #4

P: n/a
<we*****@yahoo.comwrote in message
news:11*********************@k70g2000cwa.googlegro ups.com...
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.

Nov 15 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.