473,320 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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.

Nov 15 '06 #1
4 1245
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
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

<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
<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Me | last post by:
Hi, I would like learn from people with experience in C++, which of the following styles of way to construct "get/set" member functions would be the best in terms of usability, speed, et cetera. ...
4
by: Steven T. Hatton | last post by:
Below is some code I wrote to get a better understanding of the dynamic verses static type resolution. My intention was to see if there was a way to use references for the static access to the...
2
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't...
3
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming...
2
by: Lionel B | last post by:
I have a function which takes a functor argument. I which to call it for a functor which is actually a class member; this works fine, using the mem_fun_ref and bind1st functions (see listing 1...
23
by: mike3 | last post by:
Hi. (posted to both newsgroups since I was not sure of which would be appropriate for this question or how specific to the given language it is. If one of them is inappropriate, just don't send...
12
by: hweekuan | last post by:
hi, it seems i can't assign the const variable u in class A, one way to solve the problem may be to build a copy constructor. however, why does C++ or vector class not like this code? my g++ is:...
20
by: mike3 | last post by:
Hi. (Xposted to both comp.lang.c++ and comp.programming since I've got questions related to both C++ language and general programming) I've got the following C++ code. The first routine runs in...
15
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
7
by: Immortal Nephi | last post by:
I have an idea how to design an object in a better way. I would like to give you my thought example. Please let me know what you think if it is best object design. Please recommend me any book...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.