[comp.lang.c++] char *const assignment in constructor

qbarnes@urbana.mcd.mot.com (Quentin Barnes) (02/27/91)

I would like to have a variable of type "char *const" (pointer itself
is const, not what it points to.)  However, I would like to delay its
initialization until the contructor is called for its class.  Once it
is initialized there, I would like the variable treated as const.

Is there any way to do this in C++?

Quentin Barnes
qbarnes@urbana.mcd.mot.com
..!uiucuxc!udc!qbarnes
-- 
Quentin Barnes
qbarnes@urbana.mcd.mot.com
.!uiucuxc!udc!qbarnes

jbuck@galileo.berkeley.edu (Joe Buck) (02/27/91)

In article <1991Feb26.195200.8763@urbana.mcd.mot.com> qbarnes@urbana.mcd.mot.com (Quentin Barnes) writes:
>I would like to have a variable of type "char *const" (pointer itself
>is const, not what it points to.)  However, I would like to delay its
>initialization until the contructor is called for its class.  Once it
>is initialized there, I would like the variable treated as const.
>
>Is there any way to do this in C++?

Yes.

class Quentin {
private:
	char *const constPtr;
public:
	Quentin (char *arg) : constPtr(arg) {}
};

This is one of the two cases where you must use the "colon" notation
to initialize members; the other is for reference members.  That is,
writing the constructor

	Quentin (char *arg) { constPtr = arg; }

is illegal.  Why?  Because the language designers tried to make a
distinction between initialization and assignment -- you can initialize
a const object or a reference but you cannot assign to it.



--
--
Joe Buck
jbuck@galileo.berkeley.edu	 {uunet,ucbvax}!galileo.berkeley.edu!jbuck	

rfg@NCD.COM (Ron Guilmette) (03/03/91)

In article <1991Feb26.195200.8763@urbana.mcd.mot.com> qbarnes@urbana.mcd.mot.com (Quentin Barnes) writes:
>I would like to have a variable of type "char *const" (pointer itself
>is const, not what it points to.)  However, I would like to delay its
>initialization until the contructor is called for its class.  Once it
>is initialized there, I would like the variable treated as const.
>
>Is there any way to do this in C++?

If you want the "variable" in question to be a *member* of some (containing)
class, then the answer to your question is "Yes, this can be done in C++."
For example:

	class C {
		char *const ccp;
	public:
		C (char *arg);
	};

	C::C (char *arg) : ccp(arg)
	{
		// ...
	}

Here the data member C::ccp gets initialized when any object of type C
is constructed.  From that time onwards, the ccp member of each such
object is treated as a const value (which cannot be modified).

(Note that the thing which ccp points to is *not* considered to be const
however.)

Not only is this kind of initialization (of const data members) possible,
it is actually NECESSARY!  As noted in 12.6.3 of the ARM, "This is the
only way to initialize nonstatic const and reference members."

-- 

// Ron Guilmette  -  C++ Entomologist
// Internet: rfg@ncd.com      uucp: ...uunet!lupine!rfg
// New motto:  If it ain't broke, try using a bigger hammer.