Connecting Tech Pros Worldwide Help | Site Map

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

 
LinkBack Thread Tools Search this Thread
  #1  
Old November 15th, 2006, 01:45 PM
wenmang@yahoo.com
Guest
 
Posts: n/a
Default a better way to use a const ptr as class member?

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.


  #2  
Old November 15th, 2006, 02:05 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: a better way to use a const ptr as class member?

wenmang@yahoo.com wrote:
Quote:
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


  #3  
Old November 15th, 2006, 02:15 PM
mlimber
Guest
 
Posts: n/a
Default Re: a better way to use a const ptr as class member?

wenmang@yahoo.com wrote:
Quote:
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

  #4  
Old November 15th, 2006, 06:05 PM
Howard
Guest
 
Posts: n/a
Default Re: a better way to use a const ptr as class member?


<wenmang@yahoo.comwrote in message
news:1163603177.795229.60180@k70g2000cwa.googlegro ups.com...
Quote:
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



  #5  
Old November 15th, 2006, 06:25 PM
Jim Langston
Guest
 
Posts: n/a
Default Re: a better way to use a const ptr as class member?

<wenmang@yahoo.comwrote in message
news:1163603177.795229.60180@k70g2000cwa.googlegro ups.com...
Quote:
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.



 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.